I'm very new to arduino and try to learn from basics. This code locks itself when I enter anything other than the syntax written in "loop" section.
I couldn't find way that whenever something different written it says something like "Ooopss" and wait for another syntax.... Thanks in advance.
char inData[20]; // Allocate some space for the string
char inChar=-1; // Where to store the character read
byte index = 0; // Index into array; where to store the character
void setup() {
Serial.begin(9600);
Serial.write("Power On");
}
char Comp(char* This) {
while (Serial.available() > 0) // Don't read unless
// there you know there is data
{
if(index < 19) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
inData[index] = inChar; // Store it
index++; // Increment where to write next
inData[index] = '\0'; // Null terminate the string
}
}
if (strcmp(inData,This) == 0) {
for (int i=0;i<19;i++) {
inData[i]=0;
}
index=0;
return 0;
}
else {
return 1;
}
}
void loop()
{
if (Comp("green")==0) {
Serial.println("\n Green LED");
analogWrite(9, 255);
analogWrite(3, 0);
// Serial.write("\nMotor 1 -> Online\n");
}
if (Comp("red")==0) {
Serial.println("\nRed LED");
analogWrite(3, 255);
analogWrite(9, 0);
// Serial.write("Motor 1 -> Offline\n");
}
if (Comp("turnoff")==0)
{
analogWrite(9, 0);
analogWrite(3, 0);
Serial.println("\nLEDS off");
}
}
You need some sort of "end of transmission" character to let your code know when to start a fresh string.
if (strcmp(inData,This) == 0) {
for (int i=0;i<19;i++) {
inData[i]=0;
}
index=0; // ONLY TIME YOU RESET INDEX
return 0;
}
The only time you reset your index is here, which will only be run when a string compare is true. That's why you need to know when the end of the command is.
You seem to have a pretty good grasp of the serial communications. You do however need to clean up your code a little and allow for some errors.
For example, if I am reading your code correctly, if you enter "Red" instead of "red" it wont match and you have not way of entering another value. If you enter "red" the next time, your compare string 'inData' will be "Redred". Since it never matches you will never get to the part of you code which clears the buffer.
You could do this in a number of ways. One would be a special character that means "This is a new Value".
so if you entered "red", it would be added to the existing string. If you entered #red, it would start a new string with 'red' in it. So you could send "#r", "e", "d" in three different transmissions and end up with "red" in you input buffer.
I think if you change your loop to something like this:
if (index < sizeof(inData)-1) // One less than the size of the array
{
inChar = Serial.read(); // Read a character
if (inChar == '#') {
index = 0;
} else {
inData[index++] = inChar; // Store it and increment where to write next
}
inData[index] = '\0'; // Null terminate the string
}
Obviously you can use some other reset character. Another, maybe better choice, might be '='.