35C3_Teststrecke/lichtschranke/polling/polling.ino

155 lines
3.0 KiB
Arduino
Raw Normal View History

2018-12-26 23:56:32 +00:00
#define PIN_LED 13
#define PIN_SW1 2
#define PIN_SW2 3
#define PIN_FLASH 5
volatile boolean sw1_flag = false;
volatile boolean sw2_flag = false;
volatile boolean sw1_released = true;
volatile boolean sw2_released = true;
unsigned long sw1_lastTime=0;
unsigned long sw2_lastTime=0;
unsigned long sw1_lastTimeMicros=0;
unsigned long sw2_lastTimeMicros=0;
#define SWDEBOUNCE 500
float sensordistance = 0.37; //in meters
2018-12-27 11:57:54 +00:00
/*#define TIMESCOUNT 20
2018-12-26 23:56:32 +00:00
uint8_t times_index=0;
float times[TIMESCOUNT];
2018-12-27 11:57:54 +00:00
*/
2018-12-26 23:56:32 +00:00
2018-12-27 11:57:54 +00:00
int serialvalue=0;
2018-12-26 23:56:32 +00:00
2018-12-27 11:57:54 +00:00
float kmhtriggermin=15;
2018-12-26 23:56:32 +00:00
void setup() {
pinMode(PIN_LED, OUTPUT);
pinMode(PIN_SW1, INPUT_PULLUP);
pinMode(PIN_SW2, INPUT_PULLUP);
pinMode(PIN_FLASH, OUTPUT);
digitalWrite(PIN_FLASH, HIGH);
2018-12-27 11:57:54 +00:00
/*for (uint8_t i=0;i<TIMESCOUNT;i++){
2018-12-26 23:56:32 +00:00
times[i]=15;
2018-12-27 11:57:54 +00:00
}*/
2018-12-26 23:56:32 +00:00
Serial.begin(115200);
}
void loop() {
//digitalWrite(PIN_LED, sw1_flag);
2018-12-27 11:57:54 +00:00
while (Serial.available() > 0) {
serialvalue = Serial.parseInt();
Serial.print("#");
Serial.println(serialvalue);
if (serialvalue>=1 && serialvalue <= 100){
kmhtriggermin=serialvalue;
Serial.print("settrigger=");
Serial.println(kmhtriggermin);
}
if (serialvalue==-1){
digitalWrite(PIN_FLASH, LOW); //Trigger flash
delay(100);
digitalWrite(PIN_FLASH, HIGH);
}
while (Serial.read() != '\n') {
//read until line ended
}
}
2018-12-26 23:56:32 +00:00
if (sw1_released && !digitalRead(PIN_SW1)){ //pressed
sw1_released=false;
if (sw1_lastTime+SWDEBOUNCE<millis()){
sw1_flag=true;
sw1_lastTime=millis();
sw1_lastTimeMicros=micros();
}
}else if (!sw1_released && digitalRead(PIN_SW1)){ //released
sw1_released=true;
}
if (sw2_released && !digitalRead(PIN_SW2)){ //pressed
sw2_released=false;
if (sw2_lastTime+SWDEBOUNCE<millis()){
sw2_flag=true;
sw2_lastTime=millis();
sw2_lastTimeMicros=micros();
}
}else if (!sw2_released && digitalRead(PIN_SW2)){ //released
sw2_released=true;
}
if (sw1_flag){
sw1_flag=false;
//Serial.println("SW1");
}
if (sw2_flag){
sw2_flag=false;
//Serial.println("SW2");
unsigned long timediff=sw2_lastTimeMicros-sw1_lastTimeMicros;
//Serial.println(timediff);
float meterpersecond=1.0/timediff*sensordistance*1000000; // meter per s
//Serial.print(meterpersecond);
//Serial.println(" m/s");
float kmh=meterpersecond*3.6;
if (kmh>=1){
2018-12-27 11:57:54 +00:00
if (kmh>kmhtriggermin){
// kmh>getTimesMax()
2018-12-26 23:56:32 +00:00
digitalWrite(PIN_FLASH, LOW); //Trigger flash
delay(100);
digitalWrite(PIN_FLASH, HIGH);
}
2018-12-27 11:57:54 +00:00
Serial.print("speed=");
2018-12-26 23:56:32 +00:00
Serial.println(kmh);
//Serial.println(" kmh");
//add time to last times queue
2018-12-27 11:57:54 +00:00
/*times_index++;
2018-12-26 23:56:32 +00:00
times_index%=TIMESCOUNT-1;
times[times_index]=kmh;
2018-12-27 11:57:54 +00:00
*/
2018-12-26 23:56:32 +00:00
}
}
}
2018-12-27 11:57:54 +00:00
/*
2018-12-26 23:56:32 +00:00
float getTimesMax(){
float timesmax=0;
for (uint8_t i=0;i<TIMESCOUNT;i++){
if (timesmax<times[i]){
timesmax=times[i];
}
}
return timesmax;
}
2018-12-27 11:57:54 +00:00
*/