[chronojump] Wifi terminal. Added debounce filter
- From: Xavier Padullés <xpadulles src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [chronojump] Wifi terminal. Added debounce filter
- Date: Wed, 29 Sep 2021 08:28:57 +0000 (UTC)
commit 32815bdb754c04f98dd010b42b3227876d7e81e3
Author: Xavier Padullés <x padulles gmail com>
Date: Wed Sep 29 10:28:12 2021 +0200
Wifi terminal. Added debounce filter
arduino/lightChro/terminal/terminal.ino | 166 +++++++++++++++++++++-----------
1 file changed, 108 insertions(+), 58 deletions(-)
---
diff --git a/arduino/lightChro/terminal/terminal.ino b/arduino/lightChro/terminal/terminal.ino
index b153a2b9a..e1721e11a 100644
--- a/arduino/lightChro/terminal/terminal.ino
+++ b/arduino/lightChro/terminal/terminal.ino
@@ -4,6 +4,7 @@
#include <RF24.h>
#include <printf.h>
#include <MsTimer2.h>
+#include <TimerOne.h>
String version = "Wifi-Sensor-1.11";
//
@@ -20,16 +21,19 @@ String version = "Wifi-Sensor-1.11";
// Set up nRF24L01 radio on SPI bus plus pins (CE & CS)
-// Set up nRF24L01 radio on SPI bus plus pins (CE & CS)
-RF24 radio(10, 9);
+
+//RF24 radio(A3, A4); //Old versions
+RF24 radio(10, 9); //New version
#define red_on digitalWrite(A4,LOW)
#define green_on digitalWrite(A5,LOW)
#define blue_on digitalWrite(A3,LOW)
-#define buzzer_on digitalWrite(7,HIGH)
+//#define buzzer_on digitalWrite(7,HIGH) //Old versions
+#define buzzer_on digitalWrite(A0,HIGH) //New versions
#define red_off digitalWrite(A4,HIGH)
#define green_off digitalWrite(A5,HIGH)
#define blue_off digitalWrite(A3,HIGH)
-#define buzzer_off digitalWrite(7,LOW)
+//#define buzzer_off digitalWrite(7,LOW) //Old versions
+#define buzzer_off digitalWrite(A0,LOW) //New versions
struct instruction_t
{
@@ -64,11 +68,16 @@ struct sample_t
struct sample_t sample = {.state = LOW, .termNum = 0, .elapsedTime = 0};
-int sample_size = sizeof(sample);
+int sample_size = sizeof(sample);
unsigned long time0; //Time when the command is received
bool flagint = LOW; //Interruption flag. Activated when the sensos changes
+volatile bool lastPinState = LOW; //stores the state of the pin 2 before the interruption
+volatile int debounceTime = 1;
+//The timer overflows inmediately after initialization.
+//We are aware only in the second overflow
+volatile int debounceCount = 0; //Number of the timer overflows
// First channel to be used. The 6xswitches control the terminal number and the number to add the
terminal0Channel
// The channel 125 is used to listen from the terminals.
@@ -99,7 +108,7 @@ void setup(void)
Serial.begin(115200);
printf_begin(); //Used by radio.printDetails()
-
+
Serial.println(version);
@@ -107,19 +116,21 @@ void setup(void)
radio.begin();
//Reading each pin to stablish the terminal number and the listening channel
+
+ //¡¡¡¡Atention!!!!, the first version of lightChro the pin7 is associated to the buzzer.
+ //The first versions of the Quick the microswitch was 5xSwitches. The last one associated to the buzzer
+ //In the first versions of the photocells the microswitch was 8xSwitches
+ //Remember to change comment/uncomment depending on the hardware version
pinMode(3, INPUT_PULLUP); //Least significant bit
pinMode(4, INPUT_PULLUP);
pinMode(5, INPUT_PULLUP);
pinMode(6, INPUT_PULLUP);
- pinMode(7, INPUT_PULLUP);
- pinMode(8, INPUT_PULLUP);
+ pinMode(7, INPUT_PULLUP); //Comment in old versions
+ pinMode(8, INPUT_PULLUP); //Most significant bit.
- //¡¡¡¡Atention!!!!, the first version of lightChro the pin7 is associated to the buzzer.
- //The first versions of the Quick the microswitch was 5xSwitches. The las one associated to the buzzer
- //In the first versions of the photocells the microswitch was 8xSwitches
- //Remember to change comment/uncomment depending on the hardware version
- for (int pin = 8; pin >= 3; pin--)
+ //for (int pin = 6; pin >= 3; pin--) //Old versions
+ for (int pin = 8; pin >= 3; pin--) //New versions
{
sample.termNum = sample.termNum * 2; //Each bit will be multiplied by 2 as much times as his significance
if (!digitalRead(pin)) sample.termNum++;
@@ -140,26 +151,27 @@ void setup(void)
//radio.enableDynamicAck();
radio.startListening();
-
-// printf(" Status Radio\n\r");
-// radio.printDetails();
-
-// Serial.print("Channel set to: ");
-// Serial.println(radio.getChannel());
-
+
+ // printf(" Status Radio\n\r");
+ // radio.printDetails();
+
+ // Serial.print("Channel set to: ");
+ // Serial.println(radio.getChannel());
+
Serial.flush(); //Flushing the buffer serial buffer to avoid spurious data.
- // channel of the controler
+ // channel of the controler. 3xmicroswith controls this channel
//************************************************************************************
// A0, A1, A2 connected to the 3xswith
-
- pinMode(A0, INPUT_PULLUP);
+
+// pinMode(A0, INPUT_PULLUP); //Old versions
+ pinMode(A7, INPUT_PULLUP); //New version
pinMode(A1, INPUT_PULLUP);
pinMode(A2, INPUT_PULLUP);
- // En estas entradas se pondra un microswich , de 3 botones
- // Se leeran en binario y se sumarán al canal por defecto 101
- if (!digitalRead(A0)) {
+ // Se leeran en binario y se restará al canal por defecto 125
+ if ( !digitalRead(A0)) { //Old versions
+ // if (analogRead(A7)<128) { //New versions
controlSwitch = 1; //
}
if (!digitalRead(A1)) {
@@ -171,15 +183,17 @@ void setup(void)
Serial.print("ControlChannel: ");
Serial.println(control0Channel - controlSwitch);
-
+
//Activate interruption service each time the sensor changes state
attachInterrupt(digitalPinToInterrupt(2), controlint, CHANGE);
pinMode(A3, OUTPUT); //Blue
- pinMode(A4, OUTPUT); //Green
+ pinMode(A4, OUTPUT); //Green
pinMode(A5, OUTPUT); //Red
- pinMode(7, OUTPUT); //Buzzer
- digitalWrite(7, LOW);
+ pinMode(A0, OUTPUT); //Buzzer
+ buzzer_on;
+ delay(100);
+ buzzer_off;
pinMode(2, INPUT_PULLUP); //Sensor
red_off;
@@ -187,19 +201,28 @@ void setup(void)
blue_off;
-// noInterrupts(); //Don't watch the sensor state
+ // noInterrupts(); //Don't watch the sensor state
-// Serial.print("Is chip connected:");
-// Serial.println(radio.isChipConnected());
+ // Serial.print("Is chip connected:");
+ // Serial.println(radio.isChipConnected());
+
+ Timer1.initialize(2000); //Initializing the debounce timer to 2 ms
+ Timer1.detachInterrupt();
+ sample.state = digitalRead(2);
+ lastPinState = sample.state;
+ Serial.println("Initial state");
+ Serial.print(lastPinState);
+ Serial.print("\t");
+ Serial.println(sample.state);
}
void loop(void)
{
-
- if (flagint == HIGH ) //The sensor has changed
-
+ //if (flagint == HIGH && lastPinState != sample.state) //The sensor has changed
+ if (flagint == HIGH) //The sensor has changed
{
+// lastPinState = sample.state;
sample.elapsedTime = (millis() - time0);
flagint = LOW;
buzzer_off;
@@ -214,7 +237,7 @@ void loop(void)
radio.stopListening();
radio.setChannel(control0Channel - controlSwitch);
// Serial.print("getChannel = ");
-// Serial.println(radio.getChannel());
+ // Serial.println(radio.getChannel());
bool en = radio.write( &sample, sample_size);
// if (en) {
// Serial.println("Sent OK");
@@ -228,9 +251,11 @@ void loop(void)
if (! unlimitedMode) waitingSensor = false;
radio.setChannel(terminal0Channel - sample.termNum);
radio.startListening();
+
// Serial.println("startListening()");
// Serial.print("getChannel = ");
// Serial.println(radio.getChannel());
+ Serial.println(sample.elapsedTime);
}
@@ -248,17 +273,42 @@ void loop(void)
void controlint()
{
+ //sample.state = digitalRead(2);
+ //Serial.println("Int");
if (waitingSensor == true) {
- flagint = HIGH;
+ //flagint = HIGH;
+// Timer1.initialize(1000000); //Initializing the debounce timer to 1s
+ Timer1.attachInterrupt(debounce);
+// Serial.print(lastPinState);
+// Serial.print("\t");
+// Serial.println(sample.state);
+ debounceCount = 0;
+ }
+}
+
+void debounce() {
+ debounceCount++;
+// Serial.println(debounceCount);
+ if (debounceCount >= 2) {
sample.state = digitalRead(2);
-
+ Timer1.detachInterrupt();
+// Serial.println("in debounce");
+
+ if (sample.state != lastPinState) {
+ flagint = HIGH;
+ lastPinState = sample.state;
+ Serial.print(lastPinState);
+ Serial.print("\t");
+ Serial.println(sample.state);
+// Serial.println(millis());
+ }
}
}
void executeCommand(uint16_t command)
{
if (command == deactivate) {
-// Serial.println("deactivating leds and sensor");
+ // Serial.println("deactivating leds and sensor");
deactivateAll();
} else
{
@@ -269,47 +319,47 @@ void executeCommand(uint16_t command)
blinkingGreen = false;
blinkingBlue = false;
MsTimer2::stop();
-
+
if ((command & red) == red) {
-// Serial.println("activating RED");
+ Serial.println("activating RED");
red_on;
}
if ((command & green) == green) {
-// Serial.println("activating GREEN");
+ // Serial.println("activating GREEN");
green_on;
}
if ((command & blue) == blue) {
-// Serial.println("activating BLUE");
+ // Serial.println("activating BLUE");
blue_on;
}
if ((command & buzzer) == buzzer) {
-// Serial.println("activating BUZZER");
+ // Serial.println("activating BUZZER");
buzzer_on;
}
if ((command & blinkRed) == blinkRed) {
-// Serial.println("blinking RED");
+ // Serial.println("blinking RED");
blinkingRed = true;
blinkStart(blinkPeriod);
}
if ((command & blinkGreen) == blinkGreen) {
-// Serial.println("blinking GREEN");
+ // Serial.println("blinking GREEN");
blinkingGreen = true;
blinkStart(blinkPeriod);
}
if ((command & blinkBlue) == blinkBlue) {
-// Serial.println("blinking BLUE");
+ // Serial.println("blinking BLUE");
blinkingBlue = true;
blinkStart(blinkPeriod);
}
if ((command & sensorOnce) == sensorOnce) {
-// Serial.println("activating sensor once");
+ // Serial.println("activating sensor once");
time0 = millis(); //empieza a contar time
waitingSensor = true; //Terminal set to waiting touch/proximity
unlimitedMode = false;
@@ -317,13 +367,13 @@ void executeCommand(uint16_t command)
}
if ((command & sensorUnlimited) == sensorUnlimited) {
-// Serial.println("activating sensor unlimited");
+ // Serial.println("activating sensor unlimited");
time0 = millis(); //empieza a contar time
waitingSensor = true; //Terminal set to waiting touch/proximity
unlimitedMode = true;
interrupts();
}
-
+
if ((command & ping) == ping) {
Serial.println("Pong");
time0 = millis(); //empieza a contar time
@@ -366,25 +416,25 @@ void serialEvent()
{
Serial.println("SerialEvent");
String inputString = Serial.readString();
-
+
//Trimming all the characters after the ";" including it
String commandString = inputString.substring(0, inputString.lastIndexOf(";"));
if ( commandString == "get_channel") {
Serial.println(radio.getChannel());
- } else if (commandString == "get_version"){
+ } else if (commandString == "get_version") {
Serial.println(version);
}
}
void beep(int duration)
{
- MsTimer2::set(duration, beepStop);
- MsTimer2::start();
- buzzer_on;
+ MsTimer2::set(duration, beepStop);
+ MsTimer2::start();
+ buzzer_on;
}
void beepStop(void)
{
- MsTimer2::stop();
- buzzer_off;
+ MsTimer2::stop();
+ buzzer_off;
}
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]