Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ModbusRtu.h>
- uint16_t au16data[16]; //!< data array for modbus network sharing Array
- uint8_t u8state; //!< machine state
- uint8_t u8query; //!< pointer to message query
- Modbus master(0,0,2); // this is master and RS-232 or USB-FTDI /// 7 = PIN ENEABLE rs485
- modbus_t telegram[0];
- unsigned long u32wait;
- const int switchOnePin = 3; // digital in 2 (pin the switch one is attached to)
- const int switchTwoPin = 4; // digital in 3 (pin the switch two is attached to)
- const int switchThreePin = 5; // digital in 4 (pin the switch three is attached to)
- const int LED1 = 8; // digital in 2 (pin the switch one is attached to)
- const int LED2 = 9; // digital in 3 (pin the switch two is attached to)
- const int LED3 = 10; // digital in 4 (pin the switch three is attached to)
- int switchOneState = 0; // current state of the switch
- int lastSwitchOneState = 0; // previous state of the switch
- int switchTwoState = 0;
- int lastSwitchTwoState = 0;
- int switchThreeState = 0;
- int lastSwitchThreeState = 0;
- void setup() {
- master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
- u32wait = millis() + 1000;
- u8state = u8query = 0;
- //initialize serial communication at 9600 bits per second:
- Serial.begin(9600);
- int switchOneState = 0; // current state of the switch
- int lastSwitchOneState = 0; // previous state of the switch switch pins as an input
- pinMode(switchOnePin, INPUT);
- pinMode(switchTwoPin, INPUT);
- pinMode(switchThreePin, INPUT);
- pinMode(LED1, OUTPUT);
- pinMode(LED2, OUTPUT);
- pinMode(LED3, OUTPUT);
- }
- void loop() {
- master.poll(); // check incoming messages
- if (master.getState() == COM_IDLE) {
- u32wait = millis() + 100;
- // read the switch input pins:
- switchOneState = digitalRead(switchOnePin);
- switchTwoState = digitalRead(switchTwoPin);
- switchThreeState = digitalRead(switchThreePin);
- // compare the switchState to its previous state
- if (switchOneState != lastSwitchOneState) {
- // if the state has changed, increment the counter
- if (switchOneState == HIGH) {
- // if the current state is HIGH then the button
- // went from off to on:
- //digitalWrite(LED1, HIGH);
- //Serial.println("Switch one is on");
- telegram[0].u8id = 1; // slave address
- telegram[0].u8fct = 5; // function code (write Single coil)
- telegram[0].u16RegAdd = 0; // start address in slave - direccion de Inicio 0
- telegram[0].u16CoilsNo = 2; // number of elements (coils or registers) to read 0 - 16
- master.query( telegram[0] ); // send query (only once)
- } else {
- if (switchOneState == LOW)
- // if the current state is LOW then the button
- // went from on to off:
- //Serial.println("Switch one is off");
- digitalWrite(LED1, LOW);
- }
- }
- if (switchTwoState != lastSwitchTwoState) {
- if (switchTwoState == HIGH) {
- //Serial.println("Switch two is on");
- //digitalWrite(LED2, HIGH);
- // telegram 1: write a multiple register = function 16
- telegram[1].u8id = 1; // slave address
- telegram[1].u8fct = 6; // function code (write multiple registers 16 )
- telegram[1].u16RegAdd = 1; // start address in slave - direccion de Inicio 10
- telegram[1].u16CoilsNo = 4; // number of elements (coils or registers) to read
- // telegram[1].au16reg[0] = 0;
- master.query( telegram[1] ); // send query (only once)
- } else {
- //Serial.println("Switch two is off");
- //digitalWrite(LED2, LOW);
- }
- }
- if (switchThreeState != lastSwitchThreeState) {
- if (switchThreeState == HIGH) {
- //Serial.println("Switch three is on");
- //digitalWrite(LED3, HIGH);
- telegram[2].u8id = 1; // slave address
- telegram[2].u8fct = 5; // function code (this one is registers read)
- telegram[2].u16RegAdd = 0; // start address in slave
- telegram[2].u16CoilsNo = 0; // number of elements (coils or registers) to read
- // telegram.au16reg = au16data; // pointer to a memory array in the Arduino
- master.query( telegram[3] ); // send query (only once)
- } else {
- //Serial.println("Switch thre is off");
- //digitalWrite(LED3, LOW);
- }
- }
- // Delay a little bit to avoid bouncing
- delay(50);
- // save the current state as the last state,
- //for next time through the loop
- lastSwitchOneState = switchOneState;
- lastSwitchTwoState = switchTwoState;
- lastSwitchThreeState = switchThreeState;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement