Reading sensors and modbus

Hi!

I have an Arduino Uno board, with these components:
rs-485 Modbus module shield
Multiprotocol radio shield board for Arduino

In addition I have a level sensor which reads either 1 when it is above water, and 0 when it is below.

I need to be able to give the value of this sensor to a modbus master over rs485.

Cooking Hacks Tutorial
This page has a description of how to use the ModbusMaster485 library, but I can not find any information about the slave library.

Is there anyone who has used these components before, and has an idea of how to get this to work?

landsem, welcome to the forum.

I quickly looked at the library and took the example they have for slave and made some modifications that might suit you better for your needs.

Try the following code:

/*---------------------------------------------------------------------------------
 * Program to sense state of level switch and
 * make state available to a Modbus master.
 *
 */
#include <RS485.h>
#include <ModbusSlave485.h>
#include <SPI.h>

ModbusSlave485 mbs;                         // create new mbs instance:

// Slave registers
enum {
  MB_0,                                     // register 0:
  MB_REGS                                   // leave here for size of array:
};

const int pin_level_swicth = 2;             // pin that level switch is connected to:
int level_switch = 0;
int regs[MB_REGS];

/*---------------------------------------------------------------------------------
 *  void setup()
 *  Initial setup of board:
 */
void setup() {
  pinMode(pin_level_switch, INPUT);         // set Arduino input pin for level switch as input (default):

  Serial.begin(115200);                     // intiate serial port for comms at 115200:

  const unsigned char SLAVE = 1;            // modbus slave configuration parameter:
  const long BAUD = 9600;                   // modbus baud configuration parameter:

  mbs.configure(SLAVE, BAUD);               // instanstiate modbus object:
}

/*---------------------------------------------------------------------------------
 *  void loop()
 *  Mission Control:
 */
void loop() {
  mbs.update(regs, MB_REGS);                // pass current register values to mbs:

  level_switch = digitalRead(pin_level_switch);  // read level switch into variable:
  regs[MB_0] = level_switch;                // place digital input value into Modbus registers:

  delay(20);                                // short delay, but should use time check method instead:
}

Paul