Hello, I want to use three push buttons with one led, where the first button turns the led on and off, the second button makes the led blink with a delay of 200 miliseconds and the third button makes the led blinks with a delay of 300 miliseconds. I want the first button to be able to turn on the led with one push and it stays that way untill i press it again where it turns off again. The second and the third buttons should only work while the led is on, so you shouldn't be able to turn on the led with no other button execpt with the first one.
I can get the first button to work as i want it to, but then the second and third button don't work. And i can get the second and third button to work but then the first button don't work.
Here is the code im trying to get to work:
//Pins
const int ledPin = 5;
const int buttonPin1 = 6;
const int buttonPin2 = 7;
const int buttonPin3 = 8;
const int intervalStep = 100; // Sets the reduction
const int intervalDefault = 0; // Sets the defualt blink interval
int interval = intervalDefault; // Sets the interval between blinks
unsigned long previousMillis = 0; // will store last time LED was updated
int i = 0;
int j = 0;
int stateNow = 0;
int stateBefore = 0;
//States
//int buttonState1;
//int buttonState2;
//int buttonState3;
int ledState = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(ledPin, OUTPUT);
pinMode(buttonPin1, INPUT_PULLUP);
pinMode(buttonPin2, INPUT_PULLUP);
pinMode(buttonPin3, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
// put your main code here, to run repeatedly:
int buttonState1 = digitalRead(buttonPin1);
int buttonState2 = digitalRead(buttonPin2);
int buttonState3 = digitalRead(buttonPin3);
unsigned long currentMillis = millis();
stateNow = digitalRead(buttonPin1);
if (stateNow != stateBefore) {
if (stateNow == HIGH and i == 0) {
digitalWrite (ledPin, HIGH);
j=1;
}
else if (stateNow == LOW and j==1) {
i=1;
}
else if (stateNow == HIGH and i == 1) {
digitalWrite (ledPin, LOW);
j=0;
}
else if (stateNow == LOW and j == 0) {
i=0;
}
else if (buttonState2 == 0) {
delay(200);
interval = 100;
digitalWrite(ledPin, LOW);
}
else if (buttonState3 == 0) {
delay(200);
interval = 300;
digitalWrite(ledPin, ledState);
}
if (currentMillis - previousMillis >= interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
// if the LED is off turn it on and vice-versa:
ledState = !ledState;
// set the LED with the ledState of the variable:
digitalWrite(ledPin, ledState);
}
}
stateBefore = stateNow;
//if(buttonState2 == 0){
//delay(200);
//interval = 100;
//digitalWrite(ledPin, LOW);
//}
//if(buttonState3 == 0){
//delay(200);
//interval = 300;
//digitalWrite(ledPin, LOW);
//}
//}
}