Task: I am trying to build a system to control 5 servoes using Arduino Nano Every and Max485 for communication.
The system has been simulated in Tinkercad with Arduino Uno though and without Max485, and it works without any problem.
In real life the situation is very different.
-
With Max 485 connected I get only response to the first signal sent, after that none of the servoes respond. – I have included a picture which shows the signal as sent from the Arduino master, and the same signal as received at the Slave i.e. the signals are not the signals between the two Max 485 units but the signals between the Nano every’s.
Picture of Signals:
Green is Arduino TX
Blue is Arduino Rx. -
Trying the system without Max 485 and using the TX/RX ports on the Nano every, I get random success, in that sometimes the servoes work but other times I get no response for a long time.
To isolate the issue to network only, I have made a dummy system, with two servoes only and without any buttons etc. and only the timer in the master controls when to run the servos.
The timer is set so they both run each 5 second, but with 2.5 second difference.
The diagram is below.
The code used to handle the communication is borrowed from: hackster.io, with the modification that I have added a line at the end to make sure, that the Rx buffer if fully cleard every time.
********************************
#define Btn1 2
#define Btn2 3
#define Btn3 4
int Btns[4] ={0,Btn1,Btn2,Btn3}; //array til aktivering af buttones
int btnState[4] = {0,0,0,0}; //array til buttonstate element "0" bruges ikke
int btnAction[4] = {0,0,0,0}; //array der indeholder seneste bevægelse ON/OFF
int x =0; //håndterer trykknapper er sat fast til 1 her
int y =0; //håndterer status på skiftespor åben eller lukket.
long Timecheck1 = 5000; //mSekunder = 1 minutter
long LastTime1 = 0;
long Timecheck2 = 5000; //mSekunder = 1 minutter
long LastTime2 = 0;
bool OK = false;
bool Run1 = false;
bool Continue = false;
bool Flag = false;
void setup()
{
Serial.begin(4800);
//Serial.setTimeout(250);
//sæt alle knapper til input
//pinMode(Btns[1],INPUT_PULLUP);
//pinMode(Btns[2],INPUT_PULLUP);
}
void loop()
{
if ((millis()-LastTime1) >Timecheck1)
{OK = HandleEvent(2); // Kører spor 2
LastTime1 = millis();}
if(Flag == false && (millis())>2500){
LastTime2 = millis();
Flag = true;}//Forskyder 2 2,5 sekund
if ((millis()-LastTime2) >Timecheck2)
{OK = HandleEvent(3);//Kører spor 3
LastTime2 = millis();}
}
// Begin new function - ikke i brug
bool TimerCount(int k)//Timeren kan benyttet til flere ting.
{int LastTrigger = 0;
if(millis() - LastTrigger >= k){
return true;}
}
bool HandleEvent(int k)//input = "i" modtagere til test.
{
const int x = 1;//konstant i denne udgave.
int y = btnState[k];
if (x==HIGH && btnState[k] == 0) {
btnState[k] = 1;
Serial.print("I"); //Forsøg kan nok slettes
Serial.print(k);//Sender adresse
Serial.print("O");//sender"O" for open
Serial.print("F");//Slutkarakter
}
else if (x==HIGH && btnState[k] == 1) {
btnState[k] = 0;
Serial.print("I"); //Forsøg kan nok slettes
Serial.print(k);//Sender adresse
Serial.print("C");//sender "C" for lukket.
//Serial.print("F");//Slutkarakter
}
// Serial.println(); //Debug linie
Serial.flush();
return true;
}
//End new function
**********************************
Code Slave (they are identical except for Slave number)
//Slave 2
#include<Servo.h>
//Fjernet alle delays
const int SlaveNumber = 2;
Servo servo1; // create servo object to control a servo
int closed=75; //Skiftesport lukket Kører lige ud
int open=135; //Skiftespor åben = drejer
void setup()
{
servo1.attach(9); //n, min, max)
servo1.write(closed); //Flyt servo til Spor lukket
Serial.begin(4800);
Serial.setTimeout(250);
pinMode(13, OUTPUT);
pinMode(A0, INPUT);
}
void loop()
{
if(Serial.available()>0)
{delay(20);
if(Serial.read()=='I')
{int Slave = Serial.parseInt();
if(Slave == SlaveNumber)
{char command = Serial.read();
char slut = Serial.read();
if(command == 'O' && slut =='F')
{servo1.write(open);
}// Åben skiftespor
else if(command == 'C'){
servo1.write(closed);
delay(200);}
}
}
if (Serial.available()>0){
Serial.read();}//Tømmer serial
}
}
******************************
Any ideas what is going wrong and what I can do about it would be appreciated.
John