Created
January 27, 2016 05:32
-
-
Save anonymous/496d13a33b415f1b7960 to your computer and use it in GitHub Desktop.
Arduino-controlled RGB LED grow light
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 <Adafruit_NeoPixel.h> | |
#define LEDS 512 | |
#define PIN 5 | |
#define BUTTON 2 | |
#define MODEWHITE 0 | |
#define MODEVEG 1 | |
#define MODEFLOWER 2 | |
#define MODEBOTH 3 | |
#define MODESUPERWHITE 4 | |
#define ON 255 | |
#define DIM 128 | |
#define OFF 0 | |
// Parameter 1 = number of pixels in strip | |
// Parameter 2 = Arduino pin number (most are valid) | |
// Parameter 3 = pixel type flags, add together as needed: | |
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs) | |
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers) | |
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products) | |
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2) | |
Adafruit_NeoPixel strip = Adafruit_NeoPixel(LEDS, PIN, NEO_GRB + NEO_KHZ800); | |
// IMPORTANT: To reduce NeoPixel burnout risk, add 1000 uF capacitor across | |
// pixel power leads, add 300 - 500 Ohm resistor on first pixel's data input | |
// and minimize distance between Arduino and first pixel. Avoid connecting | |
// on a live circuit...if you must, connect GND first. | |
int buttonState = 0; | |
int lastButtonState = 0; | |
int lightMode = 0; | |
long lastDebounceTime = 0; | |
long debounceDelay = 250; | |
void setup() { | |
Serial.begin(9600); | |
pinMode(BUTTON, INPUT); | |
attachInterrupt(digitalPinToInterrupt(BUTTON), buttonInterrupt, CHANGE); | |
strip.begin(); | |
strip.show(); // Initialize all pixels to 'off' | |
setLights(MODEWHITE); | |
Serial.print("Good to grow!"); | |
} | |
void loop() { | |
} | |
void buttonInterrupt() { | |
int reading = digitalRead(BUTTON); | |
if (reading == HIGH) { | |
if((millis() - lastDebounceTime) < debounceDelay) { | |
Serial.print("BOUNCE!"); | |
return; | |
} | |
lastDebounceTime = millis(); | |
lightMode = lightMode >= MODESUPERWHITE ? 0 : lightMode + 1; | |
setLights(lightMode); | |
} | |
return; | |
} | |
void setLights(int mode) { | |
switch(mode) { | |
case MODEWHITE: | |
colorWipe(DIM, DIM, DIM); | |
break; | |
case MODEVEG: | |
colorWipe(OFF, OFF, ON); | |
break; | |
case MODEFLOWER: | |
colorWipe(ON, OFF, OFF); | |
break; | |
case MODEBOTH: | |
colorWipe(ON, OFF, ON); | |
break; | |
case MODESUPERWHITE: | |
colorWipe(ON, ON, ON); | |
break; | |
default: | |
Serial.print("Invalid mode specified."); | |
Serial.print(mode); | |
break; | |
} | |
} | |
// Fill the dots one after the other with a color | |
void colorWipe(int r, int g, int b) { | |
uint32_t c = strip.Color(r, g, b); | |
for (uint16_t i = 0; i < strip.numPixels(); i++) { | |
strip.setPixelColor(i, c); | |
} | |
strip.show(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment