cleaup+comments
This commit is contained in:
+37
-34
@@ -13,7 +13,7 @@
|
|||||||
#include <APA102.h>
|
#include <APA102.h>
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
// CONFIGURE please configure next lines depending on your stripe arragmentment
|
// CONFIGURE please configure next lines depending on your stripes arragmentment
|
||||||
#define HALL_PIN 2 // digital pin of hall sensor
|
#define HALL_PIN 2 // digital pin of hall sensor
|
||||||
#define ROUND_COUNT 1 // how many rounds to take time (minimum 1)
|
#define ROUND_COUNT 1 // how many rounds to take time (minimum 1)
|
||||||
|
|
||||||
@@ -25,7 +25,6 @@ int strip_matrix_offset[LED_STRIPES] = {0, 0, (NUM_SEGMENTS / 2), (NUM_SEGMENTS
|
|||||||
bool strip_matrix_invert[LED_STRIPES] = {false, true, false, true}; // set LEDs in revert order for stripe
|
bool strip_matrix_invert[LED_STRIPES] = {false, true, false, true}; // set LEDs in revert order for stripe
|
||||||
// \CONFIGURE
|
// \CONFIGURE
|
||||||
|
|
||||||
|
|
||||||
// LED stripes
|
// LED stripes
|
||||||
#ifdef NEOPIXEL
|
#ifdef NEOPIXEL
|
||||||
#define LED_PIN 6 // data pin for neopixel LED stripe
|
#define LED_PIN 6 // data pin for neopixel LED stripe
|
||||||
@@ -36,7 +35,7 @@ Adafruit_NeoPixel ledStrip(LED_COUNT *LED_STRIPES, LED_PIN, NEO_GRB + NEO_KHZ800
|
|||||||
#define LED_CLOCK_PIN 11 // clock pin for LED strip
|
#define LED_CLOCK_PIN 11 // clock pin for LED strip
|
||||||
APA102<LED_DATA_PIN, LED_CLOCK_PIN> ledStrip;
|
APA102<LED_DATA_PIN, LED_CLOCK_PIN> ledStrip;
|
||||||
#define BRIGHTNESS 1 // brightness for LED strip [0-31]
|
#define BRIGHTNESS 1 // brightness for LED strip [0-31]
|
||||||
rgb_color color_buffer[LED_COUNT * LED_STRIPES]; // color buffer to write to LED stripe
|
rgb_color color_buffer[LED_COUNT * LED_STRIPES]; // color buffer to write to LED stripe
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
float passed = 0;
|
float passed = 0;
|
||||||
@@ -45,15 +44,15 @@ int current_image_index = 0;
|
|||||||
uint8_t *current_palette;
|
uint8_t *current_palette;
|
||||||
uint8_t *current_pixels;
|
uint8_t *current_pixels;
|
||||||
|
|
||||||
int current_state = 0;
|
int current_segment = 0;
|
||||||
|
|
||||||
void setup()
|
void setup()
|
||||||
{
|
{
|
||||||
Serial.begin(115200);
|
Serial.begin(115200); // debug serial print
|
||||||
pinMode(HALL_PIN, INPUT);
|
pinMode(HALL_PIN, INPUT); // set hall pin as input
|
||||||
pinMode(LED_BUILTIN, INPUT);
|
pinMode(LED_BUILTIN, OUTPUT); // set build-in LED as output
|
||||||
current_palette = (uint8_t *)pgm_read_word(&images[current_image_index].palette);
|
current_palette = (uint8_t *)pgm_read_word(&images[current_image_index].palette); // init palette for current image
|
||||||
current_pixels = (uint8_t *)pgm_read_word(&images[current_image_index].pixels);
|
current_pixels = (uint8_t *)pgm_read_word(&images[current_image_index].pixels); // init pixels for current image
|
||||||
#ifdef NEOPIXEL
|
#ifdef NEOPIXEL
|
||||||
ledStrip.begin();
|
ledStrip.begin();
|
||||||
ledStrip.show();
|
ledStrip.show();
|
||||||
@@ -65,52 +64,54 @@ void loop()
|
|||||||
{
|
{
|
||||||
float start = micros();
|
float start = micros();
|
||||||
int count = 0;
|
int count = 0;
|
||||||
bool change = false;
|
bool detected = false;
|
||||||
while (count <= ROUND_COUNT)
|
while (count <= ROUND_COUNT) // keep in loop while not ROUND_COUNT reached
|
||||||
{
|
{
|
||||||
if (digitalRead(HALL_PIN) == LOW)
|
if (digitalRead(HALL_PIN) == LOW) // if hall sensor detect magnet
|
||||||
{
|
{
|
||||||
digitalWrite(LED_BUILTIN, HIGH);
|
digitalWrite(LED_BUILTIN, HIGH); // use build-in LED as indicator for present magnet
|
||||||
if (!change)
|
if (!detected) // check if magnet present first time
|
||||||
{
|
{
|
||||||
change = true;
|
detected = true;
|
||||||
count++;
|
count++; // count round
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else // if hall sensor not detect magnet
|
||||||
{
|
{
|
||||||
digitalWrite(LED_BUILTIN, LOW);
|
digitalWrite(LED_BUILTIN, LOW); // build-in LED indicator
|
||||||
change = false;
|
detected = false; // reset magnet state
|
||||||
}
|
}
|
||||||
|
|
||||||
float current_diff = micros() - start;
|
float current_diff = micros() - start; // get time passed in current loop
|
||||||
|
|
||||||
#ifndef TEST_STRIPES
|
#ifdef TEST_STRIPES // testing stripes
|
||||||
current_state = ((float)passed / current_diff * NUM_SEGMENTS);
|
int segment = (micros() / 500000) % NUM_SEGMENTS; // calculate current segment as half seconds passed since runtime
|
||||||
#else
|
if (segment == current_segment) // do nothing if still in old segment
|
||||||
int state = (micros() / 500000) % NUM_SEGMENTS;
|
|
||||||
if (state == current_state)
|
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
Serial.println(current_state);
|
Serial.println(current_segment); // debug print of current segment
|
||||||
current_state = state;
|
current_segment = segment;
|
||||||
|
#else
|
||||||
|
current_segment = ((float)passed / current_diff * NUM_SEGMENTS); // calculate current segment as percentage of time passed
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
for (int strip = 0; strip < LED_STRIPES; strip++)
|
for (int strip = 0; strip < LED_STRIPES; strip++) // loop over all stripes
|
||||||
{
|
{
|
||||||
uint8_t pixel_color_index;
|
uint8_t pixel_color_index;
|
||||||
uint8_t *current_pixel = (uint8_t *)¤t_pixels[((current_state + strip_matrix_offset[strip]) % NUM_SEGMENTS) * LED_COUNT];
|
// get pointer to current pixel of image as offset of current strip and offset of current segment
|
||||||
for (int i = 0; i < LED_COUNT; i++)
|
uint8_t *current_pixel = (uint8_t *)¤t_pixels[((current_segment + strip_matrix_offset[strip]) % NUM_SEGMENTS) * LED_COUNT];
|
||||||
|
for (int i = 0; i < LED_COUNT; i++) // loop over all LEDs of current strip
|
||||||
{
|
{
|
||||||
pixel_color_index = pgm_read_byte(current_pixel++) * 3;
|
pixel_color_index = pgm_read_byte(current_pixel++) * 3; // read color palette index for current pixel
|
||||||
uint8_t pixel_index = strip * LED_COUNT + (strip_matrix_invert[strip] ? (LED_COUNT - i) : i);
|
uint8_t pixel_index = strip * LED_COUNT + (strip_matrix_invert[strip] ? (LED_COUNT - i) : i); // calculate index of LED with offset and inversion
|
||||||
#ifdef NEOPIXEL
|
#ifdef NEOPIXEL
|
||||||
#define LED_PIN 6
|
// set pixel of NEOPIXEL
|
||||||
ledStrip.setPixelColor(pixel_index, pgm_read_byte(¤t_palette[pixel_color_index]),
|
ledStrip.setPixelColor(pixel_index, pgm_read_byte(¤t_palette[pixel_color_index]),
|
||||||
pgm_read_byte(¤t_palette[pixel_color_index + 1]),
|
pgm_read_byte(¤t_palette[pixel_color_index + 1]),
|
||||||
pgm_read_byte(¤t_palette[pixel_color_index + 2]));
|
pgm_read_byte(¤t_palette[pixel_color_index + 2]));
|
||||||
#else
|
#else
|
||||||
|
// set pixel of buffer
|
||||||
color_buffer[pixel_index] = rgb_color(
|
color_buffer[pixel_index] = rgb_color(
|
||||||
pgm_read_byte(¤t_palette[pixel_color_index]),
|
pgm_read_byte(¤t_palette[pixel_color_index]),
|
||||||
pgm_read_byte(¤t_palette[pixel_color_index + 1]),
|
pgm_read_byte(¤t_palette[pixel_color_index + 1]),
|
||||||
@@ -119,11 +120,13 @@ void loop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
#ifdef NEOPIXEL
|
#ifdef NEOPIXEL
|
||||||
|
// update pixel
|
||||||
ledStrip.show();
|
ledStrip.show();
|
||||||
#else
|
#else
|
||||||
|
// write buffer to stripes
|
||||||
ledStrip.write(color_buffer, LED_COUNT * LED_STRIPES, BRIGHTNESS);
|
ledStrip.write(color_buffer, LED_COUNT * LED_STRIPES, BRIGHTNESS);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
passed = (micros() - start);
|
passed = (micros() - start); // rounds done, caluclate duration
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user