#include <String.h>
#define RESET 0
byte buffer;
char bufferString[20];
int index = 0;
void setup()
{
Serial.begin(115200);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop()
{
// Read all available serial data
if(Serial.available()>0)
{ memset(bufferString,0,index);
index = RESET;
while (Serial.available()>0 && index < 20 )
{
delay(5);
buffer = Serial.read();
bufferString[index] = buffer;
index++;
}
bufferString[index] = '\0';
// Start processing data
if(strcmp(bufferString, "On") == 0)
{
// <<------------------------------------------------ Start
do
{
for (int brightness = 0; brightness < 255; brightness++)
{
analogWrite(5, brightness);
delay(2);
}
for (int brightness = 255; brightness >= 0; brightness--)
{
analogWrite(5, brightness);
delay(2);
}
}
while(strcmp(bufferString, "On") == 0);
// <<-------------------------------------------------- End
}
else if(strcmp(bufferString, "Off") == 0)
{
digitalWrite(5,LOW);
analogWrite(5, 0);
}
else if(strcmp(bufferString, "Push") == 0)
{
digitalWrite(6,HIGH);
}
else if(strcmp(bufferString, "Pull") == 0)
{
digitalWrite(6,LOW);
}
}
}
I had problem for the code between the comment start/end region
After sending in a "On" command, Pin 5 will run according to the code but will never jump out from the while loop even i send in the "Off" command. The pin 5 LED keep running even i send the "Off" command. its seems like the program is ignoring and can't make String comparison after jump into the while loop. After in the While loop, all my command was ignored. How to make this work? Please help.