Help with code

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;
    
  }
}

Please post your actual code

Put in a piece of test code to print the data that has been received before you try to use it - just to be sure you are getting what you expect.

I have never tried atoi() on a '+' character and I don't know what it does - are you sure it returns the ascii value for a + symbol?

...R

AWOL:
Please post your actual code

This is the whole code.

fsoender:
This is the whole code.

It can't be. It won't compile.

Robin2:
Put in a piece of test code to print the data that has been received before you try to use it - just to be sure you are getting what you expect.

I have never tried atoi() on a '+' character and I don't know what it does - are you sure it returns the ascii value for a + symbol?

...R

I think your on to something, I change it to "0" and "1" from C#, now I get "Motor RPM = 0" and "Motor RPM = 1" when I press the C# buttons, when I use the slide I get the correct value.
Why is the "Motor RPM =" interfering with "LED on" or "LED off"?

Does SendNewData() ever get called to help with switching NewData back & forth between true & false?
Or just not implemented yet?

aarg:
It can't be. It won't compile.

Sorry, change the "Serial.println( "Your connected);" to Serial.println( "Your connected");

CrossRoads:
Does SendNewData() ever get called to help with switching NewData back & forth between true & false?
Or just not implemented yet?

Not more than in the code I have, you think that's were the fault is?

And fix the spelling (grammer?):
"You're connected" or "You are connected"

CrossRoads:
And fix the spelling (grammer?):
"You're connected" or "You are connected"

He he, ok :slight_smile:

Fixed it with a CASE statement :slight_smile:

fsoender:
Fixed it with a CASE statement :slight_smile:

You mean a switch statement?

yes

fsoender:
I think your on to something, I change it to "0" and "1" from C#, now I get "Motor RPM = 0" and "Motor RPM = 1" when I press the C# buttons, when I use the slide I get the correct value.
Why is the "Motor RPM =" interfering with "LED on" or "LED off"?

I don't think you are thinking.

The "Motor RPM" is not interfering with anything. It is doing what you have told it to do. The LEDs only operate if + or - is detected and the code in your Original Post can't detect + or -

You need different code (not atoi() ) if you want to detect + and -. Alternatively you could treat the values 0 and 1 as special cases to be dealt with before the MotorRPM part.

...R