Hi guys, the program compiles but it doesn´t do anything, when i push the button the master should send the letter E and the slave should turn the led on, and when the push button is not pressed and the master should be sending the letter A and the led in the slave should be off.
Master
// C++ code
//
const int boton1 = 10;
int estadoboton = 0;
void setup()
{
Serial.begin(4800, SERIAL_8E1);
pinMode(10, INPUT_PULLUP);
}
void loop()
{
estadoboton = digitalRead(boton1);
if (estadoboton == LOW)
{
Serial.write("E");
delay(50);
}
else if (estadoboton == HIGH)
{
Serial.write("A");
delay(50);
}
}
Slave
int incomingByte = 0; // for incoming serial data
void setup() {
pinMode(13, OUTPUT);
Serial.begin(4800, SERIAL_8E1); // opens serial port, sets data rate to 9600 bps
}
void loop() {
// when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();
if (incomingByte=='E'){
// say what you got:
//Serial.println(incomingByte);
Serial.write(incomingByte);
digitalWrite(13, HIGH);
}
else if (incomingByte=='A'){
Serial.write(incomingByte);
digitalWrite(13, LOW);
//Serial.println(incomingByte);
}
}
}
which UNO are you using?
it looks as though you are using the UNO hardware serial port for the RS485 communications which means you don't have a Serial Monitor to print out variables and see the program logic flow
it is probably simpler to use Software Serial to communicate with the RS485 which enables use of the serial monitor for debugging - have a look atSoftwareSerialRS485Example
the OE and RD pins can usually be wired together. the TX (OE) should only be enabled when transmitting. the Serial.flush() returns when transmission is complete and the output can be disabled
i changed Serial.write(incomingByte); for Serial.println(incomingByte); only in the slave, now it works but it gives me the DEC 65 (A) and DEC 69 (E)(i don´t want this, i want to see printing on the serial monitor E and A), also changed ,if (incomingByte=='E'), for if (incomingByte=='(any other word)') and it turns the led even though it doesn´t match the If condition, maybe should i try using char estadoboton; instead of int estadoboton = 0; also trying using serial.parseint. what should i do?
Why Serial.write() instead of Serial.print() can Someone tell me if this can help me, because using Serial.write on the master works but on the slave it doesn't work Serial.write(i want to receive and print the letters A and E on the slave)
If you are wired as per post#1, and if your two Arduinos are not powered by the same source, then you are missing a necessary wire - the serial connection needs a reference. Connect GND of ARD1 to GND of ARD2.
Dear god, not this again.
Please go read the reference documents, there's no need for me to google this for you. Yes, there does need to be a common reference between the nodes on an RS485 network. It may work, sometimes, if the moon phase is right, but there must eventually be a common reference.
See section 9 here:
A later version, Rev. D, waffles on this a bit, but it's clearly concerned with installations that are SO noisy that adding loop isolators becomes the path forward.
Up to you, and I won't debate. There are many, many documents on the web you can point to that only ever show the two signal wires. I prefer the horse's mouth - as in, original design material.
Because the designers futzed up. Plain and simple, they read too many web pages where it wasn't included. It will work for months, then you'll suddenly struggle when you change a power supply, or do something else. I struggled for a couple of weeks with just this problem, when I changed a power supply at one end from one that was grounded, to one that was not grounded.
You can find many versions of those adapters, and some have the third ground pin; most of those will have a chinese character beside the screw, instead of a G or a ground symbol. If you look at the RS485 adapters provided by industrial providers(not cheap Aliblabla junk rerouted through Amazon), the vast preponderance of those will provide some method for establishing that ground. It may be by a screw terminal per link, or it may be via a separate screw terminal, but it'll be there. @gcjr It also may be that you have an indirect ground reference. Is that adapter plugged into a desktop computer chassis, the shell of which is grounded via a 3-pin plug?