Pages: [1]   Go Down
Author Topic: 8x8 Led matrix turn signal control by switches  (Read 1035 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
 
Code:
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)
« Last Edit: April 25, 2014, 01:34:11 pm by CrossRoads » Logged

Global Moderator
Boston area, metrowest
Offline Offline
Brattain Member
*****
Karma: 533
Posts: 26941
Author of "Arduino for Teens". Available for Design & Build services. Now with Unlimited Eagle board sizes!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Designing & building electrical circuits for over 25 years. Check out the ATMega1284P based Bobuino and other '328P & '1284P creations & offerings at  www.crossroadsfencing.com/BobuinoRev17.
Arduino for Teens available at Amazon.com.

NSW Australia
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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);
  }

}
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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
Logged

NSW Australia
Offline Offline
Faraday Member
**
Karma: 84
Posts: 3433
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin

This should work now - I think!

Code:
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.
« Last Edit: May 04, 2014, 05:38:08 pm by Paul__B » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 11
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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-grin

This should work now - I think!

Code:
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.  smiley
Thanks a lot. ^^
Logged

Pages: [1]   Go Up
Jump to: