Last active
December 31, 2020 00:36
-
-
Save thankthemaker/cd2ece219a691bb99fcae39d5fbfe9ac to your computer and use it in GitHub Desktop.
Funwheel Light Control
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//#include "FastLED.h" | |
#include <Adafruit_NeoPixel.h> | |
#define PIN D1 | |
#define PIN_FORWARD D2 | |
#define PIN_BACKWARD D3 | |
#define PIN_BRAKE D4 | |
// How many NeoPixels are attached to the Arduino? | |
#define NUMPIXELS 16 | |
#define MAX_BRIGHTNESS 100 // allowed values 1-255 | |
#define MAX_BRIGHTNESS_BRAKE 255 // allowed values 1-255 | |
#define NUM_PATTERN1 4 | |
#define NUM_PATTERN2 1 | |
#ifndef ADAFRUIT_NEOPIXEL_H | |
CRGB leds[NUMPIXELS]; | |
#endif | |
int old_forward = HIGH; | |
int old_backward = HIGH; | |
int old_brake = HIGH; | |
int new_forward = HIGH; | |
int new_backward = HIGH; | |
int new_brake = HIGH; | |
#ifdef ADAFRUIT_NEOPIXEL_H | |
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800); | |
#endif | |
void setup() { | |
Serial.begin(115200); | |
pinMode(PIN_FORWARD, INPUT_PULLUP); | |
pinMode(PIN_BACKWARD, INPUT_PULLUP); | |
pinMode(PIN_BRAKE, INPUT_PULLUP); | |
#ifdef ADAFRUIT_NEOPIXEL_H | |
pixels.begin(); // This initializes the NeoPixel library. | |
#endif | |
#ifndef ADAFRUIT_NEOPIXEL_H | |
FastLED.addLeds<NEOPIXEL, PIN>(leds, NUMPIXELS); // GRB ordering is assumed | |
#endif | |
delay(100); | |
startSequence(0, 0, MAX_BRIGHTNESS, 100); | |
stop(); | |
} | |
void loop() { | |
new_forward = digitalRead(PIN_FORWARD); | |
new_backward = digitalRead(PIN_BACKWARD); | |
new_brake = digitalRead(PIN_BRAKE); | |
// if( new_brake != old_brake) { | |
if(new_brake == LOW) { | |
flash(new_forward == HIGH); | |
} | |
// else { | |
// stop(); | |
// } | |
// old_brake = new_brake; | |
// } | |
// is there a change detected | |
if(old_forward != new_forward || old_backward != new_backward) { | |
Serial.println("change detected "); | |
Serial.println("forward is " + String(new_forward) + " was " + String(old_forward)); | |
Serial.println("backward is " + String(new_backward) + " was " + String(old_backward)); | |
Serial.println("brake is " + String(new_brake) + " was " + String(old_brake)); | |
if(new_forward == LOW && new_backward == HIGH) { | |
fadeout(false); | |
fadein(true); | |
} | |
if(new_backward == LOW && new_forward == HIGH) { | |
fadeout(true); | |
fadein(false); | |
} | |
old_forward = new_forward; | |
old_backward = new_backward; | |
} | |
delay(20); | |
} | |
void setPixel(int Pixel, byte red, byte green, byte blue) { | |
#ifdef ADAFRUIT_NEOPIXEL_H | |
// NeoPixel | |
pixels.setPixelColor(Pixel, pixels.Color(red, green, blue)); | |
#endif | |
#ifndef ADAFRUIT_NEOPIXEL_H | |
// FastLED | |
leds[Pixel].r = red; | |
leds[Pixel].g = green; | |
leds[Pixel].b = blue; | |
#endif | |
} | |
void stop() { | |
Serial.println("stop"); | |
for(int i = 0; i < NUMPIXELS; i++ ) { | |
setPixel(i, 0, 0, 0); | |
} | |
showStrip(); | |
old_forward = LOW; | |
old_backward = LOW; | |
} | |
void forward(byte brightness) { | |
for(int i = 0; i < NUMPIXELS/2; i++ ) { | |
setPixel(i, brightness, brightness, brightness); | |
} | |
for(int i = NUMPIXELS/2; i < NUMPIXELS; i++) { | |
setPixel(i, brightness, 0, 0); | |
} | |
showStrip(); | |
} | |
void backward(byte brightness) { | |
for(int i = 0; i < NUMPIXELS/2; i++ ) { | |
setPixel(i, brightness, 0, 0); | |
} | |
for(int i = NUMPIXELS/2; i < NUMPIXELS; i++) { | |
setPixel(i, brightness, brightness, brightness); | |
} | |
showStrip(); | |
} | |
void showStrip() { | |
#ifdef ADAFRUIT_NEOPIXEL_H | |
// NeoPixel | |
pixels.show(); | |
#endif | |
#ifndef ADAFRUIT_NEOPIXEL_H | |
// FastLED | |
FastLED.show(); | |
#endif | |
} | |
void fadein(boolean isForward) { | |
Serial.println("fadein " + String(isForward)); | |
for(int k = 0; k < MAX_BRIGHTNESS+1; k++) { | |
isForward ? forward(k) : backward(k); | |
delay(15); | |
} | |
} | |
void fadeout(boolean isForward){ | |
Serial.println("fadeout " + String(isForward)); | |
for(int k = MAX_BRIGHTNESS; k >= 0; k--) { | |
isForward ? forward(k) : backward(k); | |
delay(15); | |
} | |
} | |
void flash(boolean isForward) { | |
Serial.println("flash " + String(isForward)); | |
for(int j=0; j<20; j++) { | |
for(int i = 0; i < NUMPIXELS/2; i++ ) { | |
isForward ? setPixel(i, j%2 == 0 ? MAX_BRIGHTNESS_BRAKE : MAX_BRIGHTNESS, 0, 0) : setPixel(i, MAX_BRIGHTNESS, MAX_BRIGHTNESS, MAX_BRIGHTNESS); | |
} | |
for(int i = NUMPIXELS/2; i < NUMPIXELS; i++) { | |
isForward ? setPixel(i, MAX_BRIGHTNESS, MAX_BRIGHTNESS, MAX_BRIGHTNESS): setPixel(i, j%2 == 0 ? MAX_BRIGHTNESS_BRAKE : MAX_BRIGHTNESS, 0, 0); | |
} | |
showStrip(); | |
delay(80); | |
} | |
} | |
void startSequence(byte red, byte green, byte blue, int speedDelay) { | |
for (int j=0; j<10; j++) { //do 10 cycles of chasing | |
for (int q=0; q < 3; q++) { | |
for (int i=0; i < NUMPIXELS; i=i+3) { | |
setPixel(i+q, red, green, blue); //turn every third pixel on | |
} | |
showStrip(); | |
delay(speedDelay); | |
for (int i=0; i < NUMPIXELS; i=i+3) { | |
setPixel(i+q, 0,0,0); //turn every third pixel off | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment