Now I think we are getting close, but I see something strange happening
When I added the line Serial.println(char_in[i]);, each character that was being stored in char_in got printed in a new line( like it should). But now the action that was to be performed is being done nicely (LEDs are turning on and off like they should). I cannot figure out why the program is running like it should after this line..
When I remove it the old behaviour starts.
The loop to give '0' values to each element is added to flush out the garbage values from previous serial input.
int ledPin = 13;
const int maxLength=140; //maximum length for textbox data
char char_in[maxLength]; //Create a buffer to input all incoming data
int i=0;
void setup() {
pinMode(ledPin, OUTPUT); //Configure pins as output
Serial.begin(9600); //begin serial communication
}
void loop() {
if (Serial.available() > 0) //Is any serial data available
{
for( i=0;i<maxLength;i++)
{
char_in[i] = 0;
}
for( i=0;i<maxLength && Serial.available();i++)
{
char_in[i] = Serial.read();
Serial.println(char_in[i]);
}
// Serial.print(char_in);
char_in[i+1]='\0';
if (!strcmp(char_in,"on1"))
{
digitalWrite(ledPin,HIGH);
}
else if(!strcmp(char_in,"of1"))
{
digitalWrite(ledPin,LOW);
}
else
{
Serial.print(char_in);
}
}
}