I didn't read all the noise, but below is some simple servo test code that gets a string from the serial port, breaks it into two parts, and converts each part into a number for a servo control position.
// zoomkat 12-13-11 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 1.0
// Powering a servo from the arduino usually DOES NOT WORK.
// two servo setup with two servo commands
// send eight character string like 15001500 or 14501550
// use serial monitor to test
#include <Servo.h>
String readString, servo1, servo2;
Servo myservo1; // create servo object to control a servo
Servo myservo2;
void setup() {
Serial.begin(9600);
myservo1.attach(6); //the pin for the servo control
myservo2.attach(7);
Serial.println("two-servo-test-1.0"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(3); //delay to allow buffer to fill
if (Serial.available() >0) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
}
}
if (readString.length() >0) {
Serial.println(readString); //see what was received
// expect a string like 07002100 containing the two servo positions
servo1 = readString.substring(0, 4); //get the first four characters
servo2 = readString.substring(4, 8); //get the next four characters
Serial.println(servo1); //print to serial monitor to see parsed results
Serial.println(servo2);
int n1 = servo1.toInt();
int n2 = servo2.toInt();
Serial.println("the numbers are :");
Serial.println(n1); //print to serial monitor to see number results
Serial.println(n2);
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}
}
Thank you, guys. All these examples helped out. When I become more proficient, I will try out Morris' first example but for now these latter examples are understandable for me.
void loop ()
{
int y = Serial.available();
Serial.println(y);
int cont=0;
int suffix=0;
//--------------------------------------
while (cont!=y && cont<n)
{
Byte_received = Serial.read();
text[cont]=Byte_received;
cont++;
}
//--------------------------------------
if (y>0)
{
suffix = (atoi(&text[1]));
Serial.print(text[0]);
Serial.print("-");
Serial.println(suffix);
switch (text[0])
{
case 'M':
if(suffix >65 && suffix < maximum)
{
change_speed(suffix);
}
break;
case '0':
change_speed(minimum);
break;
default:
break;
}
}
can somebody specify me the output at each stage by adding comments to the above code??
after " suffix = (atoi(&text[1]));" this instruction what will be the data in suffix if i send "M78" to arduino
How many characters or length of the string can i receive by using serial.read ???
can i send "A2000" to arduino via bluetooth??
how can i break it into two parts "A" and "2000" how to seperate it by using above program???
can somebody specify me the output at each stage by adding comments to the above code??
I could, but I won't. You can RTFM, and add Serial.print() statements to confirm/refute what you think you learned.
after " suffix = (atoi(&text[1]));" this instruction what will be the data in suffix if i send "M78" to arduino
Why didn't you run the code and find out?
How many characters or length of the string can i receive by using serial.read
Serial.read() returns one int that contains one useful byte in the low byte, and an error code in the high byte. If the high byte is 0, the low byte can be used.
can i send "A2000" to arduino via bluetooth??
If you have a bluetooth device, and it is paired to something that can send data, yes.
how can i break it into two parts "A" and "2000" how to seperate it by using above program???
If the first part is always one letter, then the above code does that.
@Pauls don't get angry bro but i don't have bluetooth module i have ordered it online it will be available soon...so first i am confirming that i can send and decode the sequence using bluetooth..thank you for support bro...i have tested above code on my friends device...it works fine...
i have few questions for you...
can i send "A1000B1000C1000D1000*" these 25 characters via bluetooth???
thus arduino will receive this much characters(25) ??? By using Serial.read??
3)and how can i decode 25 serial bit into A and its value...B and its value...c and its value and d and its value???i have used * for seperation?
one option in my mind that i will use AND logical bitwise function...means for decoding A and its respective value i will use
void loop ()
{
int y = Serial.available();
Serial.println(y);
int cont=0;
int suffix=0;
//--------------------------------------
while (cont!=y && cont<n)
{
Byte_received = Serial.read();
text[cont]=Byte_received;
cont++;
}
//--------------------------------------
if (y==25)
{
String decode=( Byte_received & 011111000000000000000000);//it will remove *carries A1000 and ///////////////////////////////////////////////////////////////////////////remove all char ahead
suffix = (atoi(&decode));
Serial.print(text[0]);
Serial.print("-");
Serial.println(suffix);
}
can i send "A1000B1000C1000D1000*" these 25 characters via bluetooth???
With the right app on the sending end, yes.
thus arduino will receive this much characters(25) ??? By using Serial.read??
If they do not get lost in the ether, or mangled, yes. NOT all at once, though. Sending start and end markers is a much better idea. "<A1000B1000C1000*D1000>".
The delimiters you have are not useful delimiters. There is no way, using the * to tell the start of a packet from the end of a packet from data that is reasonably IN the packet.
3)and how can i decode 25 serial bit into A and its value...B and its value...c and its value and d and its value???i have used * for seperation?
Save all the data in a NULL terminated array of chars, and use strtok() to extract each token.
String decode=( Byte_received & 011111000000000000000000);//it will remove *carries A1000 and ///////////////////////////////////////////////////////////////////////////remove all char ahead
Masking the last 16 bits received with a 24 digit octal constant does not make sense. Storing the result in a String does not make sense.
Passing atoi() the address of a String does not make sense.
@pauls i have tried my code and it work successfully but if the byte gets mangled then problem will occur as per u say...so i would like to go with u and decode "<A1000B1000C1000*D1000>". please help me to design the code using strtok()... i want indivual output and error checkung for the recieved byte..
i am coming from vb6 to Arduino C , i Have big trouble with C:
my question is exactly like {T8615 } User
this is a very simple matter in vb but here i spend so many hours for converting a String extracted from serial terminal to a number and after reading your Deals i understand Nothing.
in fact i need to recognize numbers from the serial and get they pure value to a Integer value
Truly C is very weak at String Manipulation.
valasoft:
Truly C is very weak at String Manipulation.
Then I have bad news for you It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.
n fact i need to recognize numbers from the serial and get they pure value to a Integer value
And some good news ...
Have a look at the parse example in Serial Input Basics
If that does not help solve your problem then please describe if completely. This is a very old Thread you have joined.