#include <neopixel.h>
int ledPin = D3;
int buttonPin = D4;
bool doLED = false;
int buttonVar =0;
int buttonValue;
bool LEDvar = false;
Servo flowerServo;
int servoPin = D6;
int servoValue = 0;
#define servoMin 0
#define servoMax 125
#define PIXEL_PIN D5
#define PIXEL_COUNT 1
#define PIXEL_TYPE WS2812B
Adafruit_NeoPixel flowerLED = Adafruit_NeoPixel( PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
#define PIXEL_PIN_POT D7
#define PIXEL_COUNT_POT 16
#define PIXEL_TYPE_POT WS2812
Adafruit_NeoPixel potLED = Adafruit_NeoPixel(PIXEL_COUNT_POT, PIXEL_PIN_POT, PIXEL_TYPE_POT);
int redValue = 40;
int greenValue = 40;
int blueValue = 40;
bool pulseDirection = true;
int pulseDifference = 0;
int pulseMax = 28;
int test = 0;
void setup() {
pinMode( buttonPin, INPUT_PULLUP);
flowerServo.attach(servoPin);
flowerServo.write(servoMin);
// get servo value based on focusing time
Particle.subscribe( "servo", servoControl);
// trigger LED when button is pushed on the other side
Particle.subscribe( "triggerLED", blinkPotLED);
Particle.variable( "servoValue" , &servoValue , INT );
Particle.variable( "testValue" , &test , INT );
flowerLED.begin();
potLED.begin();
// can be controlled through cloud
Particle.function("Red_Value", setRedValue);
Particle.function("Green_Value", setGreenValue);
Particle.function("Blue_Value", setBlueValue);
//lightup flower LED
flowerLED.setPixelColor(0, redValue, greenValue, blueValue);
for(int i=0; i<potLED.numPixels();i++){
potLED.setPixelColor(i, redValue, greenValue, blueValue);
//potLED.show();
test++;
}
flowerLED.setPixelColor(0, redValue, greenValue, blueValue);
flowerLED.show();
potLED.show();
}
void loop() {
buttonValue = digitalRead( buttonPin );
if( buttonValue == LOW ){
if(servoValue == servoMin){
servoValue = servoMax;
}else{
servoValue = servoMin;
}
flowerServo. write(servoValue);
}
colorPulse(); //for flower tip single pixel, not pot ring
flowerLED.show();
for(int i=0; i<potLED.numPixels();i++){
potLED.setPixelColor(i, redValue, greenValue, blueValue);
//potLED.show();
}
potLED.show();
delay(1000);
}
// set LED to red
int setRedValue(String command)
{
// Convert
redValue = command.toInt();
flowerLED.setPixelColor(0, redValue, greenValue, blueValue);
for(int i=0; i<potLED.numPixels();i++){
potLED.setPixelColor(i, redValue, greenValue, blueValue);
//potLED.show();
}
return 1;
}
// set LED to green
int setGreenValue(String command)
{
// Convert
greenValue = command.toInt();
flowerLED.setPixelColor(0, redValue, greenValue, blueValue);
for(int i=0; i<potLED.numPixels();i++){
potLED.setPixelColor(i, redValue, greenValue, blueValue);
potLED.show();
}
return 1;
}
// set LED to blue
int setBlueValue(String command)
{
// Convert
blueValue = command.toInt();
flowerLED.setPixelColor(0, redValue, greenValue, blueValue);
for(int i=0; i<potLED.numPixels();i++){
potLED.setPixelColor(i, redValue, greenValue, blueValue);
potLED.show();
}
return 1;
}
// let LED color breathing
void colorPulse(){
flowerLED.setPixelColor(0, redValue-pulseDifference, greenValue-pulseDifference, blueValue-pulseDifference);
// for(int i=0; i<PIXEL_COUNT_POT;i++){
// potLED.setPixelColor(i, redValue, greenValue, blueValue);
// }
if(pulseDirection){
pulseDifference++;
if(pulseDifference>=pulseMax){
pulseDirection = !pulseDirection;
}
}else{
pulseDifference--;
if(pulseDifference <=0){
pulseDirection = !pulseDirection;
}
}
}
// open/close the flower
void servoControl(const char *event, const char *data)
{
int newPos = String(data).toInt();
// Make sure it is in the right range
// And set the position
servoValue = constrain( newPos, servoMin , servoMax);
// Set the servo
flowerServo.write( servoValue );
// done
return;
}
// blink LED, respond to pushed button
void blinkPotLED(const char *event, const char *data)
{
setRedValue("80");
flowerLED.show();
delay(5000);
setRedValue("40");
for(int i=0; i<potLED.numPixels();i++){
potLED.setPixelColor(i, redValue, greenValue, blueValue);
potLED.show();
}
return;
}
Click to Expand
Content Rating
Is this a good/useful/informative piece of content to include in the project? Have your say!
You must login before you can post a comment. .