i´ve got a problem with the serial read command.
The numbers 0-9 are no problem, but how can i use numbers over 9 ?
The serial monitor show that as each number.
For example if i type 180 in , the serial monitor gives me the number 1 , 8 and 0 but not as one number 180.
How can i ''link'' the numbers to one ?
I wont control a servo over the serial monitor.
int incomingByte = 0; // for incoming serial data
int a;
void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// send data only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
a = incomingByte-48;
// say what you got:
Serial.print("I received: ");
Serial.println(b, DEC);
}
}
The numbers 0-9 are no problem, but how can i use numbers over 9 ?
The same way everyone else does. Read the data as it comes in, and store it in an array until the end of packet marker arrives. When the end of packet marker arrives, parse the packet and turn each token into an int. There are hundreds of examples around.
You don't have an end of packet marker? You're screwed, then, You need to add one!
What have you tried? It's hard to believe that not one of the "thousands of examples" provided any enlightenment. There are only about three ways to read serial data and convert the result to an int. The lazy way is to simply call Serial.parseInt().
int inByte = Serial.read();What does this line do apart from reading the first character and throwing it away ?
Try printing the value of a before writing it to the servo and you will see what is going on. If you still want to use parseInt() and it is too slow, then reduce the Serial timeout using Serial.setTimeout() but then you must ensure that the data arrives before the timeout.
A very simple example of capturing serial data into a String, then doing something with the data.
// zoomkat 8-6-10 serial I/O string test
// type a string in serial monitor. then send or enter
// for IDE 0019 and later
//A very simple example of sending a string of characters
//from the serial monitor, capturing the individual
//characters into a String, then evaluating the contents
//of the String to possibly perform an action (on/off board LED).
int ledPin = 13;
String readString;
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
Serial.println("serial on/off test 0021"); // so I can keep track
}
void loop() {
while (Serial.available()) {
delay(3);
char c = Serial.read();
readString += c;
}
if (readString.length() >0) {
Serial.println(readString);
if(readString.indexOf("on") >=0)
{
digitalWrite(ledPin, HIGH);
}
if(readString.indexOf("off") >=0)
{
digitalWrite(ledPin, LOW);
}
readString="";
}
}
mammut139:
nice example.
but how can i get a variable out of this ?
That would depend on what you send expecting to be made into a variable. For number maybe something like below.
// zoomkat 10-14-11 serial test
// for IDE 0022 and later
String readString;
void setup() {
Serial.begin(9600);
Serial.println("number-test-22"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); // allow serial buffer to fill with next character
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int num = readString.toInt();
Serial.print("your number is: ");
Serial.println(num);
Serial.println();
readString=""; //empty for next input
}
}
Here is the sketch that I use myself on a Leonardo to which three servos are attached.
I translated it to english right now without testing, so I hope I did not introduce errors while translating XD.
The basic idea is to treat every character as an individual command (some people may call this approach "simple event handling").
The character 3 means "append the digit 3 to the number". Same for all digits.
The character s means "select the servo".
And u (microseconds) moves the servo. Usually 1000 makes the servo point to the left, 1500 to the middle, 2000 to the right.
A z resets the number to zero.
Example: to select servo #3 and send the number 1500 to it, send this:
3sz1500uz
(z resets the number back to 0)
(Insert spaces as you like, they are ignored)
Good luck!
#include <Servo.h>
// global variables
Servo servo3; // servo attached to pin 3
Servo servo5; // servo attached to pin 5
Servo servo7; // servo attached to pin 7
int number = 0; // number that comes in through the serial port
int servoid = 0; // currently selected servo
void setup()
{
Serial.begin(9600); // open the serial interface
while (!Serial) {;} // this line required if your board is a Leonardo
servo3.attach(3); // attach to pin 3
servo5.attach(5); // attach to pin 5
servo7.attach(7); // attach to pin 7
}
void loop()
{
while(Serial.available() > 0)
{
switch( Serial.read() )
{
case 'z': number = 0; break; // z => zero the number
case '0': number = number*10+0; break;
case '1': number = number*10+1; break;
case '2': number = number*10+2; break;
case '3': number = number*10+3; break;
case '4': number = number*10+4; break;
case '5': number = number*10+5; break;
case '6': number = number*10+6; break;
case '7': number = number*10+7; break;
case '8': number = number*10+8; break;
case '9': number = number*10+9; break;
case 's': // select the motor
servoid = number;
break;
case 'u': // u => writeMicroseconds
switch(servoid)
{
case 3: servo3.writeMicroseconds(number); break;
case 5: servo5.writeMicroseconds(number); break;
case 7: servo7.writeMicroseconds(number); break;
}
break;
case '?': // get a status
Serial.print("number="); Serial.println(number, DEC);
Serial.print("servoid="); Serial.println(servoid, DEC);
break;
} // endSwitch
} // endWhile
}