Go Down

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

PeterH


I echoed what I read in and I actually get what I was expecting.


I don't believe you.

You have already told us that you are getting a spurious input which fails your 'password' check. The debug should have told you what the spurious input was. If it only shows what you expected then the problem you described cannot occur.
I only provide help via the forum - please do not contact me for private consultancy.

tuxduino

Are you using the ide-provided terminal emulator ? Try various end-of-line options.
Also, print what you Serial.read() with println(ch, HEX) otherwise \r and \n characters will go unnoticed.

Code: [Select]
void setup() {
    Serial.begin(115200);

}

unsigned int cnt = 0;

void loop() {
    char ch;
   
    if (Serial.available() > 0) {
        cnt++;
        ch = Serial.read();
        Serial.print(cnt);
        Serial.print(": ");
        Serial.println(ch, HEX);
    }
}


Here's what I get if I send 1 when "both cr and nl" option is enabled:

Code: [Select]

1: 31
2: D
3: A

HazardsMind

#7
Jan 07, 2013, 02:12 pm Last Edit: Jan 08, 2013, 06:47 pm by HazardsMind Reason: 1
Im guessing your trying to read in "1234" and get out "1234" correct? If so that is actually easy. What you need to do is read the characters in and store them in an array, then use "Blah_Blah = atoi(Data);" this convert the array data to an integer.

example:

Code: [Select]

char Data[8]; // however long it needs to be, GLOBAL VARIABLE
int currentCommand = 0; //GLOBAL VARIABLE

void setup() {...........}

void loop{
if (Serial.available() > 0) {
char ch = Serial.read();
if (ch != '\c' || ch != '\r' || ch != '\n') // It's looking for just the numbers,
  {                                         // and if it see one of these, it will print out the numbers
    Data[currentCommand++] = ch;
   //Serial.print(Data[currentCommand]);
  }
else {
    int MyData = atoi(Data); // converts the chars into ints
    Serial.println(MyData); // shows you what you put in
    while(currentCommand !=0){   // This can be used for any array size, LEAVE THIS IN
    Data[currentCommand--] = 0; } //clear for new data   LEAVE THIS IN
    MyData = 0; // clears it after use
    }
  }
}

Not compiled! it's really just to give you an idea.
Keep in mind it might have bracket " { } " errors or some other kind of errors i've overlooked. So you may need to tweak it a bit.

For when you incorporate the keypad, look at this http://arduino.cc/forum/index.php/topic,137352.0.html
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

krow

Guys, thank you all for your help, I found the problem.

Based on what Garner said about the escape characters, I decided to view the output with Putty instead of the Arduino IDE serial monitor to see if I still had the same result, it turns out that in Putty my program works fine.

As opposed to the serial monitor in the Arduino IDE, when I use Putty and I enter # 1 to deactivate the alarm (I chose 1 for the sake of simplicity), there's no need to hit 'Enter' to send the actual key, the alarm is deactivated as soon as I hit 1. Because in the serial monitor of the Arduino IDE I hit two keys (1 and then Enter) my program reads the two keys one after the other, that's why every time I send a 1 and it goes to the next function I get the  WRONG PASS message, basically what the program is reading is the Enter key which is the second key I press.

Hopefully this will help someone else in the future, I hope it was clear! cheers.

krow

Code: [Select]

Guys, thank you all for your help, I found the problem.

Based on what Garner said about the escape characters, I decided to view the output with Putty instead of the Arduino IDE serial monitor to see if I still had the same result, it turns out that in Putty my program works fine.

As opposed to the serial monitor in the Arduino IDE, when I use Putty and I enter # 1 to deactivate the alarm (I chose 1 for the sake of simplicity), there's no need to hit 'Enter' to send the actual key, the alarm is deactivated as soon as I hit 1. Because in the serial monitor of the Arduino IDE I hit two keys (1 and then Enter) my program reads the two keys one after the other, that's why every time I send a 1 and it goes to the next function I get the  WRONG PASS message, basically what the program is reading is the Enter key which is the second key I press.

Hopefully this will help someone else in the future, I hope it was clear! cheers.


I guess the way to avoid this issue with the Arduion IDE serial monitor is by writing a program that ignores the Enter key after entering my password? it's still more comfortable for me to use that monitor than Putty.

Go Up