Go Down

Topic: code not working, please help! (Read 715 times) previous topic - next topic

big93

rawr, i'm trying to use if statements, and its not working, i looked at my code and it seems to make sense!

i'm using a potentiometer and dividing the total number by 3 to make 3 equal parts of 341, and making 341 mode 1, 682 mode 2 and 1023 mode 3.

but when i tell it to print in mode, it always stays at mode 3!

i used less than , and then i used less than or equal to for 1023, now please tell me why mode is stuck on 3!

p.s, i cant get the else code to work after the if statement, it tells me i need a primary expression before it, and it doesent say what primary expression to use!! w/e for now, heres the messed up code :

Code: [Select]
int value = 0;                          
int ledpin = 9;                          
int pot = 0;
int pot1= 0;
int mode = 0;
void setup()
{
 Serial.begin(9600);
 
 
}

void loop()
{
 if (pot1 < 341);
  mode = 1;


 if (pot1 < 682);
  mode = 2;


 if (pot <= 1023);
  mode = 3;


pot1 = analogRead(1);
pot = analogRead(2) / 4;  
Serial.println(pot1);
 analogWrite(ledpin, pot);

 
}



please tell me why my code is broken, it doesent make sense to me!

thanks in advance,
-big93

westfw

ALL of your if statements get executed.  So you read in 10, and the first if statement says "if (10 < 341) mode = 1", and 10 is less than that, so it sets mode to 1.  Then you get to "if (10 < 682)", and pot1 is still 10, so that's STILL true, and mode gets set to 2.  Then you get to "if (10 < 1023)", and that's true too, so mode gets set to 3.  You also have extra semicolons on the "if" lines, but I assume that's a recent bug, since as written here I would think mode would always be zero.

The easiest fix without adding complexity (like "else") is to simply change the order:

Code: [Select]
if (pot1 < 1023)
 mode = 3;
if (pot1 < 682)
 mode = 2;
if (pot1 < 341)
 mode = 1;

wurx

Westfw-

I instantly noticed that all the "if" statements were executed and were returning a virtual "yes".
Then I thought use "else".... that is the easiest fix.

WRONG!

Your solution is great! I learned something new, thanks!


big93

thank you, ill test the better code now, and what you said about the bug, is not true, i put thiose on cuz i thought they go there, but now i know!!

thanks allot!

big93

#4
Dec 16, 2007, 05:48 pm Last Edit: Dec 16, 2007, 05:56 pm by big93 Reason: 1
....... damnit, code not working again, can you take a look again, i really dont understand why it's acting funny.

ok, i made it so modes are now controlling individual leds, but when i try to dimm and brighten the led in one mode, all the leds brighten and dimm....

Code: [Select]
int value = 0;                          
int ledpin3 = 11;
int ledpin2 = 10;
int ledpin1 = 9;                          
int pot = 0;
int pot1= 0;
int mode = 0;
void setup()
{
 Serial.begin(9600);
 
 
}

void loop()
{
if (pot1 < 1023)
 mode = 3;
if (pot1 < 682)
 mode = 2;
if (pot1 < 341)
 mode = 1;
pot1 = analogRead(1);
pot = analogRead(2) / 4;  
Serial.println(mode);
 if (mode = 3)
  analogWrite(ledpin3, pot);
  else
  analogWrite(ledpin3, 0);
 if (mode = 2)
  analogWrite(ledpin2, pot);
  else
  analogWrite(ledpin2, 0);
 if (mode = 1)
  analogWrite(ledpin1, pot);
  else
  analogWrite(ledpin1, 0);
 
 

 
}


thanks again
-big93

p.s, why does it ask me for a primary expression before the freaken "else" statement sometimes?

Go Up