Reading a value from a string through serial

Hello everyone,

I am having a problem with getting a value in between a string sent through the serial. I am sending this string from an arduino with an xbee shield using a potentiometer. And then I have attempted to categorize every different potentiometer value sent (right now i am only sending two different values 'a' and 'b') using a letter in front and then a period indicating when to stop reading for that category.

example:

potentiometer 1 reading = 500 potentiometer 2 reading = 100

arduino sender sends A500. and then sends B100.

how would i write some code that sees there is an 'A' in the serial then reads the 500 and stops at the period and then analogWrites the 500/4 into an led. and then ignores the B value so i can use it for another component.

My sending code: :o

int Pot1a = A0;//x right joy //see paper diagram int Pot1b = A1;//y right joy int Pot2a = A2;//x left joy int Pot2b = A3;//y left joy int LinePot = A4;//Thrust

void setup() { Serial.begin(9600); }

void loop() {

char string_to_send[40]; int Horizontal_Joy_Right = analogRead(Pot1a); int Vertical_Joy_Right = analogRead(Pot1b); int Horizontal_Joy_Left = analogRead(Pot2a); int Vertical_Joy_Left = analogRead(Pot2b); int Linear_Pot = analogRead(LinePot);

sprintf(string_to_send, "A%d.", Linear_Pot);//Convert the value of pot to a string (Sprintf()) returns a string identifier is first letter and the end is indicated by the period. Serial.print(string_to_send); delay(100); sprintf(string_to_send, "B%d.", Horizontal_Joy_Left); Serial.print(string_to_send); delay(100); }

My failed attempt at reading: :o

const int ledPin = 11; // the pin that the LED is attached to int PotData = 0; // a variable to read incoming serial data into

void setup() { // initialize serial communication: Serial.begin(9600); // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); }

void loop() { if (Serial.available() > 0) { char Type = Serial.read(); if (Type == 'A') { while(Type != '.'){ Type = Serial.read(); analogWrite(ledPin, Type/4); delay(5); } } } }


I feel like this is actually a very easy thing to do, but i don't know how.

Instructions for modifying a post...

• In the lower-right corner of the post is an option labeled [u]More[/u]. Click it.

• The first option on the pop-up menu is [u]Modify[/u]. Click it.

Good luck.

thanks

First off, you might want to check out: https://www.arduino.cc/en/Tutorial/StringToIntExample

Other than that, staying with your current solution, the Problem as I see it is in the receiving Loop.

If you received "A100.", the Loop is writing each numeric Digit in turn. You do three analog writes of 1/4 then 0/4 and 0/4 again.

I assume that you wanted to write 100/4.

Before the Loop, initialize an INT variable to 0. Read a Digit, if it is "." then analog write the int/4. If it was not ".", first multiply the INT variable by ten and then add the Digit. Keep in mind that a string character "0" has a numeric ASCII value of 48 so you Need to do that Offset.

Have a look at Serial Input Basics - simple reliable ways to receive data. There is also a parse example.

...R

JaBa: First off, you might want to check out: https://www.arduino.cc/en/Tutorial/StringToIntExample

Other than that, staying with your current solution, the Problem as I see it is in the receiving Loop.

If you received "A100.", the Loop is writing each numeric Digit in turn. You do three analog writes of 1/4 then 0/4 and 0/4 again.

I assume that you wanted to write 100/4.

Before the Loop, initialize an INT variable to 0. Read a Digit, if it is "." then analog write the int/4. If it was not ".", first multiply the INT variable by ten and then add the Digit. Keep in mind that a string character "0" has a numeric ASCII value of 48 so you Need to do that Offset.

HEY! thanks for the help i looked at the link u gave me and it works, well i'm pretty sure it does! glad for the answer.

When you see an a or a b, make a note of which one you saw and set the value to zero.

When you see a digit, multiply the value by 10 and add the value of the digit (‘2’ → 2).

When you see a full stop, that’s it!

int value;
byte whichone;

loop() {
  whie(Serial.available()) {
    char ch = Serial.read();
    if(ch >= 'a' && ch <= 'z') {
      whichone = ch;
      value = 0;
    }
    else if(ch >= '0' && ch <= '9')  {
      value = value * 10 + (ch-'0');
    }
    else if(ch == '.') {
      // W00T! We just read a vaue! Do something with it!


      value = 0;
      whichOne = '-';
    }
  }
}