Offline
Newbie
Karma: 0
Posts: 37
|
 |
« on: January 14, 2013, 05:52:54 pm » |
Hello Everyone, I've been able to read and select a string of data to display on my terminal using: for (int i=6;i<11;i=i+1) { Serial.print(sensorstring[i]); } Serial.println(" Liters/Minute"); //
My goal is to convert this value to a 'number' so I can run an algorithm on it. I tried to use Serial.parseInt()
but am unable to figure out the syntax and the Arduino Reference site does not have any examples. Any insight is appreciated.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #1 on: January 14, 2013, 05:58:10 pm » |
int value = atoi(sensorstring);
-br
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #2 on: January 14, 2013, 05:59:32 pm » |
That "string" is an array so by using atoi(sensorstring) it will be converted to an integer.
|
|
|
|
« Last Edit: January 14, 2013, 06:11:10 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #3 on: January 14, 2013, 06:13:41 pm » |
Thanks for that, I tried: int value = atoi(sensorstring); Serial.println(value);
error: cannot convert 'String' to 'const char*' for argument '1' to 'int atoi(const char*)'
Is it because it's reading sensorstring as '1'?
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
God Member
Karma: 37
Posts: 974
Get Bitlash: http://bitlash.net
|
 |
« Reply #4 on: January 14, 2013, 06:17:30 pm » |
Sorry, it's probably time to show us all your code, please. I was guessing that sensorvalue was char[] not String.
-br
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #5 on: January 14, 2013, 06:22:16 pm » |
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: #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 } }
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #6 on: January 14, 2013, 07:04:49 pm » |
Oh you want sensorstring.toInt(), not sensorstring[ i ].
|
|
|
|
« Last Edit: January 14, 2013, 07:11:13 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #8 on: January 14, 2013, 07:50:27 pm » |
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. /* 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; } }
|
|
|
|
« Last Edit: January 14, 2013, 08:00:57 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
God Member
Karma: 9
Posts: 836
|
 |
« Reply #9 on: January 14, 2013, 08:41:24 pm » |
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.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #10 on: January 16, 2013, 01:09:48 pm » |
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. /* control test */ #include<string.h> int DRV1,DRV2,STRR,STRL; int x = 0; int y = 0; int s = 0; ...
Hi HazardsMind, I went through the sample you suggested and noted what "I think" each line does. The only one which I am figuring out are: 1. void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ??? 2. move(x, y, s); // DO NOT UNDERSTAND "move()" //from http://arduino.cc/forum/index.php?topic=142777.0
/* control test //name of program */ #include<string.h> //name of library needed int DRV1,DRV2,STRR,STRL; //declare 4 integers int x = 0; //intialize x int y = 0; //initialize y int s = 0; //initialize s void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ??? //double z; //double the z variable (for more precision) String val = ""; //set some buffer aside for val String X= ""; //set some buffer aside for X String Y= ""; //set some buffer aside for Y String state = ""; //set some buffer aside for state //int state = 0; //initialize state to 0 int currentCommand = 0; //initialize current command to 0
int ledpin = 13; //set LED pin byte Mopen = 4; //sets Mopen to B0100 byte Mclosed = 2; //sets Mclosed to B00010 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); // pin Mopen is set as an output pinMode(Mclosed, OUTPUT); // pin Mclosed is set as an output pinMode(M1L, OUTPUT); //pin MIL is set as an output pinMode(M1R, OUTPUT); // in MIR is set as an output pinMode(M2L, OUTPUT); //pin M2L is set as an output pinMode(M2R, OUTPUT); // pin M2R is set as an output Serial.begin(9600); // start serial communication at 115200bps
}
void loop() { if( Serial.available()) // if data is available to read, this is how to check the serial part for strings { digitalWrite(ledpin, HIGH); // if data is detected set the pint set to ledpin to HIGH (ON) char c= Serial.read(); // the string in serial read() is buffered in to c as characters if (c == ','){ //look at the incoming chars, if it is a ',' then switch the case currentCommand++; //for some reason currentCommand increments???? } else { //if it is not ',' then store chars in string, keep storing the string until a , is found val += c; // same as (val + c = c)val is the next string + the previous character stored in buffer //Serial.println(val); switch (currentCommand) { // for each string, currentCommand appears to increment by 1
case 0: //in the even there was only 1 string before the ',' X is equal to the val in buffer and val buffer is cleared "" X += val; // same as (X + val = val), new variable X is equal to the whole string val before the ',' val = ""; // clear the information in the val buffer break; // break out of case 0 case 1: //if currentCommand equals 1 Y += val; // same as (Y + val = val), Y is now the string waiting in the val buffer val = ""; // clear out the val buffer break; //get out of case 2
case 2: //Serial.println("X: "+X); //Serial.println("Y: "+Y); state = val; // this will only receive 1 value currentCommand = 0; //reset currentCommand counter to 0 val = ""; // clear out the val buffer //Serial.println("state: "+state); //Serial.println(); x=X.toInt(); //string to int , convert string X to integer y=Y.toInt(); //string to int, convert string Y to integer s=state.toInt(); //string to int, convert state to integer X=""; Y=""; state=""; //clear the X, Y and state buffer information move(x, y, s); // DO NOT UNDERSTAND "move()" break; // get out of case 3 } } } } //***ALL THIS STUFF BELOW IS FOR SERVO MOVEMENT //***SENDS OUT VOLTAGE VALUES 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; } }
|
|
|
|
|
Logged
|
|
|
|
|
California
Offline
Edison Member
Karma: 41
Posts: 1883
|
 |
« Reply #11 on: January 16, 2013, 01:24:16 pm » |
Phew, where to begin... 1. void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ??? This is a function prototype. The Arduino IDE handles these for you, so typically these are not necessary. 2. move(x, y, s); // DO NOT UNDERSTAND "move()" It's just a call to the function with the provided values. if( Serial.available()) // if data is available to read, this is how to check the serial part for strings characters, not strings. char c= Serial.read(); // the string in serial read() is buffered in to c as characters the character is read, not the string if (c == ','){ //look at the incoming chars, if it is a ',' then switch the case currentCommand++; //for some reason currentCommand increments???? } If the character is a deliminator (comma in this case) increment the number of commands received. else { //if it is not ',' then store chars in string, keep storing the string until a , is found val += c; // same as (val + c = c)val is the next string + the previous character stored in buffer //Serial.println(val); If it's not a comma, concatenate it onto the String val (note the difference in capitalization, String and string are not the same thing).
|
|
|
|
« Last Edit: January 16, 2013, 01:47:46 pm by Arrch »
|
Logged
|
|
|
|
|
Queens, New York
Offline
Edison Member
Karma: 29
Posts: 1586
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #12 on: January 16, 2013, 01:31:02 pm » |
void move(int x = 0,int y = 0,int s = 0); //Not sure why this needs to be done ??? Its not needed, you can take it out. I originally wrote this code using a C++ based debugger software (old software), and it had to be inserted at the top. so when I brought it to arduino, I forgot to take it out. move(x, y, s); // DO NOT UNDERSTAND "move()" Move() is a function I made that feeds those gotten values to my motors, it is get rid of too much cluster in my Loop function. It is very difficult to modify code on an IPhone, so I just left everything in. I just wanted to show you an example of how to get, convert and split the data to different ints. And they are not servos, they are DC motors. However same can be done with servos.
|
|
|
|
« Last Edit: January 16, 2013, 01:40:36 pm by HazardsMind »
|
Logged
|
UNO, MEGA, NANO, 4x4 keypad, micro servos, RF transceivers, bluetooth, ultrasonic sensor, 20x4 I2C LCD, 3.2 TFT touch screen, L298N Dual motor driver, Voice Recognition 15W, Gameduino
Arduino Tutorials, coming soon.
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 37
|
 |
« Reply #13 on: January 16, 2013, 02:40:39 pm » |
Phew, where to begin... ...
Thanks Arrch, I would like convert this into a floating number but I'm having a hard time finding the Reference syntax in Arduino for the following C equivalents? Would you happen to know where I can find them? atol (Convert string to long integer) obvoiusly this is "toInt()" atof (Convert string to double) strtol (Convert string to long integer)
|
|
|
|
|
Logged
|
|
|
|
|
Seattle, WA USA
Online
Brattain Member
Karma: 316
Posts: 35574
Seattle, WA USA
|
 |
« Reply #14 on: January 16, 2013, 02:47:27 pm » |
You need to be clearer about what you want to do. There is a huge difference between a string and a String. The string is not a class and does not have a toInt() method.
Frankly, you should not be using the String class at all. It has a major problem that is going to, sooner or later, bite you.
|
|
|
|
|
Logged
|
|
|
|
|
|