Pages: [1]   Go Down
Author Topic: error with "if" statment  (Read 914 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry for the newbish question here, but I am new to C programming, as well as the arduino.

So, I am working on trying to get the Night Rider effect with 8 LEDs (dig outs 2-9). I want to control the speed with a POT. Now, I was able to do so, with calling each LED in sequence (2,3,4,5,6,7,8,9,8,7,6,5,4,3 loop). SO, I started trying to clean up the code a bit with a couple nested if statements.
Code:
int sensorPin = A0;
int ledPin = 2;
int sensorValue = 0;
int reverse = 0;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);   

}

void loop() {
  if   (reverse = 0); { // count up
       digitalWrite(ledPin, HIGH);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
      digitalWrite(ledPin, LOW);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
          if (ledPin = 9) {
            reverse = 1;  }
          else ledPin = (ledPin + 1);
  }
[u][b] else // count down[/b][/u]
      digitalWrite(ledPin, HIGH);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
      digitalWrite(ledPin, LOW);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
          if (ledPin = 2) {
            reverse = 0;  }
          else ledPin = (ledPin - 1);
 

However, I am getting
Quote
sketch_jun30b.cpp: In function 'void loop()':
sketch_jun30b:29: error: 'else' without a previous 'if'
with the bolded underlined portion of code being pointed out.  Why isn't that "else" associated with the very 1st "if"?
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7268
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
else // count down[/b][/u]
      digitalWrite(ledPin, HIGH);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
      digitalWrite(ledPin, LOW);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
          if (ledPin = 2) {
            reverse = 0;  }
          else ledPin = (ledPin - 1);

The else is only coupled with digitalWrite(ledPin,HIGH);

Thought you may want to know that. Your first if structure indicates everything after the else should be inside {} but you didn't put them  in.

Plus, all if statements are wrong, at least not giving you what you want, use "==" instead of "=" if you compare values like if (ledPin==2) not if (ledPin=2).

Fix the above possible mistake and see if you get what you want.
Logged


Mountain View, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Remove the semicolon after the "if" condition.

Code:
  if   (reverse = 0); { // count up
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Cool.. I'll give that a go and check it out. Thank you.. I'll update success of failure  smiley
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well, removing the ";" fixed the missing if error, and I changed to the double "=", but it wont move past pin 2. My "count up" is failing.. back to the drawing board.
Logged

Mountain View, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Can you post your code properly formatted ??  Get rid of the annotations in brackets and write the "if/else" conditions in a neater style such as this:

Code:
if (cond) {
  doSomething();
} else {
  doSomethingElse();
}

I have a hard time parsing your code and understanding what it is trying to do.
« Last Edit: June 30, 2011, 10:01:42 pm by montecito » Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Okay.

Code:
int sensorPin = A0;
int ledPin = 2;
int sensorValue = 0;
int reverse = 0;

void setup() {
  pinMode(2, OUTPUT);
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, OUTPUT);
  pinMode(8, OUTPUT);
  pinMode(9, OUTPUT);   

}

void loop() {
  if   (reverse = 0) { // count up
      digitalWrite(ledPin, HIGH);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
      digitalWrite(ledPin, LOW);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
          if (ledPin = 9) {
            reverse = 1; 
          } else {
            ledPin = (ledPin+1);
          }
 
  } else { // count down
      digitalWrite(ledPin, HIGH);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
      digitalWrite(ledPin, LOW);
      sensorValue = analogRead(sensorPin);
      delay(sensorValue);
          if (ledPin = 2) {
            reverse = 0; 
          } else {
            ledPin = (ledPin-1);
          }
 
  }
}
Logged

Mountain View, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This is a problem:

Code:
          if (ledPin = 2) {

Replace with

Code:
          if (ledPin == 2) {


I don't know if that will fix everything but it's an obvious problem.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 30
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, I reverted back trying to debug. As soon as I changed it back to "==", all is working as desired. Thank you all for your help  smiley
Logged

Mountain View, CA
Offline Offline
Newbie
*
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

This too:

Code:
          if (ledPin = 9) {

Comparisons are == not = as the other poster pointed out.  Check all our "if" statements for this error.
Logged

Central MN, USA
Offline Offline
Tesla Member
***
Karma: 75
Posts: 7268
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Yeah, I reverted back trying to debug. As soon as I changed it back to "==", all is working as desired. Thank you all for your help  smiley

There is no reason to revert. The single equal is value assignment, double is comparison. I suggest you learn the operators && and ++. Your code may work but is very bloated without proper use of them.
Logged


Fresno, CA, USA
Offline Offline
Full Member
***
Karma: 1
Posts: 153
Arduino rocks (literally, with a WaveShield!)
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

A little explanation of "=" vs "==" is in order...

"=" means "assign", as in "a=2" -> "set a to 2". This compiles properly, because "a=2" also passes-through the value "2", so things like "a = b = c = d = 2" are possible, setting a, b, c, and d to "2" by referencing each other.

"==" means "check if equal". It returns "1" if they are the same, and "0" if they aren't the same. Basically.

So if you want to check if two things are the same, use "==". And if you want to "assign and check" - like, "if (x = buttonCheck())", which would mean that "x" is set to the value returned by buttonCheck() and the loop would run if the value is non-zero (e.g. the function returns 0 if nothing is pressed, otherwise it returns the button that was pressed), but not have to run buttonCheck() a second time.
Logged

Pages: [1]   Go Up
Jump to: