Go Down

Topic: Trying to spot what's wrong in the loop (Read 907 times) previous topic - next topic

krow

Hey guys,

I'm pulling my hair out with this issue, it seems simple to solve but I can't spot what's wrong. My code activates an alarm and starts looping a signal from a distance sensor, which is then deactivated when a user enters a password (keypad to be included in the future, for now I'm just using my computer keyboard). Once deactivated, my Arduino waits for the user to reactivate the alarm by entering the password again.

The code I'm attaching is the bit I'm having trouble with, even though it is not the entire code, the bit you'll read is functional (with the problem I'm getting though).

The problem is in the 'if' statements. Basically, I'm able to deactivate the alarm but then when the program reads 'waitToTurnOn()' for some reason the program goes to Serial.println("WRONG PASS") which it shouldn't. It should wait for the user to enter the password, it can't print WRONG PASS without having entering anything. I guess the issue has to do with Serial.available and Serial.read() ? I can't get it to work the way I need, any help/suggestions will be appreciated.

Code: [Select]


boolean onState = true;

void setup()
{
  Serial.begin(9600);
  Serial.println("Alarm turned ON");
  pinMode(13,OUTPUT); 
}

void loop()
{
  if(onState == true){
   
    //Code for distance sensor...
    //.
    //.
    //.
    waitToTurnOff();
  }
  else {
    waitToTurnOn(); 
  } 
}

void waitToTurnOff()
{
  if( Serial.available() ){ 
      char passToTurnOff = Serial.read();
   
      if(passToTurnOff== '1'){                   
        Serial.println("Turned OFF!");
        onState = false;
      }else{
         Serial.println("WRONG PASSSSSSSSS");
      }
    }
}

void waitToTurnOn(){
  if(Serial.available()){
    char passToTurnOn = Serial.read();

    if(passToTurnOn == '2'){
        Serial.println("Turned ON!");
        onState = true;
    }else{
       Serial.println("WRONG PASS");
    }
  } 
}


gardner

For debugging purposes, why not echo what you read in, just to make sure you're reading what you think you're reading?
Is it possible your terminal emulator is sending a '\r' or '\n' that is confusing things?

oric_dan

I would try 2 things. First, use an int with values 1,0 instead of a boolean for onState.
Secondly, change the char passToTurnOff,On "declarations" to int, and move out
of the if..then constructs.

krow

Quote

For debugging purposes, why not echo what you read in, just to make sure you're reading what you think you're reading?
Is it possible your terminal emulator is sending a '\r' or '\n' that is confusing things?


Hello Gardner, thanks for your reply. Yes I echoed what I read in and I actually get what I was expecting.Why do you think my terminal emulator is sending a '\r' or '\n' ? why can that happen?

Quote

I would try 2 things. First, use an int with values 1,0 instead of a boolean for onState.
Secondly, change the char passToTurnOff,On "declarations" to int, and move out
of the if..then constructs.


Thanks Oric_dan, I'll try your suggestions when I get home. I have a question though, why would 1,0 yield a different result from my boolean variable? why do you think that can influence the result? I thought they were (pretty much) the same.

AWOL

Quote
Why do you think my terminal emulator is sending a '\r' or '\n' ? why can that happen?
Because that is the normal behaviour of a terminal emulator.
"carriage-return" and "line-feed" are the end of line markers from the days of teletypes.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Go Up