led_bikewheel/led_bikewheel.ino

91 lines
1.6 KiB
Arduino
Raw Normal View History

2020-12-28 19:15:19 +01:00
#include <FastGPIO.h>
#define APA102_USE_FAST_GPIO
#include <APA102.h>
2020-12-28 16:46:07 +01:00
const int HALL_PIN = 2;
2020-12-28 17:02:46 +01:00
const int ROUND_COUNT = 2;
2020-12-28 15:47:40 +01:00
2020-12-28 19:15:19 +01:00
// Define which pins to use.
const uint8_t dataPin = 10;
const uint8_t clockPin = 11;
// Create an object for writing to the LED strip.
APA102<dataPin, clockPin> ledStrip;
2020-12-28 19:21:34 +01:00
const uint16_t ledCount = 28;
2020-12-28 19:15:19 +01:00
const uint8_t brightness = 1;
const rgb_color RED = rgb_color(255, 0, 0);
const rgb_color GREEN = rgb_color(0, 255, 0);
const rgb_color BLUE = rgb_color(0, 0, 255);
const rgb_color WHITE = rgb_color(255, 255, 255);
const int state_count = 4;
rgb_color colors[ledCount];
float passed = 0;
int state = 0;
void setup()
{
2020-12-28 17:02:46 +01:00
Serial.begin(115200);
2020-12-28 15:47:40 +01:00
pinMode(HALL_PIN, INPUT);
}
2020-12-28 19:15:19 +01:00
void loop()
{
2020-12-28 17:02:46 +01:00
float start = micros();
int count = 0;
bool change = false;
2020-12-28 19:15:19 +01:00
while (count < ROUND_COUNT)
{
2020-12-28 19:21:34 +01:00
// check HALL if turned
2020-12-28 19:15:19 +01:00
if (digitalRead(HALL_PIN) == LOW)
{
if (!change)
{
2020-12-28 17:02:46 +01:00
change = true;
count++;
}
2020-12-28 19:15:19 +01:00
}
else
{
2020-12-28 17:02:46 +01:00
change = false;
}
2020-12-28 19:15:19 +01:00
2020-12-28 19:21:34 +01:00
// calc led for last turn
2020-12-28 19:15:19 +01:00
float current_diff = micros() - start;
for (int i = 0; i < state_count; i++)
{
if (current_diff > ((passed / state_count) * i))
{
state = i;
}
}
rgb_color color = WHITE;
switch (state)
{
case 1:
color = RED;
break;
case 2:
color = GREEN;
break;
case 3:
color = BLUE;
break;
}
for (uint16_t i = 0; i < ledCount; i++)
{
colors[i] = color;
}
ledStrip.write(colors, ledCount, brightness);
2020-12-28 16:47:45 +01:00
}
2020-12-28 17:02:46 +01:00
2020-12-28 19:15:19 +01:00
passed = (micros() - start);
2020-12-28 15:47:40 +01:00
}