Good evening,
I use this code to control modbus control
I use the digital outputs to control 6 on-board relays and analog inputs to check the status of relay relay-controlled relays to know their status.
I do not understand why when I pass an analog input into codesys i go high two inputs at a time.
I do not know if I made the problem clear but in case you could give me a hand?
Thank you, Alberto
\#include<Controllino.h>Â /* Usage of CONTROLLINO library allows you to use CONTROLLINO_xx aliases in your sketch. */
\#include<ModbusRtu.h>
\#defineIDÂ Â 2Modbusslave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDIint8_tstate=0;unsignedlongtempus;//dataarrayformodbusnetworksharinguint16_tau16data[9];voidsetup() {
 io_setup(); // I/O settings
 //startcommunication
 slave.begin(9600);
}
voidloop() {
 //pollmessages
 state=slave.poll(au16data, 9);
 io_poll();
}
voidio_setup() {
 pinMode(CONTROLLINO_D0, OUTPUT);
 pinMode(CONTROLLINO_D1, OUTPUT); // note that we are using CONTROLLINO aliases for the digital outputs
 pinMode(CONTROLLINO_D2, OUTPUT);
 pinMode(CONTROLLINO_D3, OUTPUT); // the alias is always like CONTROLLINO_
 pinMode(CONTROLLINO_D4, OUTPUT); // and the digital output label as you can see at the CONTROLLINO device
 pinMode(CONTROLLINO_D5, OUTPUT); // next to the digital output screw terminal
 pinMode(CONTROLLINO_A0, INPUT); // next to the digital output screw terminal
 pinMode(CONTROLLINO_A1, INPUT); // next to the digital output screw terminal
 pinMode(CONTROLLINO_A2, INPUT); // next to the digital output screw terminal
 pinMode(CONTROLLINO_A3, INPUT); // next to the digital output screw terminal
 pinMode(CONTROLLINO_A4, INPUT); // next to the digital output screw terminal
 pinMode(CONTROLLINO_A5, INPUT); // next to the digital output screw terminal
}
voidio_poll() {
 //setdigitaloutputs->au16data[1]
 digitalWrite(CONTROLLINO_D0, bitRead(au16data[1], 0));
 digitalWrite(CONTROLLINO_D1, bitRead(au16data[1], 1));
 digitalWrite(CONTROLLINO_D2, bitRead(au16data[1], 2));
 digitalWrite(CONTROLLINO_D3, bitRead(au16data[1], 3));
 digitalWrite(CONTROLLINO_D4, bitRead(au16data[1], 4));
 digitalWrite(CONTROLLINO_D5, bitRead(au16data[1], 5));
 //getdigitalinputs->au16data[0]
 bitWrite(au16data[0], 0, analogRead(CONTROLLINO_A0));
 bitWrite(au16data[0], 1, analogRead(CONTROLLINO_A1));
 bitWrite(au16data[0], 2, analogRead(CONTROLLINO_A2));
 bitWrite(au16data[0], 3, analogRead(CONTROLLINO_A3));
 bitWrite(au16data[0], 4, analogRead(CONTROLLINO_A4));
 bitWrite(au16data[0], 5, analogRead(CONTROLLINO_A5));
 //diagnosecommunication
 //au16data[6] =slave.getInCnt();
 //au16data[7] =slave.getOutCnt();
 //au16data[8] =slave.getErrCnt();
}
Don't know anything about this but I'll take a guess:
Any chance it has to do with passing an INT value int a bit when you do bitWrite( au16data[0], 0, analogRead(CONTROLLINO_A0));
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi Comingback4u,
You've caught the problem !!
If I put to 1 input A0 I also activate bit 1
If I put to 1 A1 and I put to 0 A0 I activates bit 1 and bit 2 and so on.
In the picture I put A0 to 1 !!
How can I solve on Arduino code?
Thank you, Alberto
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Good evening,
I use this code to control modbus control
I use the digital outputs to control 6 on-board relays and analog inputs to check the status of relay relay-controlled relays to know their status.
I do not understand why when I pass an analog input into codesys i go high two inputs at a time.
I do not know if I made the problem clear but in case you could give me a hand?
Thank you, Alberto
Related
Talk.ru: 1
Talk.ru: 7
Talk.ru: 8
Don't know anything about this but I'll take a guess:
Any chance it has to do with passing an INT value int a bit when you do bitWrite( au16data[0], 0, analogRead(CONTROLLINO_A0));
Hi Comingback4u,
You've caught the problem !!
If I put to 1 input A0 I also activate bit 1
If I put to 1 A1 and I put to 0 A0 I activates bit 1 and bit 2 and so on.
In the picture I put A0 to 1 !!
How can I solve on Arduino code?
Thank you, Alberto
Solved !!
I hve onlychanged analogRead with digitalRead
Thanks for advice.
Alberto
Glad I could help
Glad you got it working