i have been working on a project for a friend which has 2 banks of leds that flash when the one of the 2 buttons is pressed as we are building custom indicators ive got the left and right working but i need them to all flash when both buttons are pressed. the code im using at the moment flashs left and then right and then all and repeats when the 2 bottons are pressed but all i want is then all flash together. =(
Now you have the sequence for when the buttons are pressed individually, fine, now make it so they do something else when ButtonL_State && ButtonR_State are both HIGH
When you find yourself using the same code over and over with minor variations it is time to use a function and/or a loop:
//Pin connected to latch pin (ST_CP) of 74HC595
const int latchPin = 8;
//Pin connected to clock pin (SH_CP) of 74HC595
const int clockPin = 12;
////Pin connected to Data in (DS) of 74HC595
const int dataPin = 11;
const int buttonL =2;
const int buttonR =3;
void setup() {
//set pins to output because they are addressed in the main loop
pinMode(latchPin, OUTPUT);
pinMode(dataPin, OUTPUT);
pinMode(clockPin, OUTPUT);
pinMode(buttonL, INPUT);
pinMode(buttonR, INPUT);
}
void loop() {
// If either button is pressed, start a cycle
if (digitalRead(buttonL) || digitalRead(buttonR)) {
// Light the 16 LEDs in sequence
for (int i=0; i<16; i++) {
unsigned int leds = (1UL<<(i+1)) - 1; // 1,3,7,15...65535
// Only light the side whose button is pressed
unsigned int rightLEDs = digitalRead(buttonR) ? leds : 0;
unsigned int leftLEDs = digitalRead(buttonL) ? leds : 0;
WriteLEDs(rightLEDs, leftLEDs);
// If buttons are both released, stop the sequence
if (rightLEDs == 0 && leftLEDs == 0)
break;
delay(30);
}
delay(170); // Extra delay at fully lit
}
else {
WriteLEDs(0, 0);
}
}
void WriteLEDs(unsigned int rightLEDs, unsigned int leftLEDs) {
digitalWrite(latchPin,LOW);
shiftOut(dataPin, clockPin, MSBFIRST, rightLEDs >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, rightLEDs & 0xFF);
shiftOut(dataPin, clockPin, MSBFIRST, leftLEDs >> 8);
shiftOut(dataPin, clockPin, MSBFIRST, leftLEDs & 0xFF);
digitalWrite(latchPin, HIGH);
}
got it working i will post the code it will be getting a lot of tweeking when it in the car XD the penny drop like 2 minutes after i posted i used Boolean Operators it get it to work not bad as ive only gust started coding still a noob at it