I wanted to just have a blinking light that alternates from white to red and wanted it to last awhile on a set of AA batteries.
I calculated that in maximum brightness and standard blinking rate (you can control these) it should run for 3 days with constant use.
NOTE: my math could be wrong. I took two AA batteries and doubled the mAh. I'm not sure if this is correct; because I would
be arranging the batteries to give me not 1.5volts but 3volts. Would that mean I shouldn't double the mAh ?
Controls:
Hold Left Button -- Turn light brightness down low
Hold Right Button -- Turn light brightness up high
Switch Left -- Disable lights (low power drain 15mA for 11 days)
Switch Right -- Enable lights
Touch Pin10 -- Toggle between Fast Blink and Normal Blink
Code: Select all
#include <Adafruit_CircuitPlayground.h>
/*
* This application is to be used with the Circuit Playground.
*
* Written by: James Laderoute
* Date: 1/21/2018
*
* Purpose:
* When going for a walk at night, wearing this will make
* you more visible to oncoming traffic. It blinks the
* lights on and off and alternates RED and WHITE to make you
* very visible.
*
* Controls:
* Hold Left Button -- Turn light brightness down low
* Hold Right Button -- Turn light brightness up high
* Switch Left -- Disable lights (low power drain)
* Switch Right -- Enable lights
* Touch Pin10 -- Toggle between Fast Blink and Normal Blink
*
* Power:
* Each individual NeoPixel draws up to 60 milliamps at maximum
* brightness white (red + green + blue). In actual use though, it’s
* rare for all pixels to be turned on that way. When mixing colors
* and displaying animations, the current draw will be much less. It’s
* impossible to estimate a single number for all circumstances, but we’ve
* been using 1/3 this (20 mA per pixel) as a gross rule of thumb with
* no ill effects. -- https://learn.adafruit.com/adafruit-neopixel-uberguide/powering-neopixels
*
* When all NeoPixels are off, only the power indicator light is on. This
* drains about 15mA. Using 2xAA batteries (of good quality) you can run
* this for about 11 days. You should just get a battery pack with an on/off
* switch to save battery power.
*
* Using all the NeoPixels (10) at full power (white draws 60mA but red
* draws 20mA). Also, we have them turned off for 500mS at a time. So,
* whatever time we get with them on, we can double because half the time
* they will be off.
*
* RED 20mA for 500mS ~ 10mA per second
* OFF 0mA for 500mS ~ 0mA per second
* WHITE 60mA for 500mS ~ 30mA per second
* OFF 0mA for 500mS ~ 0mA per second
*
* So for 1 second we draw 40mA (30mA+10mA)
*
* Battery rated at 2000mAh x 2 = 4000mAh / 40mA = 100 hours = 4.1 days
* Add in pwrLight 15mA 4000mAh / 55mA = 72 hours = 3 days
*/
#define ANALOG_INPUT A10
#define NORMAL_BLINK_TIME 500
#define RAPID_BLINK_TIME 250
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
CircuitPlayground.begin();
CircuitPlayground.setBrightness(100);
CircuitPlayground.clearPixels();
CircuitPlayground.redLED(false);
uint16_t value = analogRead(ANALOG_INPUT);
delay(10);
}
void allPixelsOn( int red, int green, int blue) {
for ( uint16_t p = 0; p <= 9 ; p++ ) {
CircuitPlayground.setPixelColor(p, red, green, blue);
}
}
void loop() {
uint16_t red=0, green=0, blue=0;
static int colorMode = 0;
static int brightness = 255;
static bool lightsOn = true;
static int delay_amount_on = 500;
static int delay_amount_off = 500;
static bool delay_normal = true; // false = rapid, true = normal
static bool touched = false;
/*
* Check capacitive touch pin (flip between slow blink and rapid blink)
*/
uint16_t value = analogRead(ANALOG_INPUT);
//Serial.println( value, DEC);
if (value>15 && !touched )
{
touched = true;
delay_normal = ! delay_normal;
}
if ( value < 15 ) touched=false;
/*
* Look at the switch and buttons of Circuit Playground
*/
bool lightsOff = CircuitPlayground.slideSwitch(); // left-switch disables the lights (to save power)
uint8_t dimLights = CircuitPlayground.leftButton(); // very low brightness (good for testing things)
uint8_t brightLights = CircuitPlayground.rightButton(); // full brightness (walking at night)
if ( lightsOff )
{
if ( lightsOn )
{
CircuitPlayground.setBrightness(0);
lightsOn = false;
allPixelsOn(0,0,0);
}
delay(250); // watchdog sleep would be nice, but don't know how to do this with CircuitPlayground
}
else
{
lightsOn = true;
Serial.println( delay_normal, DEC);
/*
* NOTE: dimLights and brightLights can both be 0 at the
* same time. Which is why I have seperate if() statements
* here. Can not do condiitional if.
*/
if (dimLights)
brightness = 10;
if (brightLights)
brightness = 255;
/*
* Lights OFF (part of blink)
*/
CircuitPlayground.setBrightness(0);
CircuitPlayground.clearPixels();
delay( delay_normal ? NORMAL_BLINK_TIME : RAPID_BLINK_TIME );
/*
* Determine if we use WHITE or RED lights
*/
switch (colorMode) {
case 0:
red = 255;
green = 255;
blue = 255;
colorMode = 1;
break;
case 1:
red = 255;
green = 0;
blue = 0;
colorMode = 0; // NOTE: set this to 2 to get White-Red-Blue flashing
break;
case 2:
red = 0;
green = 0;
blue = 255;
colorMode = 0;
break;
default:
colorMode=0;
}
/*
* Lights ON (part of blink)
*/
CircuitPlayground.setBrightness(brightness);
allPixelsOn( red, green, blue);
delay( delay_normal ? NORMAL_BLINK_TIME : RAPID_BLINK_TIME );
}
}