I have gotten Arduino to Arduino communication setup. However I am getting inconsistent values when printing the incoming master data. So when I press a button on the master it sends a 3 to the slave, then on the slave it prints the incoming value. When I look at the slaves serial monitor the data looks like 230,254,254,230,230,254. with no pattern, sometime its 254 some times its 230.
Any ideas? Sketches are below //master
int button = 10; //push button
void setup()
{
Serial.begin(9600);
pinMode(left, INPUT);
digitalWrite(left, HIGH);
}
void loop()
{
int buttonOn = digitalRead(button);
if (buttonOn == 0) //when button is pushed
{
Serial.write("3"); //send 3 to the slave
}
}
\
//slave
void setup()
{
Serial.begin(9600);
}
void loop()
{
if(Serial.available() >0)
{
int input = Serial.read(); //listen for master input
Serial.println(input); //print received data
}
}
Is the 3 still in double quotes, if so, you want to receive a char, so you need to send a char. Simply put, you need to use single quotes not double quotes.
Serial.print( '3' );
If you still get garbage, check your wiring. BT: Tx -> Ard: Rx and BT: Rx -> Ard: Tx
This needs to be done for both master and slave.
You can't have the Arduino to Arduino serial on the same pins that one of the Arduinos uses to Serial Monitor. If you try, you only make something that steps on itself.
What you need to do is set up a separate serial link for every serial connection, like PC to Arduino to Arduino to Bluetooth needs 3 separate serial channels to accomplish.
With UNO's you can use SoftwareSerial to make serial links besides the USB cable link. With MEGA you have 3 extra hardware serial ports. SoftwareSerial is one of the standard Arduino libraries that comes with your IDE.
Is the 3 still in double quotes, if so, you want to receive a char, so you need to send a char. Simply put, you need to use single quotes not double quotes.
Serial.print( '3' );
If you still get garbage, check your wiring. BT: Tx -> Ard: Rx and BT: Rx -> Ard: Tx
This needs to be done for both master and slave.
Yeah, that's how I have it. Not working still. My wiring is correct. Bluetooth Tx to Arduino Rx and Bluetooth Rx Arduino Tx
Works fine if I use upper case letters. Numbers and lowercase leters don't. I think it has something to with the bluetooth modems.
How is that wired?
You can't have the Arduino to Arduino serial on the same pins that one of the Arduinos uses to Serial Monitor. If you try, you only make something that steps on itself.
What you need to do is set up a separate serial link for every serial connection, like PC to Arduino to Arduino to Bluetooth needs 3 separate serial channels to accomplish.
With UNO's you can use SoftwareSerial to make serial links besides the USB cable link. With MEGA you have 3 extra hardware serial ports. SoftwareSerial is one of the standard Arduino libraries that comes with your IDE.
Well I have one Arduino that acts as the master and is not plugged into the computer when testing and it sends data to the slave. The slave is connected to the PC via USB and connected to the master bluetooth.
Are you saying I need a software serial set up for the serial monitor and one for the BT?
For the type of serial you are using a serial channel should only have two ends that nothing else uses those pins.
It would be best if you leave RX and TX for the USB chip and cable for use or test/debug and set up a separate channel/set of pins for each other link.
You have Slave with channel to PC, channel to BT and channel to Master Arduino? Then the Slave should have 3 serial channels whether hardware or software serial and only the one to the PC uses RX/TX. The Master needs one channel for test/bebug and another for channel to Slave.
You can't have the Arduino to Arduino serial on the same pins that one of the Arduinos uses to Serial Monitor.
Sure you can. I've used the below test code on two arduinos appropriately connected tx/rx/gnd. What is typed in the master serial monitor and sent to the master is echoed back to the master serial monitor and also appears on the slave serial monitor. To make the slave operate the same way to the master, put a diode in the slave tx line with the diode band toward the slave tx pin.
//zoomkat 3-5-12 simple delimited ',' string tx/rx
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin.
//Connect the arduino grounds together.
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.print(readString); //prints string to serial port out
Serial.println(','); //prints delimiting ","
//do stuff with the captured readString
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
It is limited and it is more trouble than it's worth. Same as using String Objects on an UNO.
zoomkat:
You can't have the Arduino to Arduino serial on the same pins that one of the Arduinos uses to Serial Monitor.
Sure you can. I've used the below test code on two arduinos appropriately connected tx/rx/gnd. What is typed in the master serial monitor and sent to the master is echoed back to the master serial monitor and also appears on the slave serial monitor. To make the slave operate the same way to the master, put a diode in the slave tx line with the diode band toward the slave tx pin.
//from serial port input (via serial monitor)
//and print result out serial port
//Connect the sending arduino rx pin to the receiving arduino rx pin.
//Connect the arduino grounds together.
//What is sent to the tx arduino is received on the rx arduino.
//Open serial monitor on both arduinos to test
String readString;
void setup() {
Serial.begin(9600);
Serial.println("serial delimit test 1.0"); // so I can keep track of what is loaded
}
void loop() {
//expect a string like wer,qwe rty,123 456,hyre kjhg,
//or like hello world,who are you?,bye!,
if (Serial.available()) {
char c = Serial.read(); //gets one byte from serial buffer
if (c == ',') {
if (readString.length() >1) {
Serial.print(readString); //prints string to serial port out
Serial.println(','); //prints delimiting ","
//do stuff with the captured readString
readString=""; //clears variable for new input
}
}
else {
readString += c; //makes the string readString
}
}
}
Because coordinating use of a serial channel is easier than using separate channels?
Only in special case apps. Otherwise more trouble than it's worth.
As for Strings... you have been shown again and again about those and Arduino.
Yes, there are BAD practices that can be gotten away with. And then comes the help needed posts where as usual part of the fix is an OP UN-learning stupid/wasteful/bad habits before they can write their non-trivial projects.
Damage control? Yeah, to try and cut the HOURS WASTED over stupid/wasteful/bad practices.
GoForSmoke:
Because coordinating use of a serial channel is easier than using separate channels?
Only in special case apps. Otherwise more trouble than it's worth.
As for Strings... you have been shown again and again about those and Arduino.
Yes, there are BAD practices that can be gotten away with. And then comes the help needed posts where as usual part of the fix is an OP UN-learning stupid/wasteful/bad habits before they can write their non-trivial projects.
Damage control? Yeah, to try and cut the HOURS WASTED over stupid/wasteful/bad practices.
You put out inaccurate information. I corrected the misinformation.
I can try setting the Bluetooth on its own serial channel, but I don't think that will matter as I have problems when it connected Bluetooth to Bluetooth and not plugged into the PC. Its worth a shot, though.
As for the numbers on the monitor being inconsistent I seems to read it as a 0 sometimes and that's why I get those random 254's. So I need to either figure you how to filter out the 0's or figure our how to stop it from sending a 0.
Single quotes go around 1 text char and the compiler uses the ASCII value of the text char.
char txt = 'A'; // txt will == 65
txt += 4; // txt == 69, would print as E
or
num = Serial.read();
if (( num >= '0' ) && ( num <= '9' )) // then num is a digit and not something else sent by mistake
{
// do something number-y
}
Double quotes go around string constants and always get a zero added at the end.
char *msg = "this is a message"; // allocates 18 bytes RAM and fills it with the text and a terminating NULL byte.
char buffer[ 32 ];
....
strcpy( buffer, msg ); // now buffer holds the string msg with the unfilled chars unchanged, they started as 0's.
....
strcpy( buffer, "foo" ); // now buffer holds foo\0 is a message\0\0\0\0 etc where \0 is a char == 0.
These are just ways to tell the compiler what to do with the text in your readable, not always numbers source code.
As for print and write, those are well covered here in Serial:
This is the Examples page. The examples code for your IDE release comes with your IDE and load through the IDE File menu, File->Examples->etc, you'll see, go through 1,2,3 and 5. Section 4 teaches bad habits so avoid it!
At the top of the Examples page are links to Foundations and Hacking. Both are very good pages to look through.
There's more on the Arduino site and tons more on the web. Search is your friend!
Keep your bookmarks organized and use tabs in your browser, it will make life easier..