I am new to arduino (and to programming), so I have a very basic question. I have searched the web for solutions, but nothing really answered my question.
I'm working on a light system for an RC drift car. I want to blink 10 different leds, but in more different ways. I'm using a simple button to surf between the different led blinking modes. My problem is, that I'm using the delay() function to delay the blinking time. This stops my code and stops reading the button. I could probably solve it like it is in the "Blink without Delay" example, but I have to toggle 10 different leds in 10 different ways and that would make my code very complicated and very long. I have already written some of it, but I'm stuck now.
const int buttonPin = 2; //pushbutton
const int L1 = 3; //led 1
const int L2 = 4; //led 2
const int L3 = 5;
const int L4 = 6;
const int L5 = 7;
const int L6 = 8;
const int L7 = 9;
const int L8 = 10;
const int L9 = 11;
const int L10 = 12;
int testL = 13; //only for debugging, will be deleted
int reading;
int state;
int lastButtonState = HIGH;
//for blink mode2
int time = 100;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); //set button as input
for (int i=3; i<=12; i++) //set leds as outputs
pinMode(i, OUTPUT);
pinMode(Lpin, OUTPUT); //only for debugging, will be deleted
Serial.begin(9600); //only for debugging, will be deleted
}
void loop() {
reading = digitalRead(buttonPin);
if (reading == LOW && reading != lastButtonState) {
state++;
if (state >= 9)
state = 0;
//state changes between 0 and 9 if button is pressed
//these will be the different blinking modes
}
lastButtonState = reading;
Serial.println(state); //to know which mode is active
switch (state){ //the different modes:
case 0: //all leds off
for (int i=3; i<=12; i++)
digitalWrite(i, LOW);
break;
case 1: //double blink for all leds with 70 millis delay
for (int i=3; i<=12; i++)
digitalWrite(i, HIGH);
delay(70);
for (int i=3; i<=12; i++)
digitalWrite(i, LOW);
delay(70);
break;
case 2: //blink mode 2
digitalWrite(L1, HIGH);
digitalWrite(L4, HIGH);
digitalWrite(L7, HIGH);
digitalWrite(L10, HIGH);
digitalWrite(L5, HIGH);
digitalWrite(L6, HIGH);
delay(time);
digitalWrite(L1, LOW);
digitalWrite(L4, LOW);
digitalWrite(L7, LOW);
digitalWrite(L10, LOW);
digitalWrite(L5, LOW);
digitalWrite(L6, LOW);
delay(time);
digitalWrite(L1, HIGH);
digitalWrite(L4, HIGH);
digitalWrite(L7, HIGH);
digitalWrite(L10, HIGH);
digitalWrite(L5, HIGH);
digitalWrite(L6, HIGH);
delay(time);
digitalWrite(L1, LOW);
digitalWrite(L4, LOW);
digitalWrite(L7, LOW);
digitalWrite(L10, LOW);
digitalWrite(L5, LOW);
digitalWrite(L6, LOW);
delay(time);
digitalWrite(L2, HIGH);
digitalWrite(L3, HIGH);
digitalWrite(L8, HIGH);
digitalWrite(L9, HIGH);
delay(time);
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);
digitalWrite(L8, LOW);
digitalWrite(L9, LOW);
delay(time);
digitalWrite(L2, HIGH);
digitalWrite(L3, HIGH);
digitalWrite(L8, HIGH);
digitalWrite(L9, HIGH);
delay(time);
digitalWrite(L2, LOW);
digitalWrite(L3, LOW);
digitalWrite(L8, LOW);
digitalWrite(L9, LOW);
delay(time);
break;
case 3: //blink mode 3
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
case 8:
break;
case 9:
break;
}
}
So my question is: How can I read the button constantly without having to write hundreds of pages of code?