Im trying to be able to write 127,255,255 into the console of the arduino and i want to split the 3 serperate numbers to use for the RGB led how would i go about doing this
Step #1: Don't cross post. Delete one of your posts
http://arduino.cc/forum/index.php/topic,78024.msg589191.html#msg589191
Step 2?
Step 2 is to post the code you use to collect the serial data on the Arduino.
How you collect the data is important in choosing the right method of parsing the data.
Use strtok() if you collect the data in a char array. Use indexOf() and substring() if you collect the data in a String object. Learn more about serial data transmission and collecting if you don't collect it.
That is part of my problem i dont know how to take the data coming in as an array.
Time to put the Arduino aside for a while, and do some reading.
Yea because that makes a lot of sense -.-
You need to understand that Serial only transfers 1 byte at a time. So even if you type "255,255,255" into the Serial Monitor the Arduino sees a '2', then a '5', then a '5', then a ',' and so on.
So the first step is to get the string of characters transfered correctly. There are examples on how to do this in the forum. Start there and then you'll have actual code you can ask questions about.
Something like this right?
while(Serial.available() < 13) { // Wait until we receive 13 characters
well that won't wait for 13 characters, that'll fire even when Serial.available() is 0.
well that won't wait for 13 characters, that'll fire even when Serial.available() is 0.
Whether the body of the while does anything, or not, and whether that is a bad thing, depends on what is in the body of the while loop, which was not shown.
OP: What do you propose to do if a byte gets lost during transmission/reception?
Sending a specific number of characters, and expecting them all to arrive, is not a good thing. Start and end of packet markers are.
Examples please
Examples please
All of my posts are full of examples of start and end markers. I've highlighted the ones in this post to give you an idea.
Below is some two servo test code that you can use as a base to make yourself test code for your project. In this code the data is captured in a string, then the servo positions are extracted from the string based on knowing their positions in the string.
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// 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
#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("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
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 results
Serial.println(servo2);
int n1; //declare as number
int n2;
int n1 = servo1.toInt();
int n2 = servo2.toInt();
/*char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
servo2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2); */
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString="";
}
}
Thanks i get how it works but... it only works once then numbers start getting messed up
Thanks i get how it works but... it only works once then numbers start getting messed up
Time to post some code and input and output. We can't see your code, what you are typing, or what appears in the serial monitor.
Ok
// zoomkat 11-22-10 serial servo (2) test
// for writeMicroseconds, use a value like 1500
// for IDE 0019 and later
// 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
#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("servo-test-21"); // so I can keep track of what is loaded
}
void loop() {
while (Serial.available()) {
delay(1);
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 results
Serial.println(servo2);
int n1 = servo1.toInt();
int n2 = servo2.toInt();
/*char carray1[6]; //magic needed to convert string to a number
servo1.toCharArray(carray1, sizeof(carray1));
n1 = atoi(carray1);
char carray2[6];
servo2.toCharArray(carray2, sizeof(carray2));
n2 = atoi(carray2); */
myservo1.writeMicroseconds(n1); //set servo position
myservo2.writeMicroseconds(n2);
readString = "";
}
}
The first time you open serial mon..
IN: 12345678
OUT:12345678
OUT:1234
OUT:5678
then a second time you send 8 bits
IN: 15001500
OUT:150
OUT:150
OUT:1234
OUT:01500
OUT:0150
OUT:0
hope it helps
to me it looks like its not getting rid of the previous string
The code you posted does not print IN: or OUT: that I can see. Where did that come from?
Why don't you label what you are printing?
Serial.print("servo1: [");
Serial.print(servo1);
Serial.println("]");
would make it perfectly clear what each printed value meant.
That code that zoomkat keeps posting is fundamentally flawed, though he refuses to admit it. It expects serial data to arrive within some silly time frame. If it doesn't the code doesn't work.
You might be able to band-aid it by increasing the delay to some larger value. Try 10 minutes.
I put the in and out so you would know what i put in and got out. and yes it seems changing the delay to 10 has made it more stable.
Here is what i ended up with:
String readString, r1, g1, b1;
void setup() {
Serial.begin(9600);
Serial.println("Ready"); // sends to the aplication that opens the com port
}
void loop() {
while (Serial.available()) {
delay(10);
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.print("input ");
Serial.println(readString); //see what was received
// expect a string like 127255255 containing the pwm value for each color (RGB)
red1 = readString.substring(0, 3); //get the first three characters
green1 = readString.substring(3, 6); //get the next three characters
blue1 = readString.substring(6, 9); //get the next th characters
Serial.println(red1); //print to serial monitor to see results
Serial.println(green1);
Serial.println(blue1);
int n1 = red1.toInt(); // Converts the sring to a number
int n2 = green1.toInt();
int n3 = blue1.toInt();
readString = ""; // clears the string
}
}
\
it seems to be working so far
PaulS:
That code that zoomkat keeps posting is fundamentally flawed, though he refuses to admit it. It expects serial data to arrive within some silly time frame. If it doesn't the code doesn't work.You might be able to band-aid it by increasing the delay to some larger value. Try 10 minutes.
Come on PaulS, the code works perfectly well for testing using the serial monitor. Once again it seems you are back in you whiner mode with the same outcome of your inability to post working code doing the same thing that fixes whatever issue is buzzing around in your head. My code won't work for all situations, but is a level better than your no code.