Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I am trying to create a code that shows up on an 8x8 matrix with my arduino a \"

ID: 3607125 • Letter: I

Question

I am trying to create a code that shows up on an 8x8 matrix with my arduino a "bouncing ball". Basically one led from the matrix going and bouncing off the corners and sides. I can't seem to figure it out. ALl help is appreciated. Thanks

#include <TimerOne.h>//this is a library that uses timer 1 of the arduino to trigger interrupts in certain time intervals

//This defines a matrix defining the letter "E" for the 8x8 LED matrix display
#define E {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 0, 0, 0, 0, 0, 0}
}

//This defines a matrix
#define q {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}

//This defines a matrix
#define G {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}

// This defines a matrix
#define h {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}

//THis defines a matrix
#define j {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}

//This defines a matrix
#define k {

{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}
// This defines a matrix
#define l {
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}

//This defines a matrix
#define m {
{0, 0, 0, 0, 0, 0, 0, 1},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0}
}
  
const int row[8] = {
2,7,17,5,13,16,12,14 };

const int col[8] = {
6,11,10,3,15,4,8,9 };

volatile byte c,r,flag,counter;

volatile byte pattern[8][8]=E;
volatile byte pattern[8][8]=F;
volatile byte pattern[8][8]=G;
volatile byte pattern[8][8]=h;
volatile byte pattern[8][8]=j;
volatile byte pattern[8][8]=k;
volatile byte pattern[8][8]=l;
volatile byte pattern[8][8]=m;

byte pattern[8][8]=E;
unsigned long previousMillis =0;
unsigned long interval = 1000;
int currentPattern =0;

void setup() {
// initialize the row and column pins as outputs
// iterate through the pins:
for (int pin = 0; pin < 8; pin++) {
// initialize the output pins:
pinMode(col[pin], OUTPUT);
digitalWrite(col[pin], HIGH);
pinMode(row[pin], OUTPUT);
digitalWrite(row[pin], LOW);  
// take the col pins (i.e. the cathodes) high and the row pins (anodes) low to ensure that
// the LEDS are off:
}
Timer1.initialize(100); // initialize timer1, and set a 100 us second period for the interrupt interval (i.e. the ISR will be called
//every 100 us - this seems to be a good frequency to achieve a flicker-free LED display.
//experiment with this parameter. If it gets too small the ISR starts 'eating up' all the processor time, and the main loop becomes very slow
Timer1.attachInterrupt(refreshScreen); // attaches the refreshScreen() function as 'Interrupt Service Routine' (ISR) to the interrupt
//this means that every time 100 us have passed, the refreshScreen() routine will be called.
}


}
//main loop...here we can simply busy ourselves with changing the pattern[][] array; nothing deals with the LED display.
//this is all handled via the ISR
void loop() {
unsigned long currentMillis = millis();
if(currentMIllis - previousMillis > interval)
{
if(currentPattern==0)
{
for(int i =0; i<8; i++)
{
for (int j=0; j<8;j++)
{
pattern[i][j]=patternq[i][j];
delay(100);
pattern[i][j]=patternG[i][j];
delay(100);
pattern[i][j]=patternh[i][j];
delay(100);
pattern[i][j]=patternj[i][j];
delay(100);
pattern[i][j]=patternk[i][j];
delay(100);
pattern[i][j]=patternl[i][j];
delay(100);
pattern[i][j]=patternm[i][j];
}
}
currentPattern =1;
}

  
//here we simply enter 1s and 0s into the pattern[][] array randomly. This causes a wild flickering of all the LEDs of the display
//just an example how you could play with the display in the main loop without having to worry about actually strobing the display LEDs.
//pattern[random(0, 8)][random(0, 8)]=1;//turn a random LED on
//pattern[random(0, 8)][random(0, 8)]=0;//turn a random LED off
}
}

Explanation / Answer

void sendSerialData (
byte registerCount, // How many shift registers?
byte *pValueArray)   // Array of bytes with LSByte in array [0]
{
// Signal to the 595s to listen for data
digitalWrite (g_pinCommLatch, LOW);

for (byte reg = registerCount; reg > 0; reg--)
{
    byte value = pValueArray [reg - 1];
   
    for (byte bitMask = 128; bitMask > 0; bitMask >>= 1)
    {
      digitalWrite (g_pinClock, LOW);
   
      digitalWrite (g_pinData, value & bitMask ? HIGH : LOW);
       
      digitalWrite (g_pinClock, HIGH);
    }
}
// Signal to the 595s that I'm done sending
digitalWrite (g_pinCommLatch, HIGH);
} // sendSerialData

// Print a message specifying valid inputs, given the number of registers defined and then consume all current input.
void badNumber ()
{
int dummy;

Serial.print ("Please enter a number from 0 to ");
for (int loop = 0; loop < g_registers; loop++)
{
    Serial.print ("9");
}
Serial.println (" inclusive.");

while (Serial.available () > 0)
{
    dummy = Serial.read ();
   
    // Necessary to get all input in one go.
    delay (10);
}
} //badNumber

// Read a number from the PC with no more digits than the defined number of registers.
// Returns: number to display. If an invalid number was read, the number returned is the current number being displayed
//
int readNumberFromPC ()
{
byte incomingByte;
int numberRead;
byte incomingCount;

if (Serial.available () > 0)
{
    numberRead = 0;
    incomingCount = 0;
     
    while (Serial.available () > 0)
    {
      incomingByte = Serial.read () - 48;
      incomingCount++;
     
      if (incomingByte < 0 || incomingByte > 9 || incomingCount > g_registers)
      {
        badNumber ();
        return g_numberToDisplay;
      }
     
      numberRead = 10 * numberRead + incomingByte;
     
      // Necessary to get all input in one go.
      delay (10);
    }

    Serial.print ("Now displaying: ");
    Serial.println (numberRead, DEC);
   
    return numberRead;
}

return g_numberToDisplay;
} // readNumberFromPC


void loop()
{
g_numberToDisplay = readNumberFromPC ();

if (g_numberToDisplay < 10)
{
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [0];
    g_registerArray [0] = g_digits [g_numberToDisplay];
}
else if (g_numberToDisplay < 100)
{
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [0];
    g_registerArray [1] = g_digits [g_numberToDisplay / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
}
else if (g_numberToDisplay < 1000)
{
    g_registerArray [3] = g_digits [0];
    g_registerArray [2] = g_digits [g_numberToDisplay / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
}
else
{
    g_registerArray [3] = g_digits [g_numberToDisplay / 1000];
    g_registerArray [2] = g_digits [(g_numberToDisplay % 1000) / 100];
    g_registerArray [1] = g_digits [(g_numberToDisplay % 100) / 10];
    g_registerArray [0] = g_digits [g_numberToDisplay % 10];
}

sendSerialData (g_registers, g_registerArray);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote