Problem with VERY simple sketch (RESOLVED)

Hello.

I'm fairly proficient with Arduino, but this sketch truly blows my mind. It's a very simple sketch that uses millis() and 3 if() statements. The thing that's driving me nuts is the output in the serial monitor. As soon as the sketch starts, the monitor is saying that x = 2 and red = 3000 even though those values should be x = 1 and red = 0 until time= 5000. The first 3 values in the serial monitor should be ( 0, 1, 0 ) until time = 5000 but I'm getting ( 0, 2, 3000). Can someone please explain why this is happening?

int red;
unsigned long time; 
int x;

void setup()  
{  
        Serial.begin(9600);       
}  
  
void loop()  
{  
   time = millis();       
   
   if (time < 5000) {
      x = 1; }
      
   if (time >= 5000) {
      x = 2; }  

   if (x = 1) { 
      red = 0; }
      
   if (x = 2) { 
      red = 3000; }

        Serial.println(time);
        Serial.println(x);
        Serial.println(red);

        delay(500);
        
}
if (x = 2)

Back to C 101 for you.

AWOL ,

Can you please explain what you mean? I have if (x = 2) in the code

He means:

if (x = 2)

should be:

if (x == 2)

http://arduino.cc/en/Reference/If

WOW :astonished:. That is an incredibly stupid mistake. I am truly embarrassed.
Thank you for pointing that out, luisilva.

The whole sketch can be:

int red;
unsigned long time;
int x;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  time = millis();

  if (time < 5000) {
    x = 1;
  }
  else {
    x = 2;
  }

  if (x == 1) {
    red = 0;
  }
  else if (x == 2) {
    red = 3000;
  }

  Serial.println(time);
  Serial.println(x);
  Serial.println(red);

  delay(500);

}

Look too to the way that the code is wrote (mostly where the text start in each line). You can do this autocratically chosing the option auto-format from the menu tools from de Arduino IDE.

EDIT:

That is an incredibly stupid mistake. I am truly embarrassed.

That's why AWOL answer like he did. But in my opinion everyone need to start some day, and everyone can make "stupid" mistakes. So don't worry .

read up on blink without delay.

you will wind up needed that sooner than later.

luisilva,

I originally had if/else statements in the sketch, but I removed them because I was getting frustrated. This sketch used to be fairly complicated, but I just kept stripping it down to find the bug. I should use the auto format feature more often. I think I just prefer my own way of formatting for some reason. Like I said, this was an incredibly stupid mistake. Thank you so much for finding pointing out the issue.

If you get used to write the if's like this:

  if (5000 > time) {
    x = 1;
  }
  else {
    x = 2;
  }

  if (1 == x) {
    red = 0;
  }
  else if (2 == x) {
    red = 3000;
  }

You will NEVER do the same mistake, because it you write:

(...)
  else if (2 = x) {
    red = 3000;
  }

the compiler will give you an error message like:

sketch_sep06b.ino: In function ‘void loop()’:
sketch_sep06b:24: error: lvalue required as left operand of assignment

and then you only need to go to the line 24 and see what is wrong.

dave-in-nj,

The most embarrassing this about this is that I fully understand create delays without the delay() function. This was just a stupid mistake. This is an example of code I've written. It isn't beautiful or refined, but it works.

int potPin = 2;

long pm = 0;
long pm1 = 0;
long interval, interval1, L1;
unsigned long cm, cm1;

int val, b, b1, c, A, B;
float a, a1;
int Q = 1;
int Q1 = 1;
int x = 1;
int x1 = 1;

#include "Tlc5940.h"

void setup()  
{  
        Tlc.init();
        Serial.begin(9600);
}  
  
void loop()  
{  
     Tlc.clear();   
  
     interval = analogRead(potPin);
        
     cm = millis();
     if (cm - pm > interval) {
     pm = cm;  
     
     if (Q == 1) {
     a = 1.1;
     b = 1;
     x = ((x * a)+1);
     if (x == 4440) Q = 2;
     }
     
     if (Q == 2) {
     a = 0.9091;
     b = -1;  
     x = ((x * a)+b);
     if (x < 2) {
     Q = 1;
     L1 = millis(); }
   
     }
     
     int y = map(x, 0, 4036, 4036, 0);
     
     
     Tlc.set(3, x*.4);
     Tlc.set(0, y*.4);
     
    Serial.println(x);
     //Serial.println(y);
     

     Tlc.update();

luisilva:
If you get used to write the if's like this:

  if (5000 > time) {

x = 1;
  }
  else {
    x = 2;
  }

if (1 == x) {
    red = 0;
  }
  else if (2 == x) {
    red = 3000;
  }




You will NEVER do the same mistake, because it you write:


(...)
  else if (2 = x) {
    red = 3000;
  }




the compiler will give you an error message like:


sketch_sep06b.ino: In function ‘void loop()’:
sketch_sep06b:24: error: lvalue required as left operand of assignment



and then you only need to go to the line 24 and see what is wrong.

Sadly, you'll still trip up on comparing two variables.
Just get used to thinking "two equals here"

AWOL:
Sadly, you'll still trip up on comparing two variables.
Just get used to thinking "two equals here"

Yes, is true!

luisilva,
I didn't know using else if() would give you a complier error if you use on = sign. That's very nice to know.

AWOL,
I can't believe I forgot the two = signs, but I guess crap like that happens once in a while. I'm slightly hung over so that might be part of the problem :stuck_out_tongue:

Is not the if else, is the

 if (NUMBER = variable)

that gives you the error.

luisilva,
OH. I didn't catch that! Yikes! Yeah it should be x == 2
Thanks for pointing that out. I'm really not thinking clearly today.

I appreciate your help, guys. Take care