8x8 Led matrix turn signal control by switches

I having a problem with my right and left signal.
I connect the 8x8 LED matrix to 2 shift register.
I desire to create a code with (left switch on) left signal on for 1 second then off for 1 second and keep repeat. This goes same to right switch.

This is my code. The output of this code is when left switch on the left signal blinking very fast. The blinking rate as i desire is 1 sec for on and off.

int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int inputPin2 = 2;            // input pin for left (switch) 
int inputPin3 = 3;            //input pin for right (switch)
int valleft = 0;
int valright =0;

int LEDs[8][8] = {   
  {0, 0, 0, 0, 0, 0, 0, 0},      //blank
  {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}      
};

int right[8][8] = {   
  {0, 0, 0, 0, 1, 0, 0, 0},      //right signal 
  {0, 0, 0, 0, 1, 1, 0, 0},
  {1, 1, 1, 1, 1, 1, 1, 0},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 0},
  {0, 0, 0, 0, 1, 1, 0, 0},
  {0, 0, 0, 0, 1, 0, 0, 0}      
};

int left[8][8] = {   
  {0, 0, 0, 1, 0, 0, 0, 0},        //left signal 
  {0, 0, 1, 1, 0, 0, 0, 0},
  {0, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {1, 1, 1, 1, 1, 1, 1, 1},
  {0, 1, 1, 1, 1, 1, 1, 1},
  {0, 0, 1, 1, 0, 0, 0, 0},
  {0, 0, 0, 1, 0, 0, 0, 0}      
};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
}

void loop() {
valleft = digitalRead (inputPin2);
valright = digitalRead (inputPin3);  

  if (valleft == HIGH) {      //when the left switch is on 
lightLED(left);                    //display left signal 
delay(1000);                      //waits for 1 sec ( this code seem like not working) 
lightLED(LEDs);                   // off 
delay(1000):
  }
  else if (valright == HIGH) {
lightLED(right);
delay(1000);
lightLED(LEDs);
delay(1000):
  }
else 
lightLED(LEDs);
}
void lightLED(int tempLED[8][8]) {          //run the lightLED  bit by bit 
  byte columnbitsToSend = 0;
  byte rowbitsToSend = 0;
  
  int x = 0;
  int y = 0;
  for (x = 0; x < 8; x++) {
    columnbitsToSend = 0;
    rowbitsToSend = 0;
      
    for (y = 0; y < 8; y ++) {
      if (tempLED[x][y] == 1) {
        bitWrite(rowbitsToSend, y, HIGH);
      } 
    }
    
    digitalWrite(latchPin, LOW);
    bitWrite(columnbitsToSend, x, HIGH);
    shiftOut(dataPin, clockPin, MSBFIRST, columnbitsToSend);
    shiftOut(dataPin, clockPin, MSBFIRST, rowbitsToSend);
    digitalWrite(latchPin, HIGH);   
    delay(2);
  }
  
}

(code tags added by moderator)

You need to make the delay part of the multiplexing, instead of cyclying thru all the data and then delaying.
Run the multiplexing for longer than just the one pass it gets now.

int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int inputPin2 = 2;            // input pin for left (switch) 
int inputPin3 = 3;            //input pin for right (switch)
int valleft = 0;
int valright =0;

int LEDs[8][8] = {   
  {
    0, 0, 0, 0, 0, 0, 0, 0        }
  ,      //blank
  {
    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        }      
};

int right[8][8] = {   
  {
    0, 0, 0, 0, 1, 0, 0, 0        }
  ,      //right signal 
  {
    0, 0, 0, 0, 1, 1, 0, 0        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 0        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 0        }
  ,
  {
    0, 0, 0, 0, 1, 1, 0, 0        }
  ,
  {
    0, 0, 0, 0, 1, 0, 0, 0        }      
};

int left[8][8] = {   
  {
    0, 0, 0, 1, 0, 0, 0, 0        }
  ,        //left signal 
  {
    0, 0, 1, 1, 0, 0, 0, 0        }
  ,
  {
    0, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    0, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    0, 0, 1, 1, 0, 0, 0, 0        }
  ,
  {
    0, 0, 0, 1, 0, 0, 0, 0        }      
};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
}

void loop() {
  int t;
  valleft = digitalRead (inputPin2);
  valright = digitalRead (inputPin3);  

  if (valleft == HIGH) {                 //when the left switch is on 
    for (t = 0; t < 64) lightLED(left);  //display left signal 
    lightLED(LEDs);                      // off 
    delay(1000):
  }
  else if (valright == HIGH) {
    for (t = 0; t < 64) lightLED(right);
    lightLED(LEDs);
    delay(1000):
  }
  else 
    lightLED(LEDs);
}
void lightLED(int tempLED[8][8]) {          //run the lightLED  bit by bit 
  byte columnbitsToSend = 0;
  byte rowbitsToSend = 0;

  int x;
  int y;
  for (x = 0; x < 8; x++) {
    columnbitsToSend = 0;
    rowbitsToSend = 0;

    for (y = 0; y < 8; y ++) {
      if (tempLED[x][y] == 1) {
        bitWrite(rowbitsToSend, y, HIGH);
      } 
    }

    digitalWrite(latchPin, LOW);
    bitWrite(columnbitsToSend, x, HIGH);
    shiftOut(dataPin, clockPin, MSBFIRST, columnbitsToSend);
    shiftOut(dataPin, clockPin, MSBFIRST, rowbitsToSend);
    digitalWrite(latchPin, HIGH);   
    delay(2);
  }

}

Paul__B:

int latchPin = 8;

int clockPin = 12;
int dataPin = 11;
int inputPin2 = 2;            // input pin for left (switch)
int inputPin3 = 3;            //input pin for right (switch)
int valleft = 0;
int valright =0;

int LEDs[8][8] = {   
  {
    0, 0, 0, 0, 0, 0, 0, 0        }
  ,      //blank
  {
    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        }     
};

int right[8][8] = {   
  {
    0, 0, 0, 0, 1, 0, 0, 0        }
  ,      //right signal
  {
    0, 0, 0, 0, 1, 1, 0, 0        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 0        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 0        }
  ,
  {
    0, 0, 0, 0, 1, 1, 0, 0        }
  ,
  {
    0, 0, 0, 0, 1, 0, 0, 0        }     
};

int left[8][8] = {   
  {
    0, 0, 0, 1, 0, 0, 0, 0        }
  ,        //left signal
  {
    0, 0, 1, 1, 0, 0, 0, 0        }
  ,
  {
    0, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    1, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    0, 1, 1, 1, 1, 1, 1, 1        }
  ,
  {
    0, 0, 1, 1, 0, 0, 0, 0        }
  ,
  {
    0, 0, 0, 1, 0, 0, 0, 0        }     
};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
}

void loop() {
  int t;
  valleft = digitalRead (inputPin2);
  valright = digitalRead (inputPin3);

if (valleft == HIGH) {                 //when the left switch is on
    for (t = 0; t < 64) lightLED(left);  //display left signal
    lightLED(LEDs);                      // off
    delay(1000):
  }
  else if (valright == HIGH) {
    for (t = 0; t < 64) lightLED(right);
    lightLED(LEDs);
    delay(1000):
  }
  else
    lightLED(LEDs);
}
void lightLED(int tempLED[8][8]) {          //run the lightLED  bit by bit
  byte columnbitsToSend = 0;
  byte rowbitsToSend = 0;

int x;
  int y;
  for (x = 0; x < 8; x++) {
    columnbitsToSend = 0;
    rowbitsToSend = 0;

for (y = 0; y < 8; y ++) {
      if (tempLED[x][y] == 1) {
        bitWrite(rowbitsToSend, y, HIGH);
      }
    }

digitalWrite(latchPin, LOW);
    bitWrite(columnbitsToSend, x, HIGH);
    shiftOut(dataPin, clockPin, MSBFIRST, columnbitsToSend);
    shiftOut(dataPin, clockPin, MSBFIRST, rowbitsToSend);
    digitalWrite(latchPin, HIGH);   
    delay(2);
  }

}

May i know why the delay command work with
lightLED(LEDs);
delay(1000); //it is working

and does not work with
lightLED(right);
delay(1000); //not working

My apologies - I corrected the program flow, but failed to spot the glaring typo! Picked that up on my little netbook reading in bed this morning. :smiley:

This should work now - I think!

int latchPin = 8;
int clockPin = 12;
int dataPin = 11;
int inputPin2 = 2;            // input pin for left (switch) 
int inputPin3 = 3;            //input pin for right (switch)
int valleft = 0;
int valright =0;

int LEDs[8][8] = {   
  { 0, 0, 0, 0, 0, 0, 0, 0 } , //blank
  { 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 }   
};

int right[8][8] = {   
  { 0, 0, 0, 0, 1, 0, 0, 0 } , //right signal 
  { 0, 0, 0, 0, 1, 1, 0, 0 } ,
  { 1, 1, 1, 1, 1, 1, 1, 0 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 0 } ,
  { 0, 0, 0, 0, 1, 1, 0, 0 } ,
  { 0, 0, 0, 0, 1, 0, 0, 0 }    
};

int left[8][8] = {   
  { 0, 0, 0, 1, 0, 0, 0, 0 } , //left signal 
  { 0, 0, 1, 1, 0, 0, 0, 0 } ,
  { 0, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 0, 1, 1, 1, 1, 1, 1, 1 } ,
  { 0, 0, 1, 1, 0, 0, 0, 0 } ,
  { 0, 0, 0, 1, 0, 0, 0, 0 }    
};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
}

void loop() {
  int t;
  valleft = digitalRead (inputPin2);
  valright = digitalRead (inputPin3);  

  if (valleft == HIGH) {                 //when the left switch is on 
    for (t = 0; t < 64; t++) lightLED(left);  //display left signal 
    lightLED(LEDs);                      // off 
    delay(1000);
  }
  else if (valright == HIGH) {
    for (t = 0; t < 64; t++) lightLED(right);
    lightLED(LEDs);
    delay(1000);
  }
  else 
    lightLED(LEDs);
}
void lightLED(int tempLED[8][8]) {          //run the lightLED  bit by bit 
  byte columnbitsToSend = 0;
  byte rowbitsToSend = 0;

  int x;
  int y;
  for (x = 0; x < 8; x++) {
    columnbitsToSend = 0;
    rowbitsToSend = 0;

    for (y = 0; y < 8; y ++) {
      if (tempLED[x][y] == 1) {
        bitWrite(rowbitsToSend, y, HIGH);
      } 
    }

    digitalWrite(latchPin, LOW);
    bitWrite(columnbitsToSend, x, HIGH);
    shiftOut(dataPin, clockPin, MSBFIRST, columnbitsToSend);
    shiftOut(dataPin, clockPin, MSBFIRST, rowbitsToSend);
    digitalWrite(latchPin, HIGH);   
    delay(2);
  }
}

Didn't notice how badly "Auto Format" had mangled the arrays either!


And a few more bugs - wouldn't compile.

Paul__B:
My apologies - I corrected the program flow, but failed to spot the glaring typo! Picked that up on my little netbook reading in bed this morning. :smiley:

This should work now - I think!

int latchPin = 8;

int clockPin = 12;
int dataPin = 11;
int inputPin2 = 2;            // input pin for left (switch)
int inputPin3 = 3;            //input pin for right (switch)
int valleft = 0;
int valright =0;

int LEDs[8][8] = {   
  { 0, 0, 0, 0, 0, 0, 0, 0 } , //blank
  { 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 }   
};

int right[8][8] = {   
  { 0, 0, 0, 0, 1, 0, 0, 0 } , //right signal
  { 0, 0, 0, 0, 1, 1, 0, 0 } ,
  { 1, 1, 1, 1, 1, 1, 1, 0 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 0 } ,
  { 0, 0, 0, 0, 1, 1, 0, 0 } ,
  { 0, 0, 0, 0, 1, 0, 0, 0 }   
};

int left[8][8] = {   
  { 0, 0, 0, 1, 0, 0, 0, 0 } , //left signal
  { 0, 0, 1, 1, 0, 0, 0, 0 } ,
  { 0, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 1, 1, 1, 1, 1, 1, 1, 1 } ,
  { 0, 1, 1, 1, 1, 1, 1, 1 } ,
  { 0, 0, 1, 1, 0, 0, 0, 0 } ,
  { 0, 0, 0, 1, 0, 0, 0, 0 }   
};

void setup() {
  pinMode(latchPin, OUTPUT);
  pinMode(clockPin, OUTPUT);
  pinMode(dataPin, OUTPUT);
  pinMode(inputPin2, INPUT);
  pinMode(inputPin3, INPUT);
}

void loop() {
  int t;
  valleft = digitalRead (inputPin2);
  valright = digitalRead (inputPin3);

if (valleft == HIGH) {                 //when the left switch is on
    for (t = 0; t < 64; t++) lightLED(left);  //display left signal
    lightLED(LEDs);                      // off
    delay(1000);
  }
  else if (valright == HIGH) {
    for (t = 0; t < 64; t++) lightLED(right);
    lightLED(LEDs);
    delay(1000);
  }
  else
    lightLED(LEDs);
}
void lightLED(int tempLED[8][8]) {          //run the lightLED  bit by bit
  byte columnbitsToSend = 0;
  byte rowbitsToSend = 0;

int x;
  int y;
  for (x = 0; x < 8; x++) {
    columnbitsToSend = 0;
    rowbitsToSend = 0;

for (y = 0; y < 8; y ++) {
      if (tempLED[x][y] == 1) {
        bitWrite(rowbitsToSend, y, HIGH);
      }
    }

digitalWrite(latchPin, LOW);
    bitWrite(columnbitsToSend, x, HIGH);
    shiftOut(dataPin, clockPin, MSBFIRST, columnbitsToSend);
    shiftOut(dataPin, clockPin, MSBFIRST, rowbitsToSend);
    digitalWrite(latchPin, HIGH);   
    delay(2);
  }
}




Didn't notice how badly "Auto Format" had *mangled* the arrays either!


---


And a few *more* bugs - wouldn't compile.

Yup. It is working. :slight_smile:
Thanks a lot. ^^