Yuchuan Shan – Skills Dev III
Made by Yuchuaun Shan
Made by Yuchuaun Shan
Ambient Orb & Neopixel Ring
Created: November 17th, 2021
#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();
// Initialize all pixels to "off"
strip.show();
}
void loop(){
uint16_t i;
uint32_t c = strip.Color(0, 25, 25);
uint32_t off = strip.Color(0, 0, 0);
// turn the pixel on one by one
for(i=0; i < strip.numPixels(); i++){
strip.setPixelColor(i, c);
strip.show();
delay(200);
}
delay(500);
// reverse the sequence and turn the pixel off one by one
for(i=strip.numPixels(); i > 0; i--){
strip.setPixelColor(i, off);
strip.show();
delay(200);
}
delay(500);
}
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();
// Initialize all pixels to "off"
strip.show();
//Register particle function
Particle.function("Set to Red", setRed);
Particle.function("Set to Green", setGreen);
Particle.function("set to Blue", setBlue);
}
void loop(){
}
int setRed(String command){
uint16_t i;
// convert the input command into int
// and store the value into the variable rValue
int rValue = command.toInt();
uint32_t red = strip.Color(rValue, 0, 0);
for(i=0; i < strip.numPixels(); i++){
strip.setPixelColor(i, red);
strip.show();
}
delay(200);
return 1;
}
int setGreen(String command){
uint16_t i;
// convert t a number gValue
// and store the value into the variable gValue
int gValue = command.toInt();
// use gValue to define the green color
uint32_t green = strip.Color(0, gValue, 0);
for(i=0; i < strip.numPixels(); i++){
strip.setPixelColor(i, green);
strip.show();
}
delay(200);
return 1;
}
int setBlue(String command){
uint16_t i;
// convert t a number bValue
// and store the value into the variable bValue
int bValue = command.toInt();
// use bValue to define the blue color
uint32_t blue = strip.Color(0, 0, bValue);
for(i=0; i < strip.numPixels(); i++){
strip.setPixelColor(i, blue);
strip.show();
}
delay(200);
return 1;
}
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);
// store rgb values in global variables
int r = 0;
int g = 0;
int b = 0;
// store the name of the color in a global variable
String npColor;
/////////// Ambient Orb/////////////////
void setup(){
strip.begin();
// initialize all pixels to "off"
strip.show();
Particle.function("Set color", setColor);
Particle.variable("brightness", brightness);
Particle.variable("Neopixel Color", npColor);
// set default color
setColor("white");
}
void loop(){
}
// function to set color to white or red
int setColor(String command){
// update the stored neopixel color value
npColor = command;
uint16_t i;
uint32_t white = strip.Color(100, 100, 100);
uint32_t red = strip.Color(100, 0, 0);
if (npColor == "white"){
// set rgb values all to 100 (white)
r = 100;
g = 100;
b = 100;
// turn on neopixel using the rgb values of white
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else if (npColor == "red"){
// set r=100, g & b = 0 (red)
r = 100;
g = 0;
b = 0;
// turn on neopixel using the rgb values of red
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else {
// if the command is neither white or red, return -1
return -1;
}
}
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);
// create a global variable to store the time when last performed an action
unsigned long lastFade = 0;
int brightness = 100;
// store rgb values in global variables
int r = 0;
int g = 0;
int b = 0;
// store the name of the color in a global variable
String npColor;
void setup(){
strip.begin();
// initialize all pixels to "off"
strip.show();
Particle.function("Set color", setColor);
Particle.function("Fade to red", fadeToRed);
Particle.variable("brightness", brightness);
Particle.variable("Neopixel Color", npColor);
// set default color
setColor("white");
}
void loop(){
//call the function to fade to red (input: given time in minute)
fadeToRed("1");
}
// function to fade from white to red
// function input: fading time in minute
int fadeToRed(String command){
// convert input command to milliseconds
int totalFadingTime = command.toInt();
int t = ((totalFadingTime * 60) * 1000) / 100;
unsigned long timeNow = millis();
if(brightness <= 100){
if (timeNow - lastFade > t){
for(int i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, brightness, brightness);
}
strip.show();
brightness = brightness-1;
lastFade = timeNow;
}
} else {
return -1;
}
return 1;
}
// function to set color to white or red
int setColor(String command){
// update the stored neopixel color value
npColor = command;
uint16_t i;
uint32_t white = strip.Color(100, 100, 100);
uint32_t red = strip.Color(100, 0, 0);
if (npColor == "white"){
// set rgb values all to 100 (white)
r = 100;
g = 100;
b = 100;
// turn on neopixel using the rgb values of white
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else if (npColor == "red"){
// set r=100, g & b = 0 (red)
r = 100;
g = 0;
b = 0;
// turn on neopixel using the rgb values of red
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else {
// if the command is neither white or red, return -1
return -1;
}
}
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);
// create a global variable to store the time when last performed an action
unsigned long lastFade = 0;
int brightness = 100;
// store rgb values in global variables
int r = 0;
int g = 0;
int b = 0;
// store the name of the color in a global variable
String npColor;
void setup(){
strip.begin();
// initialize all pixels to "off"
strip.show();
Particle.function("Set color", setColor);
Particle.function("Fade to white", fadeToWhite);
Particle.variable("brightness", brightness);
Particle.variable("Neopixel Color", npColor);
// set default color
setColor("red");
}
void loop(){
//call the function to fade to red (input: given time in minute)
fadeToWhite("1");
}
// function to fade from red to white
// function input: fading time in minute
int fadeToWhite(String command){
// convert input command to milliseconds
int totalFadingTime = command.toInt();
int t = ((totalFadingTime * 60) * 1000) / 100;
unsigned long timeNow = millis();
if(brightness <= 100){
if (timeNow - lastFade > t){
for(int i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, 100-brightness, 100-brightness);
}
strip.show();
brightness = brightness-1;
lastFade = timeNow;
} else{
return -1;
}
}
return 1;
}
// function to set color to white or red
int setColor(String command){
// update the stored neopixel color value
npColor = command;
uint16_t i;
uint32_t white = strip.Color(100, 100, 100);
uint32_t red = strip.Color(100, 0, 0);
if (npColor == "white"){
// set rgb values all to 100 (white)
r = 100;
g = 100;
b = 100;
// turn on neopixel using the rgb values of white
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else if (npColor == "red"){
// set r=100, g & b = 0 (red)
r = 100;
g = 0;
b = 0;
// turn on neopixel using the rgb values of red
for(i = 0; i < strip.numPixels(); i++){
strip.setPixelColor(i, r, g, b);
strip.show();
}
delay(100);
return 1;
} else {
// if the command is neither white or red, return -1
return -1;
}
}
Click to Expand
This ambient orb exercise familiarixed me with using millis(). I had a lot of issues using the function in the beginning. Then I realized that the problem was that the time returned by millis() is done by the internal clock in Particle, and it calculates the time since the device began running the current program, and NOT the time lapsed since the function was called.
I also got more practice creating functions and learned how it can be triggered in the console as well as IFTTT. I realized it is more convenient to keep a block of code as a its own function than putting everything in the setup loop. The downside is that if the function only needs to be called one time, then there needs to be a conditional control in setup to make sure it only runs once.