Go Down

Topic: Slow sensor activation over RS485 network (Read 104 times) previous topic - next topic

Eddy99

I have setup model railway train control as in the picture attached.
Servos respond with no delay but the sensors attached to the reed switches are very slow to report their state.
If i pass a magnet over the reed slowly it activates and reports to CMRI active but if I pass over the reed at speed the reed activates but is not reported back.
I have upped the baud rate to 19200 but it did not make much difference.
Any help pse

Eddy

Code: [Select]
/**
 *A6-A7 not used  analogue inputs
 *17 sensor inputs for CMRI.
 *Set cmri(num) as the nano number.
 */

#include <Auto485.h>
#include <CMRI.h>


Auto485 bus(2); // Arduino pin 2 -> MAX485 DE and RE pins
CMRI cmri(2, 24, 48, bus); // sets up an SMINI with address 0. SMINI = 24 inputs, 48 outputs

void setup() {
  pinMode (13, INPUT);
  bus.begin(19200, SERIAL_8N2); // open the RS485 bus at 9600bps
 for (int i=3; i<=13; i++) { pinMode(i, INPUT); digitalWrite(i, HIGH); }
 for (int i=A0; i<=A5; i++) { pinMode(i, INPUT); digitalWrite(i, HIGH); }
}

void loop() {
  cmri.process(); //Starts CMRI process
  
  cmri.set_bit(0, !digitalRead(3)); //Reads input pins from reed switches. When pulled low by reed
  cmri.set_bit(1, !digitalRead(4)); //switches, bit sent to change sensor status in CMRI.
  cmri.set_bit(2, !digitalRead(5));
  cmri.set_bit(3, !digitalRead(6));
  cmri.set_bit(4, !digitalRead(7));
  cmri.set_bit(5, !digitalRead(8));
  cmri.set_bit(6, !digitalRead(9));
  cmri.set_bit(7, !digitalRead(10));
  cmri.set_bit(8, !digitalRead(11));
  cmri.set_bit(9, !digitalRead(12));
  cmri.set_bit(10, !digitalRead(A0));
  cmri.set_bit(11, !digitalRead(A1));
  cmri.set_bit(12, !digitalRead(A2));
  cmri.set_bit(13, !digitalRead(A3));
  cmri.set_bit(14, !digitalRead(A4));
  cmri.set_bit(15, !digitalRead(A5));
  cmri.set_bit(16, !digitalRead(13));
}

MarkT

#1
Aug 01, 2020, 02:47 pm Last Edit: Aug 01, 2020, 02:49 pm by MarkT
The CMRI library only reports back when polled, so its missing short lived events.

You need to delay the clearing of those bits for long enough they get polled reliably.

Perhaps the other end should be polling more rapidly anyway?

I'd suggest setting the bits when you detect the switch change state to on, and storing millis()
in an array indexed by input number.

When the timestamp is sufficiently out of date in that array, clear the bit.

Your comment "//Starts CMRI process" is wrong.  the process() method in the library checks for
incoming command bytes, such as the poll command.  If it sees a poll it transmits back the state.

Its always worth (speed)reading the code of any library you use...

And you might want to change setup() to use INPUT_PULLUP in its pinModes, much more
readable.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Eddy99

Thanks Mark, your talking to a newbie with limited experience.
I got half of what you told me but some I did not get.
The most important bit I did not get was.


Quote
I'd suggest setting the bits when you detect the switch change state to on, and storing millis()
in an array indexed by input number.
Can you elaborate for me pse?

Thanks Eddy

MarkT

Have a look at StateChangeDetection example.

Here you want to notice when a switch becomes active and record the timestamp,
the values of millis(), at that point, as you set the bit in the library.

Rather than clear the bit immediately the switch releases, you wait till enough time since
the timestamp was taken has gone by, and only clear the librarie's bit then, hoping that
its been polled by that point.

Look at the BlinkWithoutDelay example for timestamps and waiting for timeouts like this.
[ I will NOT respond to personal messages, I WILL delete them, use the forum please ]

Go Up