Hello everybody,
I'm new on this Forum and I've just started to "play" with Arduino.
I decided to use a MKR ZERO board connected to a MKR RS485 Shield in order to make a very simple RS485 byte transmitter.
Sincerely, I've not found on internet many info about MKR ZERO pinout or examples.
However my question is: how can I convert the MKR ZERO UART to RS485 on MKR RS485 shield?
I'm right if I use MKR ZERO default pins #14 and #13?
Again: into the IDE code, is right to use the Serial1 as into following code example?
Scope of the program is explained into introduction.
/*
First: By pressing Connection button sends on Rs485 a 3 bytes sequence until the remote device doesn't answer the same sequence.
When the remote device answers, the first received byte is the device address.
I use the device address to form the Reset sequence.
By pressing Reset button sends a 3 bytes reset sequence.
*/
// ================= Constants:
const int PinConnectButton = 5; // Connection starting sequence Button
const int PinResetButton = 4; // Reset command Button
const long TxDelayTime = 1000; // Transmission delay time
// ================= Variables:
int ConnectButtonState = 0; // Connection button state
int ResetButtonState = 0; // Reset button state
byte Address = 0x00; // remote device address
bool Connected = false; // connection state
int NumByteRx = 0; // read bytes number
// ================= Arrays:
byte Tx_Message[3]; // transmission buffer
byte Rx_Message[3]; // receiving buffer
void setup() {
pinMode(PinConnectButton, INPUT_PULLUP);
pinMode(PinResetButton, INPUT_PULLUP);
// Serial1 at 9600 baud (default pins 14-TX e 13-RX)
Serial1.begin(9600);
Serial1.setTimeout(1000);
}
void loop() {
// Read "PinConnectButton" state:
ConnectButtonState = digitalRead(PinConnectButton);
// Check if button is pressed:
if (ConnectButtonState == LOW) {
// Reset Address:
Address = 0x00;
// Interrogation sequence and waiting for response
for (byte i = 0x01; i <= 0x18; i++) {
// Preparing message to send:
Tx_Message[0] = {i};
Tx_Message[1] = {0x10};
Tx_Message[2] = {Tx_Message[0] ^ Tx_Message[1]};
// Send message:
Serial1.write(Tx_Message, sizeof(Tx_Message));
// Listening to response:
while (Serial1.available()>0){
NumByteRx = Serial1.readBytes(Rx_Message, 3);
if ((Rx_Message[0] == Tx_Message[0]) && (Rx_Message[1] == Tx_Message[1]) && (Rx_Message[2] == Tx_Message[2])){
Address = Rx_Message[0];
Connected = true;
break;
}
else{
Connected = false;
}
}
// Delay before next send
delay(TxDelayTime);
}
// Read "PinResetButton" state:
ResetButtonState = digitalRead(PinResetButton);
// Check if button is pressed and goes on only if it's connected:
if ((ResetButtonState == LOW) && (Connected == true)) {
// Preparing Reset message to send:
Tx_Message[0] = {Address};
Tx_Message[1] = {0x11};
Tx_Message[2] = {Tx_Message[0] ^ Tx_Message[1]};
// Send Reset message:
Serial1.write(Tx_Message, sizeof(Tx_Message));
delay(500);
}
}
Thank you in advance.