Hi all,
I'm brand new to Arduino, but not to programming in general, and I'm baffled by the results I'm getting with what should be a very simple program.
My goal is to turn my Arduino One built in LED on and off. I want the interval to be 2.5 seconds on, 2 seconds off for purposes of this example.
I started with this code:
int intLEDpin = 13; //Specify LED to switch
boolean bPinLevel = true; //Start with the LED on
void setup()
{
// put your setup code here, to run once:
pinMode(intLEDpin,OUTPUT);
}
void loop()
{
digitalWrite (intLEDpin, bPinLevel);
if (bPinLevel = true)
{
delay (2500);
}
else
{
delay (2000);
}
bPinLevel = !bPinLevel;
}
This resulted in the LED turning on for 2.5 seconds, then turning off permanently.
When I modified the if statement to:
if (bPinLevel = false)
{
delay (2000);
}
else
{
delay (2500);
}
the LED turned on and stayed on.
I assumed maybe there was something strange about if statements that I didn't understand, so I decided to try using switch case instead
int intLEDpin = 13; //Specify LED to switch
boolean bPinLevel = true; //Start with the LED on
void setup()
{
// put your setup code here, to run once:
pinMode(intLEDpin,OUTPUT);
}
void loop()
{
switch (bPinLevel)
{
case false:
digitalWrite (intLEDpin, LOW);
delay(2000);
case true:
digitalWrite (intLEDpin, HIGH);
delay(2500);
}
bPinLevel = !bPinLevel;
}
The results of this should be a repeating pattern of 2.5 seconds ON, 2 seconds OFF. It would enter loop() with bPinLevel = true, go to that case, turn the LED on, wait 2.5 seconds, exit the case, flip the bPinLevel to false, and start over.
What I actually happens is that the LED turns on for 2.5 seconds, off for 2 seconds, and then repeats on for 5 seconds, off for 2 seconds. If I change the case true: to delay(5000) then the LED turns on for 5 seconds the first time, and 10 seconds every time after that.
All insight welcome.
Doug