I wrote this sketch to send codes to a speakjet sound processor. It will store any number of three digit integers from 128 to 254. I type them into the serial monitor and hit enter after each one. it is stored in an array, and after 900 is entered, the values stored in the array are sent to the speakjet and also read back to the serial monitor. The issue I'm having is that the array will return a value of three for index 0 every time. I have no clue as to why this is.
Here is the code.
int in=0;
int code;
int phrase[] {};
int x=0;
String stack="";
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
Serial.println("Ready");
}
void loop() {
while (Serial.available()>0) {
int inbyte = Serial.read();
if (in<=2);
{
stack += (char)inbyte;
in++;
//Serial.println(stack);
//Serial.println(in);
}
if (in==3) {
if (stack == "900") {
for(int i=0;i<x;i++) {
Serial1.write(phrase[i]);
Serial.println(i);
Serial.println(phrase[i]);
Serial.println();
}
stack="";
x = 0;
in = 0;
Serial.println("Ready");
} else {
phrase[x]=0;
int code = stack.toInt();
if (code < 128) {
code = 253;
}
phrase[x]=code;
Serial.println(x);
Serial.println(phrase[x]);
x++;
in=0;
Serial.write(code);
Serial.println();
stack = "";
}
}
}
}
Thanks for any advice, or help with this matter.
Brewer.