Hi All
I have an issue with Serial.print while using RS485
When I want to use any Serial.print for debugging in the code the signal to RS485 start lagging or totally gone.
Here is a code
#include <ModbusMaster.h>
//RS485 //DI TX0 //DE pin3 //RE pin2//R0 RX0
//ENCODER
//RS485
#define MAX485_DE 3
#define MAX485_RE_NEG 2
// instantiate ModbusMaster object
ModbusMaster node;
void preTransmission() {digitalWrite(MAX485_RE_NEG, 1);digitalWrite(MAX485_DE, 1);}
void postTransmission() {digitalWrite(MAX485_RE_NEG, 0);digitalWrite(MAX485_DE, 0);}
uint8_t result,j;
uint16_t data[6];
//RS485
volatile unsigned int temp; //this variable will increase or decrease depending on the rotation of encoder
int counter = 0; // initial point of rotary encoder
int candleLenght = 1500; // Lenght -> signal for cutting candle
int RELAY1 = 8; // relay for something else
int x; // var for operate RELAY1 on/off
boolean runMotorOneTime = false; //for running motor once
void setup() {
//Encoder
pinMode(21, INPUT_PULLUP); // internal pullup input pin 21
pinMode(20, INPUT_PULLUP); // internal pullup input pin 20
//Setting up interrupt A rising pulse from encodenren activated ai0(). AttachInterrupt 2 is DigitalPin nr 20.
attachInterrupt(2, ai0, RISING); //0 & 1 taken by RS485
//B rising pulse from encodenren activated ai1(). AttachInterrupt 3 is DigitalPin nr 21.
attachInterrupt(3, ai1, RISING); //0 & 1 taken by RS485
//Encoder
//RS 485
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
//RS 485
Serial.begin (9600);
pinMode(RELAY1, OUTPUT);
digitalWrite(RELAY1, HIGH); //RELAY 1 is OFF
// Modbus slave ID 1
node.begin(1, Serial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
writeMotorParams();
delay(100);
readMotorParams();
delay(100);
}
void loop() {
runMotor();
//value of counter from encoder
if( counter != temp ){
temp = counter;
//Serial.println (temp); //<-- if uncomment runMotor() not run at all, serial is buisy
}
}
/*****************************************************************************
******* ANGLE ENCODER COLLECT DATA AND PASS FOR CUTTING SIGNAL *************
*****************************************************************************/
void ai0() {
if (digitalRead(21) == LOW) {
counter++;
if (counter % candleLenght == 0 ) { //activate cutCandle for RUN motor
x = !x;
cutCandle(x);//cut candle RUN motor + var y for relay to do something else
}
} else {
//counter--; //we do not want count anticlockwise
}
}
void ai1() {
if (digitalRead(20) == LOW) {
//counter--; //we do not want count anticlockwise
} else {
counter++;
if (counter % candleLenght == 0 ) { //activate cutCandle for RUN motor
x = !x;
cutCandle(x);//cut candle RUN motor + var y for relay to do something else
}
}
}
/*****************************************************************************
*************************** CUT CANDLE **************************************
*****************************************************************************/
int cutCandle(unsigned int x) {
if (x == 1) {
runMotorOneTime = false;
//digitalWrite(RELAY1, HIGH); //relay for something else OFF
//Serial.println("activate cutting solenoid OFF"); //<--lagging if uncomment
delay(1);
counter = 0;
}
if (x == 0) {
runMotorOneTime = false;
//digitalWrite(RELAY1, LOW); //relay for something else ON
//Serial.println("activate cutting solenoid ON"); //<--lagging if uncomment
delay(1);
counter = 0;
}
}
/*****************************************************************************
*************************** MOTOR DATA **************************************
*****************************************************************************/
void writeMotorParams() { //Setup the motor driver after hard reset
//308 = 0x0134 fan of driver 1off
//901 = 0x0385
//902 = 0x0386
//903 = 0x0387 RPM
result = node.writeSingleRegister(0x0004, 8); delay(4);
result = node.writeSingleRegister(0x0134, 1); delay(4);
result = node.writeSingleRegister(0x0385, 0); delay(4);
result = node.writeSingleRegister(0x0386, 1000); delay(4);
result = node.writeSingleRegister(0x0387, 500); delay(4);
}
void readMotorParams() {
result = node.readHoldingRegisters(0x0386, 1); // Control mode selection
if (result == node.ku8MBSuccess)
{ Serial.println("***SOME PARAMETER : ***" );
for (j = 0; j < 1; j++) {
data[j] = node.getResponseBuffer(j);
Serial.print("Control Mode selection P00.04 => "); Serial.print(j); Serial.println(data[j]);
}
} delay(100);
}
void runMotor(){ //MOTOR ON
if (runMotorOneTime == false){
result = node.writeSingleRegister(0x0384, 1); //When write to siggle register, every time I can see two signs -> ⸮g
//Serial.flush();
runMotorOneTime = true;
//Serial.println("MOTOR => CUT ON"); //<--lagging if uncomment
}
}
Here is my connections
I have inserted comments where is the problem in the code.
Tried to setup
Serial1.begin(9600);
and then use it like
node.begin(1, Serial1);
This dosen't help.
