Advertisement
ektoras2012

Untitled

Jun 9th, 2018
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.77 KB | None | 0 0
  1. #include <ModbusRtu.h>
  2.  
  3. uint16_t au16data[16]; //!< data array for modbus network sharing  Array
  4. uint8_t u8state; //!< machine state
  5. uint8_t u8query; //!< pointer to message query
  6.  
  7. Modbus master(0,0,2); // this is master and RS-232 or USB-FTDI  ///  7 = PIN ENEABLE rs485
  8.  
  9. modbus_t telegram[0];
  10.  
  11. unsigned long u32wait;
  12.  
  13. const int  switchOnePin = 3;    // digital in 2 (pin the switch one is attached to)
  14. const int  switchTwoPin = 4;    // digital in 3 (pin the switch two is attached to)
  15. const int  switchThreePin = 5;  // digital in 4 (pin the switch three is attached to)
  16. const int  LED1 = 8;    // digital in 2 (pin the switch one is attached to)
  17. const int  LED2 = 9;    // digital in 3 (pin the switch two is attached to)
  18. const int  LED3 = 10;  // digital in 4 (pin the switch three is attached to)
  19.  
  20.  
  21. int switchOneState = 0;         // current state of the switch
  22. int lastSwitchOneState = 0;     // previous state of the switch
  23.  
  24. int switchTwoState = 0;
  25. int lastSwitchTwoState = 0;
  26.  
  27. int switchThreeState = 0;
  28. int lastSwitchThreeState = 0;
  29.  
  30.  
  31. void setup() {
  32.  
  33.     master.setTimeOut( 5000 ); // if there is no answer in 5000 ms, roll over
  34.     u32wait = millis() + 1000;
  35.     u8state = u8query = 0;
  36.    
  37.     //initialize serial communication at 9600 bits per second:
  38.     Serial.begin(9600);
  39.  
  40.     int switchOneState = 0;         // current state of the switch
  41.     int lastSwitchOneState = 0;     // previous state of the switch switch pins as an input
  42.     pinMode(switchOnePin, INPUT);
  43.     pinMode(switchTwoPin, INPUT);
  44.     pinMode(switchThreePin, INPUT);
  45.     pinMode(LED1, OUTPUT);
  46.     pinMode(LED2, OUTPUT);
  47.     pinMode(LED3, OUTPUT);
  48. }
  49.  
  50. void loop() {
  51.       master.poll(); // check incoming messages
  52.     if (master.getState() == COM_IDLE) {
  53.        u32wait = millis() + 100;
  54.        
  55.     // read the switch input pins:
  56.     switchOneState   = digitalRead(switchOnePin);
  57.     switchTwoState   = digitalRead(switchTwoPin);
  58.     switchThreeState = digitalRead(switchThreePin);
  59.  
  60.     // compare the switchState to its previous state
  61.     if (switchOneState != lastSwitchOneState) {
  62.         // if the state has changed, increment the counter
  63.         if (switchOneState == HIGH) {
  64.         // if the current state is HIGH then the button
  65.         // went from off to on:
  66.             //digitalWrite(LED1, HIGH);
  67.             //Serial.println("Switch one is on");
  68.            
  69.    telegram[0].u8id = 1;                // slave address
  70.    telegram[0].u8fct = 5;               // function code (write Single coil)
  71.    telegram[0].u16RegAdd = 0;           // start address in slave -  direccion de Inicio 0
  72.    telegram[0].u16CoilsNo = 2;          // number of elements (coils or registers) to read  0 - 16
  73.    master.query( telegram[0] ); // send query (only once)
  74.         } else {
  75.             if (switchOneState == LOW)
  76.             // if the current state is LOW then the button
  77.             // went from on to off:
  78.             //Serial.println("Switch one is off");
  79.             digitalWrite(LED1, LOW);
  80.         }
  81.     }
  82.  
  83.     if (switchTwoState != lastSwitchTwoState) {
  84.         if (switchTwoState == HIGH) {
  85.             //Serial.println("Switch two is on");
  86.             //digitalWrite(LED2, HIGH);
  87.               // telegram 1: write a multiple  register = function 16
  88.   telegram[1].u8id = 1;                 // slave address
  89.   telegram[1].u8fct = 6;               // function code (write multiple registers 16 )
  90.   telegram[1].u16RegAdd = 1;           // start address in slave  -  direccion de Inicio 10
  91.   telegram[1].u16CoilsNo = 4;          // number of elements (coils or registers) to read
  92. //   telegram[1].au16reg[0] = 0;
  93. master.query( telegram[1] ); // send query (only once)
  94.         } else {
  95.             //Serial.println("Switch two is off");
  96.             //digitalWrite(LED2, LOW);
  97.         }
  98.     }
  99.  
  100.     if (switchThreeState != lastSwitchThreeState) {
  101.         if (switchThreeState == HIGH) {
  102.             //Serial.println("Switch three is on");
  103.             //digitalWrite(LED3, HIGH);
  104.     telegram[2].u8id = 1; // slave address
  105.     telegram[2].u8fct = 5; // function code (this one is registers read)
  106.     telegram[2].u16RegAdd = 0; // start address in slave
  107.     telegram[2].u16CoilsNo = 0; // number of elements (coils or registers) to read
  108. //    telegram.au16reg = au16data; // pointer to a memory array in the Arduino
  109. master.query( telegram[3] ); // send query (only once)
  110.         } else {
  111.             //Serial.println("Switch thre is off");
  112.             //digitalWrite(LED3, LOW);
  113.         }
  114.     }
  115.  
  116.     // Delay a little bit to avoid bouncing
  117.     delay(50);
  118.  
  119.     // save the current state as the last state,
  120.     //for next time through the loop
  121.     lastSwitchOneState   = switchOneState;
  122.     lastSwitchTwoState   = switchTwoState;
  123.     lastSwitchThreeState = switchThreeState;
  124.     }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement