Disclaimer: I'm kind of new.
Attached is my code. My goal is to get numerical input from an IR remote and store a 10 digit code as latstr and another as longstr. This is part of a larger project. My IR works great and stores latstr perfectly if I do a serial.print(latstr) right after the first while statement. However, when it moves to the next part of the program and stores longstr, if I view the latstr variable, it contains the longstr as well and I can't figure out why. Also longstr seems to be adding a few random characters at the end and I don't know why.
Exanple:
Start program.
Press 1234567890 for LAT input on ir keypad
Press 5554443332 for LONG input on ir keypad
Output of serial.println(latstr) = 1234567890 after first while statement
Output of longstr = null after first while statement (obviously)
Output of latstr = 12345678905554443332 after second statement
Output of longstr = 5554443332 and some weird characters after 2nd while statement (obviously)
2 issues. Why is it adding on to latstr when I didn't even mention it in the second while statement? And why is it tacking on weird characters at the end of longstr?
Code:
#include <IRremote.h>
//setup variables
const int irPin = 11; //IR in pin
IRrecv irrecv (irPin); //Create IR receiving
decode_results results; //object on irPin
char irnum[12] = {'.', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'f'};
char latstr[10]; //latitude string location - format ##.######f
char longstr[10]; //longitude string location - format ##.######f
char lcdlat[6] = "Lati:";
char lcdlong[6] = "Long:";
char lcdmsg[16];
int oldVal = 0; //These two variables store
int val = 0; //numbers decoded from the remote
int irnumCodes[12] = {
16728765,
16730805, //Values for certain
16738455, //keys that are assigned
16750695, //to certain letters.
16756815, //Replace these with
16724175, //values from your remote.
16718055,
16743045,
16716015,
16726215,
16734885,
16732845,
};
void setup () {
Serial.begin (9600); // debugger
irrecv.enableIRIn (); //enable decoding
}
void loop () {
Serial.println("loopstart");
int y = 0; //lat increment
int x = 0; //long increment
Serial.println("intxy set");
while (y <= 9)//input Lat
{
if (irrecv.decode (&results)) { //check if IR data is received
val = results.value; //If so, set val to decoded data
irrecv.resume (); //keep receiving
if (val != oldVal) { //make sure a single key is not being held down
for (int i = 0; i <= 12; i++) {
if (val == irnumCodes[i]) { //check if val is equal to the code for a certain character
Serial.print (irnum[i]); //If so, print out the appropriate character to the Serial Monitor
latstr[y] = irnum[i];
y++;
}
}
}
oldVal = val;
}
}Serial.println("endwhile");
while (x <= 9) // input Long
{
if (irrecv.decode (&results)) { //check if IR data is received
val = results.value; //If so, set val to decoded data
irrecv.resume (); //keep receiving
if (val != oldVal) { //make sure a single key is not being held down
for (int i = 0; i <= 12; i++) {
if (val == irnumCodes[i]) { //check if val is equal to the code for a certain character
Serial.print (irnum[i]); //If so, print out the appropriate character to the Serial Monitor
longstr[x] = irnum[i];
x++;
}Serial.println(irnum);
}
}
oldVal = val;
}
}
Serial.println("endwhile2");
//outputs 16 characters to lcd
//lcd.clear(); //clears lcd
Serial.println("."); //debug only
Serial.println("."); //debug only
Serial.println("6char lcdlat");
Serial.println(lcdlat); //prints 6chars
Serial.println("."); //debug only
Serial.println("10char latstr");
Serial.println(latstr); //prints 10chars
Serial.println("."); //debug only
Serial.println("6char lcdlong");
Serial.println(lcdlong); //prints 6chars
Serial.println("."); //debug only
Serial.println("10char longstr");
Serial.println(longstr); //prints 10chars
Serial.println("."); //debug only
Serial.println("."); //debug only
Serial.println("The above should be 16 characters"); //debug only
}
Serial Output:
loopstart
intxy set
loopstart
intxy set
1234567890endwhile
5554443332endwhile2
.
.
6char lcdlat
Lati:
.
10char latstr
12345678905554443332gg
.
6char lcdlong
Long:
.
10char longstr
5554443332gg
.
.
The above should be 16 characters