Back to Parent

// NeoPixel
#include <neopixel.h>

// Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D6
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

// Servo
Servo servo;
int servoPin = A3;

// Switch 
int switchPin = D3;

// User input
int minutes = 0;

void setup() {
    // Setup particle serial monitor
    Serial.begin(9600);
    
    // Setup neopixel
    strip.begin();
    strip.show();
    
    // Setup servo
    servo.attach(servoPin);
    servo.write(0);
    delay(500);
    servo.write(180);
    delay(500);
    
    // Set up switch
    pinMode(switchPin, INPUT_PULLUP);
    
    // Set up particle function
    Particle.function("Set your timer in minutes:", setTime);

}

void loop() {
    // Read switch mode
    int switchState = digitalRead(switchPin);
    int count = 0;
    
    // this loop represents 30 sec timer
    // this loop repeats the number of times of the user input
    if(switchState == LOW) {
        for (int i = 0; i < minutes * 2; i++ ) {
            for (int a = 0; a <= 180; a = a + 6) {
                count++;
                servo.write(a);
                delay(1000);
                // if remaining time is lower that 1/3 of the user input
                // slowly neoPixel glows up into gentle color
                Serial.println(count);
                if (count >= 2 * 60 * minutes / 3) {
                    for (int i = 0; i < strip.numPixels(); i++) {
                        int color = map(count, 0, 60 * minutes, 0, 10);
                        strip.setPixelColor(i, 0, color, 0);
                    }
                    strip.show();                    
                }
            }
        }
        switchState = digitalRead(switchPin);
        // turn off neopixels
        for (int i = 0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, 0, 0, 0);
        }
        strip.show();  
        count = 0;
    }
}

int setTime(String cmd) {
    minutes = cmd.toInt();
    
    return -1;
}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0