2022-07-23 10:06:03 +00:00
|
|
|
#include <Arduino.h>
|
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
float flashspeed=20; //in kmh
|
|
|
|
unsigned long flashdeadtime=1000; //in ms
|
2022-07-23 10:13:16 +00:00
|
|
|
|
|
|
|
#define PIN_SW1 D6
|
2022-07-23 12:49:47 +00:00
|
|
|
#define PIN_SW2 D5
|
|
|
|
#define PIN_TRIGGER D7
|
2022-07-23 10:13:16 +00:00
|
|
|
volatile boolean sw1_flag = false;
|
|
|
|
volatile boolean sw2_flag = false;
|
|
|
|
|
|
|
|
unsigned long sw1_lastTime=0;
|
|
|
|
unsigned long sw2_lastTime=0;
|
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
float calib_distance=0.062; //distance of sensors in meters
|
2022-07-23 10:13:16 +00:00
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
#define SWDEBOUNCE 100000
|
2022-07-23 10:13:16 +00:00
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
ICACHE_RAM_ATTR void interrupt_sw1();
|
|
|
|
ICACHE_RAM_ATTR void interrupt_sw2();
|
|
|
|
float getLastSpeed1();
|
|
|
|
float getLastSpeed2();
|
|
|
|
void doTrigger1();
|
|
|
|
void doTrigger2();
|
2022-07-23 10:13:16 +00:00
|
|
|
|
2022-07-23 10:06:03 +00:00
|
|
|
void setup() {
|
2022-07-23 10:13:16 +00:00
|
|
|
pinMode(PIN_SW1, INPUT_PULLUP);
|
|
|
|
pinMode(PIN_SW2, INPUT_PULLUP);
|
2022-07-23 12:49:47 +00:00
|
|
|
pinMode(PIN_TRIGGER, OUTPUT);
|
2022-07-23 10:13:16 +00:00
|
|
|
attachInterrupt(digitalPinToInterrupt(PIN_SW1), interrupt_sw1, FALLING);
|
|
|
|
attachInterrupt(digitalPinToInterrupt(PIN_SW2), interrupt_sw2, FALLING);
|
2022-07-23 12:49:47 +00:00
|
|
|
digitalWrite(PIN_TRIGGER, HIGH); //active low
|
2022-07-23 10:13:16 +00:00
|
|
|
|
|
|
|
Serial.begin(115200);
|
2022-07-23 10:06:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
2022-07-23 10:13:16 +00:00
|
|
|
|
|
|
|
if (sw1_flag){
|
|
|
|
sw1_flag=false;
|
2022-07-23 12:49:47 +00:00
|
|
|
sw1_lastTime=micros();
|
2022-07-23 10:13:16 +00:00
|
|
|
Serial.println("SW1");
|
2022-07-23 12:49:47 +00:00
|
|
|
doTrigger1();
|
2022-07-23 10:13:16 +00:00
|
|
|
}
|
|
|
|
if (sw2_flag){
|
|
|
|
sw2_flag=false;
|
2022-07-23 12:49:47 +00:00
|
|
|
sw2_lastTime=micros();
|
2022-07-23 10:13:16 +00:00
|
|
|
Serial.println("SW2");
|
2022-07-23 12:49:47 +00:00
|
|
|
doTrigger2();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void doTrigger1() {
|
|
|
|
static unsigned long last_flash=0;
|
2022-07-23 10:13:16 +00:00
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
|
|
|
|
float speed=getLastSpeed1();
|
|
|
|
|
|
|
|
if (speed*3.6<0.1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (millis()-last_flash>flashdeadtime) { //deadtime
|
|
|
|
last_flash=millis();
|
|
|
|
|
|
|
|
if (speed*3.6 >= flashspeed) {
|
|
|
|
Serial.print("> Speed="); Serial.print(speed*3.6); Serial.println(" km/h");
|
|
|
|
Serial.println("Flash");
|
|
|
|
pinMode(PIN_TRIGGER, INPUT); //high impedance
|
|
|
|
delay(100);
|
|
|
|
pinMode(PIN_TRIGGER, OUTPUT); digitalWrite(PIN_TRIGGER, LOW);
|
|
|
|
}
|
2022-07-23 10:13:16 +00:00
|
|
|
}
|
2022-07-23 12:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void doTrigger2() {
|
|
|
|
static unsigned long last_flash=0;
|
|
|
|
|
|
|
|
float speed=getLastSpeed2();
|
2022-07-23 10:13:16 +00:00
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
|
|
|
|
if (speed*3.6<0.1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (millis()-last_flash>flashdeadtime) { //deadtime
|
|
|
|
last_flash=millis();
|
|
|
|
|
|
|
|
if (speed*3.6 >= flashspeed) {
|
|
|
|
Serial.print("> Speed="); Serial.print(speed*3.6); Serial.println(" km/h");
|
|
|
|
Serial.println("Flash");
|
|
|
|
pinMode(PIN_TRIGGER, INPUT); //high impedance
|
|
|
|
delay(100);
|
|
|
|
pinMode(PIN_TRIGGER, OUTPUT); digitalWrite(PIN_TRIGGER, LOW);
|
|
|
|
}
|
|
|
|
}
|
2022-07-23 10:13:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
|
|
|
|
|
|
|
|
ICACHE_RAM_ATTR void interrupt_sw1() {
|
|
|
|
if (sw1_lastTime+SWDEBOUNCE<micros()){
|
2022-07-23 10:13:16 +00:00
|
|
|
sw1_flag=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
ICACHE_RAM_ATTR void interrupt_sw2() {
|
|
|
|
if (sw2_lastTime+SWDEBOUNCE<micros()){
|
2022-07-23 10:13:16 +00:00
|
|
|
sw2_flag=true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-23 12:49:47 +00:00
|
|
|
float getLastSpeed1() {
|
|
|
|
return calib_distance/((sw1_lastTime-sw2_lastTime)/1000000.0);
|
|
|
|
}
|
|
|
|
float getLastSpeed2() {
|
|
|
|
return calib_distance/((sw2_lastTime-sw1_lastTime)/1000000.0);
|
|
|
|
}
|