1
0
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:
Harald Barth 2020-10-26 09:10:45 +01:00
parent ecd176042e
commit 9149bc0ee2
2 changed files with 22 additions and 10 deletions

View File

@ -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;
if(!readingSensor->active && readingSensor->signal<0.5){ bool sensorstate = digitalRead(readingSensor->data.pin);
readingSensor->active=true;
if (stream != NULL) StringFormatter::send(stream, F("<Q %d>"), readingSensor->data.snum); if (!sensorstate == readingSensor->active) { // active==true means sensorstate=0/false so sensor unchanged
} else if(readingSensor->active && readingSensor->signal>0.9){ // no change
readingSensor->active=false; if (readingSensor->latchdelay != 0) {
if (stream != NULL) StringFormatter::send(stream, F("<q %d>"), readingSensor->data.snum); // enable if you want to debug contact jitter
//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

View File

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