Go Down

Topic: If/Else and Switch Issue (Read 67 times) previous topic - next topic

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:
Code: [Select]

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:
Code: [Select]

  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

Code: [Select]

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

AWOL

Code: [Select]
if (bPinLevel = false)
Oops
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

UKHeliBob

No break; after each case ?
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

No break; after each case ?
Thank you HeliBob - I figured it had to be something silly.  I even read the Reference but mistakenly assumed that was what the example wanted to have happen.

Thanks!

Code: [Select]
if (bPinLevel = false)
Oops
While I appreciate the response, could you please be a bit more explicit? 

Is there a syntactical issue here of which I'm not aware?  It seems to be correct per the reference page.

Is there some logical concern here that you are alluding to?  The only difference that if clause should make is whether it holds true for the first time as the code is initially executed or on the second time through - that is why the delay value swap with the if clause.

Doug

AWOL

Syntactally, it is correct, if you wanted an assignment.
Normally, the presence of the "if" implies you want a comparison.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

#6
Mar 01, 2015, 09:48 pm Last Edit: Mar 01, 2015, 09:50 pm by DouglasABaker
Got it - I missed an equal sign.

d-

AWOL

Quote
The syntax on the reference page is:
But can you point me to where it says
Code: [Select]
if (x = y) do something(); ?

No, you can't.
Syntactically, there's nothing wrong with it, which is why the compiler didn't complain, but a comparison uses ==, whereas an assignment uses =.

It's a really important distinction in C programming.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

But can you point me to where it says
Code: [Select]
if (x = y) do something(); ?

No, you can't.
Syntactically, there's nothing wrong with it, which is why the compiler didn't complain, but a comparison uses ==, whereas an assignment uses =.

It's a really important distinction in C programming.
You're too speedy - I found my error right after I posted my reply, and thus revised it.  You are correct, of course, which is why I revised my reply hoping you wouldn't have gotten to it yet.  Guess I didn't make it.

Thank you for your help!

Doug

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy