Go Down

Topic: Convert String From Serial Data to Numerical Value (Read 3 times) previous topic - next topic

chiques


Sorry, it's probably time to show us all your code, please.  I was guessing that sensorvalue was char[] not String.

-br



No Problem, here it is:


Code: [Select]


#include <SoftwareSerial.h>                                                    //add the soft serial libray
#define rxpin 2                                                                //set the RX pin to pin 2
#define txpin 3                                                                //set the TX pin to pin 3


SoftwareSerial myserial(rxpin, txpin);                                         //enable the soft serial port


String inputstring = "";                                                       //a string to hold incoming data from the PC
String sensorstring = "";                                                      //a string to hold the data from the Atlas Scientific product
boolean input_stringcomplete = false;                                          //have we received all the data from the PC
boolean sensor_stringcomplete = false;                                         //have we received all the data from the Atlas Scientific product


  void setup(){                                                                //set up the hardware
     Serial.begin(38400);                                                      //set baud rate for the hardware serial port to 38400
     myserial.begin(38400);                                                    //set baud rate for software serial port to 38400
     inputstring.reserve(5);                                                   //set aside some bytes for receiving data from the PC
     sensorstring.reserve(30);                                                 //set aside some bytes for receiving data from Atlas Scientific product
     }



   void serialEvent() {                                                         //if the hardware serial port receives a char
               char inchar = (char)Serial.read();                               //get the char we just received
               inputstring += inchar;                                           //add it to the inputString
               if(inchar == '\r') {input_stringcomplete = true;}                //if the incoming character is a <CR>, set the flag
              } 



void loop(){                                                                   //here we go....
     
  if (input_stringcomplete){                                                   //if a string from the PC has been recived in its entierty
      myserial.print(inputstring);                                             //send that string to the Atlas Scientific product
      inputstring = "";                                                        //clear the string:
      input_stringcomplete = false;                                            //reset the flage used to tell if we have recived a completed string from the PC
      }


  while (myserial.available()) {                                               //while a char is holding in the serial buffer
         char inchar = (char)myserial.read();                                  //get the new char
         sensorstring += inchar;                                               //add it to the sensorString
         if (inchar == '\r') {sensor_stringcomplete = true;}                   //if the incoming character is a <CR>, set the flag
         }
 
   for (int i=6;i<11;i=i+1) {
        Serial.print(sensorstring[i]);
        int value = atoi(sensorstring);
        Serial.println(value);
        }

   if (sensor_stringcomplete){                                                 //if a string from the Atlas Scientific product has been received in its entirety
       Serial.print(sensorstring);                                             //use the hardware serial port to send that data to the PC
       sensorstring = "";                                                      //clear the string:
       sensor_stringcomplete = false;                                          //reset the flag used to tell if we have received a completed string from the Atlas Scientific product
      }
}




HazardsMind

#6
Jan 15, 2013, 01:04 am Last Edit: Jan 15, 2013, 01:11 am by HazardsMind Reason: 1
Oh you want sensorstring.toInt(), not sensorstring[ i ].
Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

chiques


Oh you want sensorstring.toInt(), not sensorstring[ i ].


Hi HazardMind,
I'm looking for some sample code to see how this is used. Is this something similar to what you suggest?

http://electronics.stackexchange.com/questions/38845/arduino-serial-read-to-int

HazardsMind

#8
Jan 15, 2013, 01:50 am Last Edit: Jan 15, 2013, 02:00 am by HazardsMind Reason: 1
I'm not home right now, but I will give you a sample as soon as I can.

Edit: I was able to get this from a post I did a while back. Wow my iPhone likes to stretch things out. Sorry about that.


Code: [Select]
/*
control test
*/
#include<string.h>
int DRV1,DRV2,STRR,STRL;
int x = 0;
int y = 0;
int s = 0;
void move(int x = 0,int y = 0,int s = 0);
//double z;
String val = "";
String  X= "";
String  Y= "";
String  state = "";
//int state = 0;
int currentCommand = 0;

int ledpin = 13;
byte Mopen = 4;
byte Mclosed = 2;
byte M1L = 3;// PWM
byte M2L = 5;// PWM
byte M1R = 9;// PWM
byte M2R = 6;// PWM

void setup()
{
  pinMode(ledpin, OUTPUT);  // pin 13 (on-board LED) as OUTPUT
  pinMode(Mopen, OUTPUT);                               
  pinMode(Mclosed, OUTPUT);
  pinMode(M1L, OUTPUT);                               
  pinMode(M1R, OUTPUT);
  pinMode(M2L, OUTPUT);                               
  pinMode(M2R, OUTPUT);
  Serial.begin(9600);       // start serial communication at 115200bps

}

void loop() {
  if( Serial.available())       // if data is available to read
  {
    digitalWrite(ledpin, HIGH);
    char c= Serial.read();
    if (c == ','){    //look at the incoming chars, if it is a ',' then switch the case
      currentCommand++;
    }
    else {   //if it is not ',' then store chars in string
      val += c;
      //Serial.println(val);
      switch (currentCommand) {

      case 0:
        X += val;
        val = "";
        break;

      case 1:
        Y += val;
        val = "";
        break;

      case 2:
      //Serial.println("X: "+X);
      //Serial.println("Y: "+Y);   
        state = val;   // this will only receive 1 value
        currentCommand = 0;
        val = "";
        //Serial.println("state: "+state);
        //Serial.println();
        x=X.toInt();  //string to int
        y=Y.toInt();  //string to int
        s=state.toInt();  //string to int
        X=""; Y=""; state="";
        move(x, y, s);
        break;
      }
    }
  }
}
   
  void move(int x, int y, int s)
  { 
      x=constrain(x, -90, 90);
      y=constrain(y, -90, 90);

  //Movement varibles
 
  int DRV2 = map(x, -90, 0, 255, 0);
  int DRV1 = map(x, 0, 90, 0, 255);
  int STRR = map(y, -90, 0, 255, 0);
  int STRL = map(y, 0, 90, 0, 255); 

  if(x > 0)//forwards               
  {
    //Serial.println("Driving");
    analogWrite(M1L, abs(DRV1 - STRL)); analogWrite(M1R, abs(DRV1 - STRR));   
    analogWrite(M2L, 0); analogWrite(M2R, 0);   
  }
  else if(x < 0)//backwards               
  {
    //Serial.println("Driving");
    analogWrite(M1L, 0); analogWrite(M1R, 0);   
    analogWrite(M2L, abs(DRV2 - STRR)); analogWrite(M2R, abs(DRV2 - STRL));   
  }
  else if(x == 0 && y >0)//right             
  {
    //Serial.println("Driving");
    analogWrite(M1L, STRL); analogWrite(M1R, 0);   
    analogWrite(M2L, 0); analogWrite(M2R, STRL);   
  }
  else if(x == 0 && y < 0)//left             
  {
    //Serial.println("Driving");
    analogWrite(M1L, 0); analogWrite(M1R, STRR);   
    analogWrite(M2L, STRR); analogWrite(M2R, 0);   
  }

  else //full stop
  {
    digitalWrite(M1L, LOW); digitalWrite(M1R, LOW);       
    digitalWrite(M2L, LOW); digitalWrite(M2R, LOW);   
  }

   if(s == 1)
    {
      //Serial.println("Claw Opening");
      digitalWrite(Mopen, HIGH); digitalWrite(Mclosed, LOW);
    }
    if(s == 2)
    {
      //Serial.println("Claw Closing");
      digitalWrite(Mopen, LOW); digitalWrite(Mclosed, HIGH);
    }
    if(s == 0)
    {
      digitalWrite(Mopen, LOW);  digitalWrite(Mclosed, LOW);
    }
   else{
    digitalWrite(ledpin, LOW);
    x=0; y=0; s=0;
  }
}


Created Libraries:
TFT_Extension, OneWireKeypad, SerialServo, (UPD)WiiClassicController, VWID

michinyon

You could also try something like this:

int bufsize=20 ;
char buffer[bufsize] ;
yourString.toCharArray( buffer, bufsize );
int val = atoi( &buffer[0] );

provided it is an integer  and not a number with a decimal point in it,  this might work.
if you like your strings to be simple character arrays, use C
if you like your strings to be objects, use Java
Dealing with both can be a real pain in the neck.

Go Up