Skill Dev 3: Jenny
Made by Yanling Zhang
Made by Yanling Zhang
Created: November 9th, 2021
Modify the code to light up pixel by pixel then reverse the sequence turning each pixel off one by one
#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
#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
#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
#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
#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
We’ll create a simple version of the ambient orb first. It will work as follows:
#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
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;
}
// 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
#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