Skills Dev 3: Nicole

Made by Nicole Chu

Skills Dev 3: Ambient Orb

Created: December 13th, 2020

0

Controlling Outputs: Neopixels

For this Skills Dev, we created an ambient calendar alert using a neopixel strip. To understand how to do this, I first went through the Neopixel tutorial on the Lab site.

0

Programming Neopixels

Following the setup guide on the Lab site, I was able to connect the Neopixel strip and turn on all of the pixels at once.

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  uint16_t i;
  uint32_t c = strip.Color(0, 0, 255); // Blue

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, c );
    strip.setBrightness(20);
    
  }
strip.show();
delay( 100 );

}
Click to Expand
0

Programming Neopixels: Exercise 1

Modify the code to light up pixel by pixel then reverse the sequence turning each pixel off one by one

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {
  
  uint16_t i;
  uint32_t c = strip.Color(0, 0, 255); // Blue
  uint32_t d = strip.Color(0, 0, 0); // Off
    
    for( int i = 0; i < strip.numPixels(); i++ ){
       strip.setPixelColor(i,c); // set a color 
       strip.setBrightness(20);
       strip.show();
       delay( 500 );
    }
	
	delay( 100 );

    for( int i = strip.numPixels(); i > 0; i-- ){
       strip.setPixelColor(i, d); // set a color 
       strip.setBrightness(20);
       strip.show();
       delay( 500 );
    }

}
Click to Expand
0

Programming Neopixels: Exercise 2

Modify the code to light up blue, then red, then green then white in sequence

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  uint16_t i;
  uint32_t blue = strip.Color(0, 0, 255); //white
  uint32_t white = strip.Color(255, 255, 255); //white
  uint32_t red = strip.Color(255, 0, 0); //red
  uint32_t green = strip.Color(0, 255, 0); //green
 
  //int colors[] = {blue,white, red, green};
    
    for( int i = 0; i < strip.numPixels(); i++ ){
        strip.setPixelColor(i, blue); // set a color 
        strip.setBrightness(20);
    }

    strip.show();
    delay(700);
  
    for( int i = 0; i < strip.numPixels(); i++ ){
        strip.setPixelColor(i, red); // set a color 
        strip.setBrightness(20);
    }

    strip.show();
    delay(700);
    
    for( int i = 0; i < strip.numPixels(); i++ ){
        strip.setPixelColor(i, green); // set a color 
        strip.setBrightness(20);
    }

    strip.show();
    delay(700);
  
    for( int i = 0; i < strip.numPixels(); i++ ){
        strip.setPixelColor(i, white); // set a color 
        strip.setBrightness(20);
    }

    strip.show();
    delay(700);
  
}
Click to Expand
0

Programming Neopixels: Exercise 3

Change the code to only light up only one pixel at a time. With each loop move that pixel one position higher. When it reaches the final pixel, restart the sequence at zero i.e. build a single pixel that will continually cycle

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int pixelNum = 0;

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  uint16_t i;
  uint32_t blue = strip.Color(0, 0, 255); //blue
  uint32_t off = strip.Color(0, 0, 0); //off
    
    for( int i = 0; i < strip.numPixels(); i++ ){
        if (i != pixelNum) {
            strip.setPixelColor(i, off);
        }
        else {
            strip.setPixelColor(i, blue); // set a color 
            strip.setBrightness(20);
        }
    }
    pixelNum++;
   
    if (pixelNum >= strip.numPixels()) {
            pixelNum = 0;
        }
        
    strip.show();
    delay(100);
    
}
Click to Expand
0

Programming Neopixels: Exercise 4

Add three Particle.function to set the Red, Green and Blue components. Allow these values to be set from 0-255 and as they are set to change all of the pixels on the LED strip to that color.

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

int pixelNum = 0;
int redValue = 255;
int greenValue = 255;
int blueValue = 255;

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  Particle.function("red", redVal);
  Particle.function("green", greenVal);
  Particle.function("blue", blueVal);
  
}

int redVal(String val){

    redValue = val.toInt();
    return redValue;
}

int greenVal(String val){

    greenValue = val.toInt();
    return greenValue;
}

int blueVal(String val){

    blueValue = val.toInt();
    return blueValue;
}
    

void loop() {

  uint16_t i;
     
    for( int i = 0; i < strip.numPixels(); i++ ){
       strip.setPixelColor(i, redValue, greenValue, blueValue); // set a color 
       strip.setBrightness(20);
    }
    strip.show();
    delay( 100 );
    
    Particle.process();
}
Click to Expand
0

Practice Exercise: Ambient Calendar Alert

This exercise asks us to create an ambient calendar alert using a neopixel strip. It is connected to Google Calendar through IFTTT and is able to send an alert to a device 15 minutes before an appointment. The neopixels will respond by fading up slowly to signal there’s an event, and changing color as the event approaches. Ultimately it will then fade out after the event has begun. The process is outlined in steps below:

0

Step 1:

Program your Neopixel Ring to display a cool white by default.

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}

void loop() {

  uint16_t i;
  uint32_t c = strip.Color(255, 255, 255); // White

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, c );
    strip.show();
    delay( 100 );
  }
	
	delay( 100 );

}
Click to Expand
0

Step 2:

Add a Particle Cloud function and use it to change the color to red immediately. Test this with the Particle Console. 

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

bool meetingStatus = false;

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  Particle.function("meeting_soon", meetingSoon);
}

int meetingSoon(String arg){
    
    if (arg == "yes"){
        meetingStatus = true;
    } else if (arg == "no") {
        meetingStatus = false;
    }
    return 0;
}

void loop() {

  uint32_t white = strip.Color(255, 255, 255); // White
  uint32_t red = strip.Color(255, 0, 0); // red

  if (meetingStatus == true) {
      
    for( int i=0; i< strip.numPixels(); i++) {
        strip.setPixelColor(i, red);
        strip.setBrightness(20);
    }
    strip.show();
    delay( 100 );
    
  } else {
      
    for( int i=0; i< strip.numPixels(); i++) {
        strip.setPixelColor(i, white);
        strip.setBrightness(20);
    }
    strip.show();
    delay( 100 );
      
  }
  

}
Click to Expand
0

Step 3:

Program your Neopixel to fade to red over 1 minute using millis(). Starting with a much faster transition will help you quickly test, iterate and debug it.

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

bool meetingStatus = false;

unsigned long timeAtStart = 0;
unsigned long timeToFade = (60*1000); //one minute

float redVal = 255.0;
float greenVal = 255.0;
float blueVal = 255.0;

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  Particle.function("meeting_soon", meetingSoon);
}

int meetingSoon(String arg){
    
    timeAtStart = millis();
    if (arg == "yes"){
        meetingStatus = true;
    } else if (arg == "no") {
        meetingStatus = false;
    }
    return 0;
}

void loop() {

  uint32_t white = strip.Color(255, 255, 255); // White
  uint32_t red = strip.Color(255, 0, 0); // red
  uint32_t currentColor;

  if (meetingStatus == true) {
      
    unsigned long now = millis();
    
    while (now - timeAtStart <= timeToFade){
        now = millis();
        greenVal -= (255/60);
        blueVal -= (255/60);
        currentColor = strip.Color(255, greenVal, blueVal);
        for( int i=0; i< strip.numPixels(); i++) {
            strip.setPixelColor(i, currentColor);
            strip.setBrightness(20);
        }
        strip.show();
        delay(1000);
    }

  } else {
      
    for( int i=0; i< strip.numPixels(); i++) {
        strip.setPixelColor(i, white);
        strip.setBrightness(20);
    }
    strip.show();
    delay( 100 );
      
  }
  

}
Click to Expand
0

Step 4:

After the initial transition, now program your Neopixel to fade from red back to white over 1 minute using millis()

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

bool meetingStatus = false;
bool redOn = false;

unsigned long timeAtStart = 0;
unsigned long timeToFade = (60*1000); //one minute

float redVal = 255.0;
float greenVal = 255.0;
float blueVal = 255.0;

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  Particle.function("meeting_soon", meetingSoon);
}

int meetingSoon(String arg){
    
    timeAtStart = millis();
    if (arg == "yes"){
        meetingStatus = true;
    } else if (arg == "no") {
        meetingStatus = false;
    }
    return 0;
}

void loop() {

  uint32_t white = strip.Color(255, 255, 255); // White
  uint32_t red = strip.Color(255, 0, 0); // red
  uint32_t currentColor;

  if (meetingStatus == true) {
      
    unsigned long now = millis();
    
    while (now - timeAtStart <= timeToFade){
        now = millis();
        greenVal -= (255/60);
        blueVal -= (255/60);
        currentColor = strip.Color(255, greenVal, blueVal);
        for( int i=0; i< strip.numPixels(); i++) {
            strip.setPixelColor(i, currentColor);
            strip.setBrightness(20);
        }
        strip.show();
        delay(1000);
    }
    redOn = true;

  }else if (meetingStatus == false && redOn == true){
    greenVal = 0.0;
    blueVal = 0.0;
    
    unsigned long now = millis();
    
    while (now - timeAtStart <= timeToFade){
        now = millis();
        greenVal += (255/60);
        blueVal += (255/60);
        currentColor = strip.Color(255, greenVal, blueVal);
        for( int i=0; i< strip.numPixels(); i++) {
            strip.setPixelColor(i, currentColor);
            strip.setBrightness(20);
        }
        strip.show();
        delay(1000);
    }
    redOn = false;
  
  
  } else {
      
    for( int i=0; i< strip.numPixels(); i++) {
        strip.setPixelColor(i, white);
        strip.setBrightness(20);
    }
    strip.show();
    delay( 100 );
    
    redOn = false;
      
  }
  

}
Click to Expand
0

Step 5:

With those working as quick transitions, test them working over 15 minutes

0
// This #include statement was automatically added by the Particle IDE.
#include <neopixel.h>

// IMPORTANT: Set pixel COUNT, PIN and TYPE
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812

Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);

bool meetingStatus = false;
bool redOn = false;

unsigned long timeAtStart = 0;
unsigned long timeToFade = (60*15000); //15 minutes

float redVal = 255.0;
float greenVal = 255.0;
float blueVal = 255.0;

void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  
  Particle.function("meeting_soon", meetingSoon);
}

int meetingSoon(String arg){
    
    timeAtStart = millis();
    if (arg == "yes"){
        meetingStatus = true;
    } else if (arg == "no") {
        meetingStatus = false;
    }
    return 0;
}

void loop() {

  uint32_t white = strip.Color(255, 255, 255); // White
  uint32_t red = strip.Color(255, 0, 0); // red
  uint32_t currentColor;

  if (meetingStatus == true) {
      
    unsigned long now = millis();
    
    while (now - timeAtStart <= timeToFade){
        now = millis();
        greenVal -= (255/60);
        blueVal -= (255/60);
        currentColor = strip.Color(255, greenVal, blueVal);
        for( int i=0; i< strip.numPixels(); i++) {
            strip.setPixelColor(i, currentColor);
            strip.setBrightness(20);
        }
        strip.show();
        delay(15000);
    }
    redOn = true;

  }else if (meetingStatus == false && redOn == true){
    greenVal = 0.0;
    blueVal = 0.0;
    
    unsigned long now = millis();
    
    while (now - timeAtStart <= timeToFade){
        now = millis();
        greenVal += (255/60);
        blueVal += (255/60);
        currentColor = strip.Color(255, greenVal, blueVal);
        for( int i=0; i< strip.numPixels(); i++) {
            strip.setPixelColor(i, currentColor);
            strip.setBrightness(20);
        }
        strip.show();
        delay(15000);
    }
    redOn = false;
  
  
  } else {
      
    for( int i=0; i< strip.numPixels(); i++) {
        strip.setPixelColor(i, white);
        strip.setBrightness(20);
    }
    strip.show();
    delay( 100 );
    
    redOn = false;
      
  }
  

}
Click to Expand
0

Step 6:

Finally and once you’re happy with the behavior, hook it up to IFTTT.

0

Reflection & Next Steps

I really enjoyed this Skills Dev, and it provided a nice opportunity to test out what I've learned so far about for loops, millis(), Neopixels, and the Particle Cloud functions and Console app. It was also extremely helpful to follow the Lab site's suggestion of breaking the practice exercise in small steps, rather than jumping straight into IFTTT. This is a method I will definitely implement with future projects.

Next, I will be moving onto Skills Dev 4!

x