Offline
Full Member
Karma: 2
Posts: 208
|
 |
« Reply #30 on: December 12, 2012, 07:32:48 am » |
I tried zoomkat's code and it worked. I tried to modify his code for wireless communication and it didn't work. I found out that the Println command adds carriage return and line feed characters to end of string being printed. Do I have to modify the code to look for these characters? #include <SoftwareSerial.h> #include <Servo.h> String readString; Servo myservo; // create servo object to control a servo const int xb_rx = 2; const int xb_tx = 3; SoftwareSerial Xbee(xb_rx,xb_tx); void setup() { Serial.begin(9600); Xbee.begin(9600); myservo.writeMicroseconds(1500); //set initial servo position if desired myservo.attach(9); //the pin for the servo control Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded } void loop() { while (Xbee.available()) { char c = Xbee.read(); //gets one byte from serial buffer Serial.println(c); readString += c; //makes the string readString delay(2); //slow looping to allow buffer to fill with next character } if (readString.length() >0) { Serial.println(readString); //so you can see the captured string int n = readString.toInt(); //convert readString into a number if(n >= 500) { Serial.print("writing Microseconds: "); Serial.println(n); myservo.writeMicroseconds(n); } else { Serial.print("writing Angle: "); Serial.println(n); myservo.write(n); } readString=""; //empty for next input } }
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Online
Edison Member
Karma: 31
Posts: 1733
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #31 on: December 12, 2012, 10:39:49 am » |
What are you sending and what are you getting back, lets start there.
You have this, n = readString.toInt(); but you need to convert it AFTER you get everything so right I think it is only getting 1 char then it goes to the IF/ELSE and then gets cleared. You don't give it time to get multiple chars. you need a stop char for it to look for like ":" or ";" or "."
If the incoming char is anyone of these, then it know that you have everthing and it should then convert the string to an INT.
|
|
|
|
|
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
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 208
|
 |
« Reply #32 on: December 12, 2012, 03:43:28 pm » |
I have code that reads joystick input and sends it to another Arduino board. #include <SoftwareSerial.h> const byte PIN_ANALOG_X = 0; const byte PIN_ANALOG_Y = 1; int x_position; int y_position; int x_direction; int y_direction; const int xb_rx = 2; const int xb_tx = 3; SoftwareSerial Xbee(xb_rx,xb_tx); void setup() { Serial.begin(9600); Xbee.begin(9600); } void loop () {
x_direction = 0; y_direction = 0; x_position = analogRead(PIN_ANALOG_X); y_position = analogRead(PIN_ANALOG_Y); map(x_position,0,1024,0,180); Serial.println(x_position); Xbee.println(x_position); }
Serial Monitor displays values properly. Should I try the code from http://arduino.cc/en/Tutorial/VirtualColorMixer ?
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Online
Edison Member
Karma: 31
Posts: 1733
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #33 on: December 12, 2012, 04:05:29 pm » |
The controller puts out values 0 - 180? OK, what are you getting on the other end?
Yea about the println, I found that out too, just do:
Serial.print(n); Serial.println();
|
|
|
|
« Last Edit: December 12, 2012, 04:26:21 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
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
0
Offline
Tesla Member
Karma: 58
Posts: 6781
Arduino rocks
|
 |
« Reply #34 on: December 12, 2012, 06:31:46 pm » |
I found out that the Println command adds carriage return and line feed characters to end of string being printed. Do I have to modify the code to look for these characters? I think the readString.toInt() will ignore trailing non neumeric characters. You may be transmitting faster than you are receiving, flooding the receiving buffer. Try a delay like below to slow the transmission to see if the receiving does better. If that works, then you could do some more tweeking. map(x_position,0,1024,0,180); delay(50); //slow looping Serial.println(x_position); Xbee.println(x_position);
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Full Member
Karma: 2
Posts: 208
|
 |
« Reply #35 on: December 12, 2012, 07:08:04 pm » |
The code for Processing won't work on Arduino. How can I convert the code so that it will work on Arduino? void serialEvent(Serial myPort) { String inString = myPort.readStringUntil('\n'); if (inString != null) { inString = trim(inString); float[] colors = float(split(inString, ",")); if (colors.length >=3) { redValue = map(colors[0], 0, 1023, 0, 255); greenValue = map(colors[1], 0, 1023, 0, 255); blueValue = map(colors[2], 0, 1023, 0, 255); } } }
|
|
|
|
|
Logged
|
|
|
|
|
Queens, New York
Online
Edison Member
Karma: 31
Posts: 1733
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #36 on: December 12, 2012, 08:51:27 pm » |
I can't help you with processing, I don't like it and I try to stay away from it. Sorry.
|
|
|
|
|
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
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
Queens, New York
Online
Edison Member
Karma: 31
Posts: 1733
"Of all the things I've ever lost, I miss my mind the most" -Ozzy Osbourne
|
 |
« Reply #37 on: December 13, 2012, 08:25:50 am » |
I wasn't sure what you wanted to do with Processing, but now I see. Arduino might be made from processing but it does not have the same functions as processing. One function in particular is Split(). Arduino does not have it, and that make things harder to break apart incoming strings. So what you(everyone in general) have to do is MAKE a split function using IF statements and Case statements. This should do exactly what you want to do, get the incoming data, and convert it to an integer. String inString; int Data;
void setup() { Serial.begin(9600); }
void loop() { if( Serial.available() ) { char val = Serial.read();
if (val == '.'){ // looks for the period in the incoming data then prints it. Serial.print(inString); Serial.print(" :: "); Serial.println(Data); inString = ""; } else { inString += val; // stores the characters in a string Data=inString.toInt(); // converts the string into an integer } } }
Not compiled but it should work. Make sure when you send out the data, you include a period at the end, otherwise it will continue to add to the string and never print. Also I gave you a simple Split function in a previous post, did you analyze it.
|
|
|
|
« Last Edit: December 13, 2012, 11:49:46 am 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
"If your doing nothing, it does not mean your lazy, it just means your open for anything that suits you" - Unknown
|
|
|
|
|