I have a project to make about controlling a small train model wirelessly like sending PWM and HIGH/LOW orders to it. I do have UART wireless modules which work by serial communication, I got it working on PICs as I have a little experience in C language and it works by wires the same it works by the modules. Two codes, one transmitting and the other for receiving.
I tried to get working on Arduino Unos but with no luck, it seems like I can't write the proper code. I just need a sample code to send HIGH/LOW states from on Arduino to another serially.
I just need a sample code to send HIGH/LOW states from on Arduino to another serially.
What have you tried? HIGH/LOW states of what? You send information over the serial port, just like you were sending it to the Serial Monitor. If you want to send information to the effect that pin 8 was HIGH, you might send "<8, 1>".
I just need a sample code to send HIGH/LOW states from on Arduino to another serially.
What have you tried? HIGH/LOW states of what? You send information over the serial port, just like you were sending it to the Serial Monitor. If you want to send information to the effect that pin 8 was HIGH, you might send "<8, 1>".
I tried these codes:
Transmitter:
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
// turn LED on:
digitalWrite(ledPin, HIGH);
Serial.print("a" );
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
Serial.print("b" );
}
delay(2);
}
Receiver:
const int ledPin = 13;
char x;
void setup() {
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
x=Serial.read();
if (x=='a') {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW); ;
}
delay(2);
}
I'm sure there is something wrong in them, but I can't figure it out.
gharryh:
Is the connection on the hardware side correct?
Arduino1 TX goes to Arduino2 RX vise versa as you are talking on connecting two arduino's
x=Serial.read();
if (x=='a') {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW); ;
}
If there are no characters (which will be just about every time this code runs) x will == -1, therefore you will always execute the else block.
Even when you finally get an 'a' the LED will only light until the next loop, about 50uS later. With the right tools you would see than, but the eye won't.
Look up Serial.available(), and also don't have an else, test for 'b'
if (x=='a') {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW); ;
}
If there are no characters (which will be just about every time this code runs) x will == -1, therefore you will always execute the else block.
Even when you finally get an 'a' the LED will only light until the next loop, about 50uS later. With the right tools you would see than, but the eye won't.
Look up Serial.available(), and also don't have an else, test for 'b'
______
Rob
I will try that and see.
PaulS:
Are the grounds of the two Arduinos connected?
No, that is the point I think, I need them to be away from each other and control them remotely, so no wires are connected between them, the same I did with PICs.
EDIT: I connected both grounds and it worked, but this won't help me in my project, can I get it to work without connecting the grounds?
can I get it to work without connecting the grounds?
No. If the Arduinos can be connected by wires from Rx to Tx and from Tx to Rx, connecting the grounds shouldn't be a problem. You'll need to explain why two wires is OK, but three wires is not.
I'm confused; are you testing your code with or without the wireless modules? If you're testing without them to check your code then you will need to connect RX, TX and ground between the arduinos. If the wireless modules are in place, then of course, no connection between arduinos is required.
can I get it to work without connecting the grounds?
No. If the Arduinos can be connected by wires from Rx to Tx and from Tx to Rx, connecting the grounds shouldn't be a problem. You'll need to explain why two wires is OK, but three wires is not.
The two wires are just for testing, I said that I'm gonna connect them by UART wireless modules which work the same way the wires do. That's why I don't want the ground connected.
wildbill:
I'm confused; are you testing your code with or without the wireless modules? If you're testing without them to check your code then you will need to connect RX, TX and ground between the arduinos. If the wireless modules are in place, then of course, no connection between arduinos is required.
I'm testing with wires for now, I will try with the modules and see what happens with no wires attached.