mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-11-26 17:46:14 +01:00
no float in Sensor no more
This commit is contained in:
parent
ecd176042e
commit
9149bc0ee2
30
Sensors.cpp
30
Sensors.cpp
|
@ -83,15 +83,27 @@ void Sensor::checkAll(Print *stream){
|
||||||
|
|
||||||
if (firstSensor == NULL) return;
|
if (firstSensor == NULL) return;
|
||||||
if (readingSensor == NULL) readingSensor=firstSensor;
|
if (readingSensor == NULL) readingSensor=firstSensor;
|
||||||
readingSensor->signal=readingSensor->signal*(1.0-SENSOR_DECAY)+digitalRead(readingSensor->data.pin)*SENSOR_DECAY;
|
|
||||||
|
bool sensorstate = digitalRead(readingSensor->data.pin);
|
||||||
if(!readingSensor->active && readingSensor->signal<0.5){
|
|
||||||
readingSensor->active=true;
|
if (!sensorstate == readingSensor->active) { // active==true means sensorstate=0/false so sensor unchanged
|
||||||
if (stream != NULL) StringFormatter::send(stream, F("<Q %d>"), readingSensor->data.snum);
|
// no change
|
||||||
} else if(readingSensor->active && readingSensor->signal>0.9){
|
if (readingSensor->latchdelay != 0) {
|
||||||
readingSensor->active=false;
|
// enable if you want to debug contact jitter
|
||||||
if (stream != NULL) StringFormatter::send(stream, F("<q %d>"), readingSensor->data.snum);
|
//if (stream != NULL) StringFormatter::send(stream, F("JITTER %d %d\n"),
|
||||||
|
// readingSensor->latchdelay, readingSensor->data.snum);
|
||||||
|
readingSensor->latchdelay=0; // reset
|
||||||
|
}
|
||||||
|
} else if (readingSensor->latchdelay < 127) { // byte, max 255, good value unknown yet
|
||||||
|
// change but first increase anti-jitter counter
|
||||||
|
readingSensor->latchdelay++;
|
||||||
|
} else {
|
||||||
|
// make the change
|
||||||
|
readingSensor->active = !sensorstate;
|
||||||
|
readingSensor->latchdelay=0; // reset
|
||||||
|
if (stream != NULL) StringFormatter::send(stream, F("<%c %d>"), readingSensor->active ? 'Q' : 'q', readingSensor->data.snum);
|
||||||
}
|
}
|
||||||
|
|
||||||
readingSensor=readingSensor->nextSensor;
|
readingSensor=readingSensor->nextSensor;
|
||||||
} // Sensor::checkAll
|
} // Sensor::checkAll
|
||||||
|
|
||||||
|
@ -131,7 +143,7 @@ Sensor *Sensor::create(int snum, int pin, int pullUp){
|
||||||
tt->data.pin=pin;
|
tt->data.pin=pin;
|
||||||
tt->data.pullUp=(pullUp==0?LOW:HIGH);
|
tt->data.pullUp=(pullUp==0?LOW:HIGH);
|
||||||
tt->active=false;
|
tt->active=false;
|
||||||
tt->signal=1;
|
tt->latchdelay=0;
|
||||||
pinMode(pin,INPUT); // set mode to input
|
pinMode(pin,INPUT); // set mode to input
|
||||||
digitalWrite(pin,pullUp); // don't use Arduino's internal pull-up resistors for external infrared sensors --- each sensor must have its own 1K external pull-up resistor
|
digitalWrite(pin,pullUp); // don't use Arduino's internal pull-up resistors for external infrared sensors --- each sensor must have its own 1K external pull-up resistor
|
||||||
|
|
||||||
|
|
|
@ -34,7 +34,7 @@ struct Sensor{
|
||||||
static Sensor *readingSensor;
|
static Sensor *readingSensor;
|
||||||
SensorData data;
|
SensorData data;
|
||||||
boolean active;
|
boolean active;
|
||||||
float signal;
|
byte latchdelay;
|
||||||
Sensor *nextSensor;
|
Sensor *nextSensor;
|
||||||
static void load();
|
static void load();
|
||||||
static void store();
|
static void store();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user