Skill Dev 3: Jenny

Made by Yanling Zhang

Created: November 9th, 2021

0

Exercise 1

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

0
#include "neopixel.h"

#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, 255, 255);

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

}
Click to Expand
0
iot SkillDev3 3 1
zhang yanling - https://youtu.be/avlebbn8R0w
0

Exercise 2

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

0
#include "neopixel.h"

#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;

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, 0,0,255 );//blue
  }
	strip.show();
	delay( 1000 );
	
  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, 255,0,0 );//red
  }
	strip.show();
	delay( 1000 );
	
  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, 0,255,255 );//green
  }
	strip.show();
	delay( 1000 );
	
	
  for(int i=0; i<strip.numPixels(); i++) {
     strip.setPixelColor(i, 255, 255, 255 );//white
  }
     strip.show();
     delay(1000);
  
	

}
Click to Expand
0
iot SkillDev3 3 2
zhang yanling - https://youtu.be/yT_xTdXZTQY
0

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
#include "neopixel.h"

#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;

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, 0,0,255 );//blue
    strip.show();
	delay( 1000 );
	strip.setPixelColor(i, 0, 0, 0 );
  }

}
Click to Expand
0
iot SkillDev3 3 3
zhang yanling - https://youtu.be/nS2106L41os
0

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

The first try was wrong but beautiful. 

I put the "strip.show()" in the void loop, so the new pixel changed color when the previous pixel remain the old color.

0
#include "neopixel.h"

#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int red = 0;
int green = 0;
int blue = 0;


void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  Particle.function("Red", setRed);
  Particle.function("Green", setGreen);
  Particle.function("Blue", setBlue);
}



void loop() {
    
  uint16_t i;

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, red, green, blue);//blue
    strip.show();
	delay( 1000 );
	//strip.setPixelColor(i, 0, 0, 0 );
  }

}

int setRed(String command){
    int number = atoi(command);
    if(number >=0 && number <= 255){
        red = number;
    }
    return red;
}

int setGreen(String command){
    int number = atoi(command);
    if(number >=0 && number <= 255){
        green = number;
    }
    return green;
}

int setBlue(String command){
    int number = atoi(command);
    if(number >=0 && number <= 255){
        blue = number;
    }
    return blue;
}
Click to Expand
0
iot SkillDev3 3 4 1
zhang yanling - https://youtu.be/kEzTBoEJh2s
0

I moved strip.show(); out of the void loop.

Now after every pixel get the new color information, the Neopixel LED will display the color as a whole.

0
#include "neopixel.h"
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int red = 0;
int green = 0;
int blue = 0;


void setup() {

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
  Particle.function("Red", setRed);
  Particle.function("Green", setGreen);
  Particle.function("Blue", setBlue);
}



void loop() {

  uint16_t i;

  for(i=0; i< strip.numPixels(); i++) {
    strip.setPixelColor(i, red, green, blue);//blue
	//strip.setPixelColor(i, 0, 0, 0 );
  }
   strip.show();
// 	delay( 1000 );

}

int setRed(String command){
    int number = atoi(command);
    if(number >=0 && number <= 255){
        red = number;
    }
    return red;
}

int setGreen(String command){
    int number = atoi(command);
    if(number >=0 && number <= 255){
        green = number;
    }
    return green;
}

int setBlue(String command){
    int number = atoi(command);
    if(number >=0 && number <= 255){
        blue = number;
    }
    return blue;
}
Click to Expand
0
iot SkillDev3 3 4 2
zhang yanling - https://youtu.be/hnQtAFuZWWE
0

Simple Ambient Orb

We’ll create a simple version of the ambient orb first. It will work as follows:

  1. You’ll set up IFTTT and connect it to your Google Calendar.
  2. You’ll create a Particle function in your code.
  3. You’ll connect the two so that 15 minutes before a meeting on your calendar, IFTTT will call your Particle function.
  4. Over the 15 minutes between the Particle function being called, and the start of the meeting, program your Neopixel to slowly fade to red.
  5. For 15 minutes after your meeting was due to start have it slowly fade back to white (the inverse of step 5)
0

At first, I let redPercentage subtracts 50 every time, but in the last loop, the redPercentage will go below 0, which causes errors in LED display.   So I realized I need to set up some number that can divisible by 225.

0
#include "neopixel.h"
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
unsigned long lastFade = 0;
int redPercentage=255;


void setup() {

  strip.begin();
  strip.show(); 
}


void loop() {

	unsigned long now = millis();
	if ((now - lastFade) >= 1000) {
		if (redPercentage <= 255 && redPercentage > 0 ) {
		    
		    if(redPercentage < 50) {
		        redPercentage = 0;
		    } else {
		        redPercentage = redPercentage - 50;     
		    }
		    
		}
		lastFade=now;
	}
	
    for(uint16_t i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, 255, redPercentage, redPercentage);
    }

	strip.show();

}
Click to Expand
0

I changed the number from 50 to 2.5, and added a new cloud function "Red" to turn on the light. It took a long time to figure out how to connect to ifttt platform. I changed the function command from string to number because I don't why string don't work.

int TurnOn(String command){

if(command==TurnOn){

}


int setRed(String command){

int number = atoi(command);

if(number ==1){

red = number;

}

return red;

}

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

#include "neopixel.h"
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int red = 0;
unsigned long lastFade = 0;
int redPercentage=255;
// int state=HIGH;


void setup() {

  strip.begin();
  strip.show(); 
  Particle.function("Red", setRed);

}


void loop() {
    

	unsigned long now = millis();
// 	int state = HIGH;
if(red == 1){
	    
	    if ((now - lastFade) >= 500) {
		if (redPercentage <= 255 && redPercentage > 0 ) {
			redPercentage = redPercentage - 2.5; 
		}
		lastFade=now;
	    }
	
    for(uint16_t i=0; i < strip.numPixels(); i++) {
        strip.setPixelColor(i, 255, redPercentage, redPercentage);
    }
	strip.show();
	    
}
	
}
  
int setRed(String command){
    int number = atoi(command);
    if(number ==1){
        red = number;
    }
    return red;
}
Click to Expand
0

With the help of the Jaki, The code finally worked! After the ifttt platform gives the signal, it turns from white to red, and then turns from red to white.

0
#include "neopixel.h"
#define PIXEL_PIN D2
#define PIXEL_COUNT 16
#define PIXEL_TYPE WS2812
 
Adafruit_NeoPixel strip = Adafruit_NeoPixel(PIXEL_COUNT, PIXEL_PIN, PIXEL_TYPE);
int red = 0;
unsigned long lastFade = 0;
int redPercentage=255;
// bool goingRed = true;
int goingRed = 1;


void setup() {

  strip.begin();
  strip.show(); 
  Particle.function("Red", setRed);

}


void loop() {
    
    
	unsigned long now = millis();
    if(red == 1){
	    
	    if ((now - lastFade) >= 500) {
            if (goingRed == 1) {
    			redPercentage = redPercentage - 2.5; 
    // 			Particle.publish("It is going red now");
    			if(redPercentage <= 0){
    			    redPercentage = 0;
    			    goingRed = 0;
    			}
    		}
    		
    		else if(goingRed == 0) {
    		    redPercentage = redPercentage + 2.5;
    		  //  Particle.publish("It is going white now");
    		    if(redPercentage >= 255){
    		        redPercentage = 255;
    		        goingRed = 1;
    		        red = 0;
    		    }
    	    }
    	
            lastFade=now;
    
        }
        
	
    }
    
    for(uint16_t i=0; i < strip.numPixels(); i++) {
            strip.setPixelColor(i, 255, redPercentage, redPercentage);
    }
	
	strip.show();
}
	
  
int setRed(String command){
    int number = atoi(command);
    if(number == 1){
        red = 1;
    }
    return red;
}
Click to Expand
0
iot SkillDev3 3 5
zhang yanling - https://youtu.be/twcRxR1m7Pc
0

Reflection

1. Check the number carefully, sometimes 255.0 doesn't equal 255.

2. Don't set too many if conditions, because it is hard for understanding.

x