I have an Aruduino Mega and Duemilanove. I want each to communicate to each other. Can I have the Serial TX on the Mega send to the Serial RX on the Duemilanove?
I know that Serial.read will work on the Duemilanove to receive info from the Mega, but how do I send info from the Mega?
I am not quite getting the outcome I was expecting. I think I might be using the print function incorrectly.
Mega code
int speedOfWheels;
int i = 0;
void setup() {
speedOfWheels = 0;
pinMode(0,INPUT);
pinMode(1,OUTPUT);
Serial.begin(9600);
Serial1.begin(9600);
}
void loop() {
Serial.print("doesn't matter");//send data to Duemilanove
if (Serial.available() > 0 && i == 0) {
speedOfWheels = Serial.read(); //get data from Duemilanove
Serial1.println(speedOfWheels);
}
i++;
}
Duemilanove code
#include <Wheel.h>
Wheel wheels;
void setup() {
pinMode(0,INPUT);
pinMode(1,OUTPUT);
Serial.begin(9600);
}
void loop() {
wheels.reverse(124);
delay(40);
if (Serial.available() > 0)//if data sent from Mega
Serial.write(wheels.getSpeed());//send data back to Mega
}
Also in your duemielnove code you never read the data you just check if it is available, that way the buffer will not be emptied, and there will always be data if the mega sent data just once.
Connecting the grounds together is very important.
The Mega sends "Doesn't Matter!" over and over to the Nove as fast as it can. The Nove doesn't process this character string sequence, and the Nove's input buffer is always going to be full.
You've also defined a second Serial1 on the Mega which I'm thinking will also just Tx/Rx on the same HW UART pins, although it's the Mega so maybe it has a few extra HW UARTS? Can you clarify what's hooked to the Nove and if one is meant to be a diagnostic to the computer?
On the Mega, since you are qualifying this if (Serial.available() > 0 && i == 0) with i==0, and i only equals 0 on init, and every time it wraps, this code will seldom be true. Therefor your Mega's input buffer is always going to be full as well. Well, the 40ms delay on the Nove might help this, but you could certainly fix up the Mega read loop to occur more often.
If you are not going to try to slow down the communication rate back and fourth, you'll need to run Serial.flush(); after your Serial.read(); on both the Mega and Nove. This will clear the full buffer.
Serial.print() or Serial.write() will work, it just depends on what kind of data you are trying to send. Look up the methods and experiment.
You've got some work to do... just try to get the data flowing back and forth smoothly, without too much delay building up on either side (unless you employ a technique to get around the delayed response, like flushing the buffer, or reading all the bytes in the buffer quick to get to the first one sent)
What's your project? Maybe we can make some suggestions that are more than just debugging, that would help you future-proof your journey.
Well I just wanted to see if I can get some communication between the two. I am just messing around mostly, trying to learn the capabilities of the arduino. I changed some of my code. I am just getting some weird symbols.
Duemilanove Code
#include <Wheel.h>
Wheel wheels;
char input[20];
void setup() {
// set all the other pins you're using as outputs:
pinMode(0,INPUT);
pinMode(1,OUTPUT);
Serial.begin(9600);
}
void loop() {
//turn led on to signal car is in operation
wheels.reverse(124);
delay(40);
int i = 0;
while (Serial.available() > 0) {
input[i] = Serial.read();
i++;
}
Serial.write(wheels.getSpeed());
}
In the Mega, I have Serial1.print(); at the first line. I thought this will send a message to the Nove. Than the Nove will pick it up with Serial.read()
Your Mega is hooked to the PC. Upon reset, it continuously prints "speedspeedspeedspeed" etc.. to your serial monitor.
You are trying to send a BYTE value from the PC to the Mega, which if the Mega receives the BYTE, it will transfer it to the Nove. Part of the problem may in fact be that you are sending the newline character to the Nove as well. Use Serial1.print( Serial.read() ); instead of the println version.
You are sending ASCII from the PC, so a space ' ' = 0x20 in HEX, and 'z' = 0x7A in HEX. It's kind of hard to type 0x00 through 0xFF when you are typing on your keyboard. I haven't found a reliable way to do it using ALT+DECIMAL VAL either, like ALT+248 for degree symbol '°'. You would eventually need to create a command sequence, or come up with a way to parse the data on the Nove, and send stuff like "SPD,00" through "SPD,FF" or "SPD,000" through "SPD,255".
Now on the Nove side, it's not clear what the input array is being used for, and you'll need to check the bounds on your index variable so you don't index off into LALA land in RAM. Whatever index goes to though, supposedly to control wheel speed?, the Serial.write(wheels.getSpeed()); should be Serial.print(wheels.getSpeed(),HEX); Then on the Mega side, instead of printing "speedspeedspeed" to the PC, how about: while (Serial1.available() > 0) {
*Serial.print("Speed: "); * Serial.println(Serial1.read(),DEC); //Should look like //Speed: 000 //Speed: 001 // ... //Speed: 254 //Speed: 255 }
I was trying to send a value to the Nove, have the Nove get a value and return it to the Mega. The Serial1.print is to send the initial value to the Nove, not the serial monitor.
Serial is hardwired to PC
Serial1 is one of the OTHER UARTS that should be hooked to your Nove
Arduino Mega example:
// Arduino Mega using all four of its Serial ports
// (Serial, Serial1, Serial2, Serial3),
// with different baud rates:
void setup(){
Serial.begin(9600);
Serial1.begin(38400);
Serial2.begin(19200);
Serial3.begin(4800);
Serial.println("Hello Computer");
Serial1.println("Hello Serial 1");
Serial2.println("Hello Serial 2");
Serial3.println("Hello Serial 3");
}
void loop() {}