A
A
Alexey2019-07-12 12:19:49
Arduino
Alexey, 2019-07-12 12:19:49

Arduino and ModBusRtu.h how to set up slaves?

faced with the problem of transferring data from several timers to uno + multifunction shield with aliexpress. the first slave clearly transmits the variable to the computer (I use the simpLight scad), so that's the trouble
while the first slave transmits, my time variable ticks on the PC screen only on it, the second slave freezes at the selected time, although it is also running, but if you press pause, then the numbers will be updated (only in a stationary state, in general, it transmits from the 2nd slave) poke your nose please
here is the code
#include TimerOne.h>
#include ModbusRtu.h>
#include Wire.h>
#include MultiFuncShield.h>
#define CLK 5
#define DT 6
#define SW A5
#include "GyverEncoder.h"
#define ID 1 // slave address
#define TxEnablePin_1 9 // RS485 Tx/Rx pin
#define rxPin 0
#define txPin 1
Modbus slave(ID, 1, 9);
Encoder enc1(CLK, DT, SW);
enum CountDownModeValues
​​{
COUNTING_STOPPED,
COUNTING
};
unsigned long millis1,millis2;
// string for receiving data
String inputString0 = "";
// sign of the end of the string
boolean stringComplete9 = false;
longprevmicros = 0;
byte countDownMode = COUNTING_STOPPED;
String secChar; // Variable for converting the numeric value of seconds into a string value.
String minuChar; // Variable for converting the numeric value of minutes into a string value.
byte tenths = 0;
char seconds = 0;
char minutes = 1;
int8_t state = 0; //
uint16_t au16data[11];
unsigned long tempus;
String times2;
int a;
int b = 1;
int intVar;
String stringVar = times2;
void setup() {
tempus = millis() + 100;
pinMode(TxEnablePin_1, OUTPUT);
Timer1.initialize();
MFS.initialize(&Timer1); // initialize multifunction shield library
MFS.write(0);
slave.begin( 115200 );
enc1.setType(TYPE1); // encoder type TYPE1 single step, TYPE2 double step. If your encoder is acting weird, change the type
}
void loop() {
// put your main code here, to run repeatedly:
enc1.tick();
byte btn = MFS.getButton();
if (enc1.isRight()) {delay(100);
minutes++; // if there was a turn to the right, increase by 1
if (minutes > 60)
{
minutes = 0;
}
MFS.write(minutes*100 + seconds);}
if (enc1.isLeft()) {delay(100);
minutes--; // if there was a turn to the right, increase by 1
if (minutes < 0)
{
minutes = 0;
}
MFS.write(minutes*100 + seconds);}
if (btn == BUTTON_3_PRESSED || btn == BUTTON_3_LONG_PRESSED)
{MFS.beep(100, 100, 4); // beep 3 times, 500 milliseconds on / 500 off
minutes += 59;
if (minutes <= 60)
{
minutes = 59;
}
MFS.write(minutes*100 + seconds);
}
switch (countDownMode)
{
case COUNTING_STOPPED:
if (btn == BUTTON_1_SHORT_RELEASE && (minutes + seconds) > 0)
{
// start the timer
countDownMode = COUNTING;
}
else if(enc1.isHold())
{if(digitalRead(SW))
// start the timer
countDownMode = COUNTING;
}
else if (btn == BUTTON_1_LONG_PRESSED)
{
// reset the timer
tenths = 0;
seconds = 0;
minutes = 1;
MFS.write(minutes*100 + seconds);
}
else if (btn == BUTTON_2_PRESSED || btn == BUTTON_2_LONG_PRESSED)
{
minutes++;
if (minutes > 60)
{
minutes = 0;
}
MFS.write(minutes*100 + seconds);
}
else if (btn == BUTTON_3_PRESSED || btn == BUTTON_3_LONG_PRESSED)
{
minutes += 59;
if (minutes <= 60)
{
minutes = 59;
}
MFS.write(minutes*100 + seconds);
}
break;
case COUNTING:
if (btn == BUTTON_1_SHORT_RELEASE || btn == BUTTON_1_LONG_RELEASE)
{
// stop the timer
countDownMode = COUNTING_STOPPED;
}
else
{
// continue counting down
tenths++;
if (tenths == 10)
{
tenths = 0;
seconds--;
if (seconds < 0 && minutes > 0)
{
seconds = 59;
minutes--;
}
if (minutes == 0 && seconds == 0)
{
// timer has reached 0, so sound the alarm
MFS.beep(50, 50, 10); // beep 3 times, 500 milliseconds on / 500 off
countDownMode = COUNTING_STOPPED;
}
MFS.write(minutes*100 + seconds);
}
delay(100);
}
break;
// message processing
} {state = slave.poll( au16data,11);
// if we received a packet without errors, we light the LED for 50 ms
if (state > 4) {
tempus = millis() + 500;
}
if (millis() > tempus)
io_poll();
}
}
void io_setup(){}
void io_poll(){
a=minutes;
b=seconds;
minuChar=a;
secChar=b;
times2=minuChar +secChar;
int intVar;
String stringVar = times2;
intVar=stringVar.toInt();
au16data[1] = intVar;
}

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question