LED blink - need help

Hi everyone,

I'm trying to get an LED to blink at a set value, with 3 different states.

I have two switches so when both are pressed, I want the delay value (blink rate) to change.

When one is pressed, I want it to have a different value.

But it isn't working when I press the buttons! Here's the code.


byte switch15Output = 0;
byte switch41Output = 1;
byte switch15Input = 10;
byte switch41Input = 11;
byte redLED = 5;
int val = 1000;

void setup() {
// put your setup code here, to run once:
pinMode(switch15Output, OUTPUT);
pinMode(switch41Output, OUTPUT);
pinMode(switch15Input, INPUT);
pinMode(switch41Input, INPUT);
pinMode(redLED, OUTPUT);

}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite(redLED, HIGH);
delay(val);
digitalWrite(redLED, LOW);
delay(val);
while (switch15Input == HIGH && switch41Input == HIGH){
val = 100;
}
while (switch15Input == HIGH || switch41Input == HIGH){
val = 2000;
}
}

Thanks!

switch15Input == HIGH10 is never going to equal 1.

Try reading the pin

Please remember to use code tags when posting code

ah okay yes I understand! I will try to fix it.

Apologies for that, this is my first time posting here and I don't yet know how to do that. I will find out for my next post. :slight_smile:

Missing digitalRead() calls.

Using if statements makes more sense than using while statements.

Why do you have two pins per switch? One is normally enough.

Why do you have two identical while statements?

Okay thanks for the pointers.

Okay, I used two pins because I thought that you needed an output to provide voltage and an input to see if the switch is pressed? Could you please explain why this is not the case?

The while statements are slightly different. One looks for switch 15 and 41 being pressed and sets the delay value to 100, the other looks for just one and assigns a 2000 delay.

I don't know how you wired this.

Leaving your input pin floating (i.e. no connection whatsoever) is a recipe for disaster as you don't know the state of the pin (it will likely randomly fluctuate between HIGH and LOW). You must always have it pulled one way, so the switch can pull it the other way.

Easiest switch connection: one pin for input, internal pull up resistor activated, switch between the pin and GND. Reading is HIGH when the switch is open (due to the pull up resistor), LOW when the button is pressed (connecting the pin to GND - and you have a small current of <0.5 mA running through it).