Go Down

Topic: if/else question (Read 692 times) previous topic - next topic

Haavard GJ

Hi
i have a web server on my arduino duemilanove, and with this code, i get printed both "feedback high" and "feedback error"
Code: [Select]
 
  feedbackin = digitalRead(feedback);
 
  if (feedbackin == HIGH){
    client.println ("<font color='green' size='5'>Feedback high</font>");
  }
 
  if (feedbackin == LOW) {
    client.println ("<font color='grey' size='5'>Feedback low</font>");
  }
 
  else {client.println ("<font color='red' size='5'>Feedback ERROR</font>");
  Serial.println ("FEEDBACK ERROR");
  Serial.println (feedbackin);
  Serial.println ("");
  }

gives output
"Feedback high Feedback ERROR"

anyone that have a clue on why this happen?

Grumpy_Mike

Yes it is working exactly as you wrote it.
If it is high then the first if gets true and the second is false so the else kicks in.
Therefore you get both high and error.

wildbill

Feedbackin is evidently high, so your first if prints Feedback high

In your second if, Feedbackin isn't low, so it executes the else part, emitting Feedback Error

perhaps you wanted
Code: [Select]
else if (feedbackin == LOW) {

But since digitalRead will only ever give you high or low, I'm struggling to see what the else clause is really for.

Haavard GJ

aha... i did put in the error part to see if the input was floating... because i was experimenting with inputs...

thanks for the help  8)

WizenedEE

digitalRead will always return either HIGH or LOW (look at the source code). If the pin is left floating, it will return a random value.

Go Up