Hi,
I'm almost there, I have tested the code under individual, but when I put them together I don't get what I expect.
When I test the code for the LED, they turn on and off from my C# program. I send the command "<->" and "<+>".
When I test the analog code, I get what I want out on the port 0-5 Volts by sending the value between 0 and 5000 with the <> included "<0-5000>".
I guess there is something wrong with my IF statement, since when I send "<+>" or "<->" I only get the "Serial.print("Motor RPM =");" executed.
Any suggestions?
const byte NumChars = 32;
char ReceivedChars[NumChars];
boolean NewData = false;
void setup()
{
Serial.begin(9600);
Serial.println( "Your connected);
}
void loop()
{
RecWithStartEndMarkers();
SendNewData();
}
void RecWithStartEndMarkers()
{
static boolean RecInProgress = false;
static byte ndx = 0;
char StartMarker = '<';
char EndMarker = '>';
char rc;
if (Serial.available() > 0)
{
while (Serial.available() > 0 && NewData == false)
{
rc = Serial.read();
if (RecInProgress == true)
{
if (rc != EndMarker)
{
ReceivedChars[ndx] = rc;
ndx++;
if (ndx >= NumChars)
{
ndx = NumChars - 1;
}
}
else {
ReceivedChars[ndx] = '\0';
RecInProgress = false;
ndx = 0;
NewData = true;
}
}
else if (rc == StartMarker)
{
RecInProgress = true;
}
}
}
}
void SendNewData()
{
if (NewData == true)
{
int Test = atoi(ReceivedChars);
if (Test == '+')
{
digitalWrite(9, HIGH);
Serial.println( "LED ON");
}
else if (Test == '-')
{
digitalWrite(9, LOW);
Serial.println( "LED OFF");
}
else
{
int MotorRPM = atoi(ReceivedChars);
Serial.print("Motor RPM =");
Serial.println(MotorRPM);
MotorRPM = map(MotorRPM, 0, 5000, 0, 255);
analogWrite(3, MotorRPM);
}
NewData = false;
}
}