Offline
Newbie
Karma: 0
Posts: 30
|
 |
« on: June 30, 2011, 09:41:00 pm » |
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. 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 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
Faraday Member
Karma: 35
Posts: 5935
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #1 on: June 30, 2011, 09:48:44 pm » |
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
Newbie
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
|
 |
« Reply #2 on: June 30, 2011, 09:49:59 pm » |
Remove the semicolon after the "if" condition. if (reverse = 0); { // count up
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #3 on: June 30, 2011, 09:51:17 pm » |
Cool.. I'll give that a go and check it out. Thank you.. I'll update success of failure 
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #4 on: June 30, 2011, 09:56:53 pm » |
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
Newbie
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
|
 |
« Reply #5 on: June 30, 2011, 10:00:10 pm » |
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: 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
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #6 on: June 30, 2011, 10:04:34 pm » |
Okay. 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
Newbie
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
|
 |
« Reply #7 on: June 30, 2011, 10:08:42 pm » |
This is a problem: if (ledPin = 2) {
Replace with if (ledPin == 2) {
I don't know if that will fix everything but it's an obvious problem.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 30
|
 |
« Reply #8 on: June 30, 2011, 10:09:59 pm » |
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 
|
|
|
|
|
Logged
|
|
|
|
|
Mountain View, CA
Offline
Newbie
Karma: 0
Posts: 20
Professional C/C++ programmer available for projects
|
 |
« Reply #9 on: June 30, 2011, 10:11:09 pm » |
This too: 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
Faraday Member
Karma: 35
Posts: 5935
Phi_prompt, phi_interfaces, phi-2 shields, phi-panels
|
 |
« Reply #10 on: June 30, 2011, 10:31:24 pm » |
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  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
Full Member
Karma: 1
Posts: 153
Arduino rocks (literally, with a WaveShield!)
|
 |
« Reply #11 on: June 30, 2011, 11:20:40 pm » |
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
|
|
|
|
|
|