Question controllino code

alverman
2017-05-18
2017-05-19
  • alverman - 2017-05-18

    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>
    \#define ID   2
    Modbus slave(ID, 0, 0); // this is slave ID and RS-232 or USB-FTDI
    int8_t state = 0;
    unsigned long tempus;
    // data array for modbus network sharing
    uint16_t au16data[9];
    void setup() {
      io_setup(); // I/O settings
      // start communication
      slave.begin( 9600 );
    }
    void loop() {
      // poll messages
      state = slave.poll( au16data, 9 );
      io_poll();
    } 
    void io_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
    }
    void io_poll() {
      // set digital outputs -> 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 ));
      // get digital inputs -> 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));
      // diagnose communication
      //au16data[6] = slave.getInCnt();
      //au16data[7] = slave.getOutCnt();
      //au16data[8] = slave.getErrCnt();
    }
    
     

    Related

    Talk.ru: 1
    Talk.ru: 7
    Talk.ru: 8

  • Lo5tNet - 2017-05-18

    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));

     
  • alverman - 2017-05-18

    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

    IMG: CSYS_1.png

     
  • alverman - 2017-05-18

    Solved !!
    I hve onlychanged analogRead with digitalRead

    Thanks for advice.

    Alberto

     
  • Lo5tNet - 2017-05-19

    Glad I could help
    Glad you got it working

     

Log in to post a comment.