0

I am trying to use a Raspberry Pi to control a ESC and for that I am sending the instructions for the ESC using serial. My problem is that when i send the instructions i can not interoperate them, and by interoperate I mean check what the first letter is and then remove it and make the Arduino output a PWM signal. My problem is when I use the serial monitor to send an instruction it firstly comes up on several lines, and when I try and look at the first letter of the string the if statement thinks it is true for all inputs of "FirstLetter" my function to get the serial input it

String readSerial()
{
  char SerialInput;
  String Data = "";
  if (Serial.available())
  {
    SerialInput = Serial.read();
    Data.concat(SerialInput);
    //Serial.println(Data);
  }
  return Data;
}

the function that should interoperate the information from readSerial() is

void DecodeInput()
{
  String input = readSerial();
  if (input != "")
  {
    char FirstLetter = input.charAt(1);
    if (FirstLetter = 'U')
    {
      Serial.println("Hello World");
      //input.setCharAt[0, '');
      //SetPower('U', Power);
    }

2 Answers 2

1

Two things immediately jump out at me:

char FirstLetter = input.charAt(1);

charAt(1) is the second letter, not the first - the first is charAt(0).

if (FirstLetter = 'U')

That is saying "If I have been successful in assigning the value 'U' to FirstLetter then..." - "=" is assignment, you need "==" for comparison.

Also your readSerial() function is complete junk. It's not doing anything at all like you think it is. You should read this to better understand Arduino serial reading: http://hacking.majenko.co.uk/reading-serial-on-the-arduino

0

You may want to look at the Firmata protocol, and not re-invent the wheel. Although doing so (re-inventing the wheel) is a good learning exercise, so is understanding something already written.

http://www.arduino.cc/en/Reference/Firmata

Then move on to the cool stuff that uses it.

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.