updated hall code

This commit is contained in:
2021-10-11 07:44:21 +02:00
parent 62cac4c80d
commit 24a6e8e662
14 changed files with 214066 additions and 11390 deletions

View File

@@ -1,7 +1,12 @@
#include "game.h"
#include "math.h"
uint64_t last_hall_read;
uint32_t last_hall_read;
uint16_t last_hall_sample;
uint8_t hall_idle_count;
bool hall_is_Idle = true;
void initGame(void)
{
@@ -14,7 +19,42 @@ void handleGame(void)
uint32_t timeNow = millis();
if(timeNow - last_hall_read > HALLINTERVAL)
{
Serial.println(analogRead(HALL_INPUT));
uint16_t hall_sample = analogRead(HALL_INPUT);
uint16_t hall_delta = (last_hall_sample > hall_sample)? (last_hall_sample - hall_sample) : (hall_sample - last_hall_sample);
if(hall_delta > HALLIDLETHRESHOLD)
{
if(hall_idle_count > HALLIDLESAMPLES)
{
hall_is_Idle = false;
}
else
{
hall_idle_count++;
}
}
else
{
if(hall_idle_count == 0)
{
hall_is_Idle = true;
}
else
{
hall_idle_count--;
}
}
Serial.printf("HallSensor: val=%d, delta=%d, count=%d, idle=%s\n",
hall_sample,
hall_delta,
hall_idle_count,
(hall_is_Idle? "yes":"no"));
last_hall_sample = hall_sample;
last_hall_read = timeNow;
}
}
bool hallIsIdle(void)
{
return hall_is_Idle;
}