37 lines
959 B
C++
37 lines
959 B
C++
#include "Detector.hpp"
|
|
|
|
#include "TimeSync.hpp"
|
|
#include "kartSender.hpp"
|
|
|
|
Detector detector;
|
|
|
|
#define PIN_BUTTON 0
|
|
#define NUM_SAMPLES 100
|
|
|
|
void Detector::Init() {
|
|
pinMode(PIN_BUTTON, INPUT_PULLUP);
|
|
}
|
|
|
|
void Detector::Tasks() {
|
|
uint32_t avg = 0;
|
|
for (int i = 0; i < NUM_SAMPLES; i++) {
|
|
avg += analogRead(1);
|
|
}
|
|
float detect = avg / (float)NUM_SAMPLES / 4095.0f;
|
|
detect -= .37; // idle state
|
|
// Serial.printf("Button detect: %.2f\n", detect);
|
|
uint16_t button_state = detect < .02f;
|
|
if (button_state != last_button_state) {
|
|
if (!button_state) {
|
|
falling_edge_time = millis();
|
|
handled = false;
|
|
}
|
|
}
|
|
last_button_state = button_state;
|
|
|
|
if (!button_state && millis() - falling_edge_time > 30 && !handled) {
|
|
handled = true;
|
|
// Serial.printf("Timestamp: %llu\n", TimeSync::GetTime());
|
|
kart_sender.Send();
|
|
}
|
|
} |