Hi guys,
I'm trying to get this if else loop to assign an interger (0-7) to 'val2 ' depending on 'val' read from the analog input pin, which is connected to resisters which can be shorted by switches. The led simply turns on if any of the switches are operated.
Problem is:
if val>1020, val2=0 which is correct
if 1000<val<=1020 val2=1 which is correct
if val is in anything else val2=1 I need val2 to have the appropriate integer assigned with respect to val
So the program seems unable to move past the if loop (1000<val<=1020)
What am I doing wrong?
Thanks
int sensor = 0;
int ledPin = 5;
int val2;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
int val = analogRead(sensor);
Serial.println(val);
delay(100);
if (val<1020) {digitalWrite(ledPin, HIGH);
}
else {digitalWrite(ledPin, LOW);
}
if (val>1020){
val2=0;
}
else if (1000<val<=1020){
val2=1;
}
else if (910<=val<=1000){
val2=2;
}
else if (898<val<910){
val2=3;
}
else if (810<=val<=898){
val2=4;
}
else if (800<val<810){
val2=5;
}
else if(740<val<=800){
val2=6;
}
else {
val2=7;
}
else if (1000<val<=1020){
Won't work. try,
if(val > 1000 && val <= 1020)
Same for other like comparisons.
...or
if (1000 < val && val <= 1020) {
}
Your IDE has Example Sketches built right in and you want to work your way through section 05.Control.
Start in the IDE by getting the File dropdown menu and click Examples->05.Control and start with the first and work your way through until you understand what each part does because so far, you don't and seeing working code should help you the most.
HOWEVER, you can line by line explanations of your Examples sketches through this page:
You really need to go through section 5 if you can.
Try this (and see my comments):
int sensor = 0;
int ledPin = 5;
int val2;
void setup(){
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
int val = analogRead(sensor);
Serial.println(val);
delay(100);
if (val<1020) {
digitalWrite(ledPin, HIGH);
}
else {
digitalWrite(ledPin, LOW);
}
if (val>1020){
val2=0;
}
else if (1000<val){ // will only reach here if val <= 1020 so no need to test
val2=1;
}
else if (910<=val){ // will only reach here if val <= 1000 so no need to test
val2=2;
}
else if (898<val){ // will only reach here if val < 910 so no need to test
val2=3;
}
else if (810<=val){ // will only reach here if val <= 898 so no need to test
val2=4;
}
else if (800<val){ // will only reach here if val < 810 so no need to test
val2=5;
}
else if(740<val){ // will only reach here if val <= 800 so no need to test
val2=6;
}
else { // will only reach here if val <= 740
val2=7;
}
} // Don't forget the closing brace for loop()...
Also, if ... else isn't a loop, it's a conditional control structure. Please watch your terminology to keep confusion down.
Thanks guys
So basically its my boolean expression.
Thanks again
SmartElectro:
Thanks guys
So basically its my boolean expression.
Thanks again
Code-wise, your syntax and structure are off in very beginner ways. I know, I started out once too.
Really, you will benefit from looking through working code on your Arduino with the Arduino site tutorial full lesson open in a browser it would be hard to not learn more than you show.
You can learn just by loading, running and going though the Arduino site tutorial, do section 05.Control.
The lessons are already there, tested and working.