const int switchPin = 2; // switch is connected to pin 2
const int led1Pin = 12;
const int led2Pin = 11;
const int led3Pin = 10;
const int led4Pin = 9;
const int led5Pin = 8;
int val; // variable for reading the pin status
int val2; // variable for reading the delayed status
int buttonState; // variable to hold the button state
unsigned long previousMillis = 0; // will store last time LED was updated
const long interval = 50; // interval at which to blink (milliseconds)
const long interval2 = 500;
int lightMode = 0; // What mode is the light in?
void setup() {
pinMode(switchPin, INPUT); // Set the switch pin as input
for (int i=8; i<=12; i++){ // Set the leds pin as output
pinMode(i, OUTPUT);
}
Serial.begin(9600); // Set up serial communication at 9600bps
buttonState = digitalRead(switchPin); // read the initial state
// attachInterrupt(digitalPinToInterrupt(switchPin), LightMode, CHANGE);
}
void LightMode(){
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval){
val = digitalRead(switchPin); // read input value and store it in val
//Serial.println("Previo");
//Serial.print(previousMillis);
if (currentMillis - previousMillis >= interval2) {
//previousMillis = currentMillis;
val2 = digitalRead(switchPin); // read the input again to check for bounces
if (val == val2) { // make sure we got 2 consistant readings!
if (val != buttonState) { // the button state has changed!
if (val == LOW) { // check if the button is pressed
if (lightMode <= 3) { // light is off
lightMode+= 1; // implement the lightmode sequence
} else {
lightMode = 0;
}
}
buttonState = val; // save the new state in our variable
}
}
}
}
}
void loop(){
// Now do whatever the lightMode indicates
int i,j;
LightMode();
Serial.print(lightMode);
unsigned long currentMillis = millis();
if (lightMode == 0) {
for (i=8; i<=12; i++){
digitalWrite(i, LOW);
}
}
if (lightMode == 1) {
for (i=8; i<=12; i++){
digitalWrite(i, HIGH);
//delay(interval);
}
}
if (lightMode == 2) {
for (i=8; i<=12; i++){
digitalWrite(i, HIGH);}
Serial.print("valor i ");
Serial.print(i);
// delay(200);
if (currentMillis - previousMillis >= interval2){
for (j=8; j<=12; j++){
digitalWrite(j, LOW);
}
delay(interval);
}
}
if (lightMode == 3) {
for (i=8; i<=12; i++){
digitalWrite(i, HIGH);
delay(200);
digitalWrite(i, LOW);
}
}
if (lightMode == 4) {
for (i=8; i<=12; i++){
digitalWrite(i, HIGH);
delay(200);
}
for (i=12; i>=8; i--){
digitalWrite(i, LOW);
delay(200);
}
}
}
// If lightmode is 0, we dont have to do anything because the LEDs are already off!