Ok, first off, I am super green. I am a mechanical engineer, so this is definitely not my area of expertise. I am making an art piece for a client and the requirements are as follows:
Battery powered (6600mAh Li Ion Battery)
LEDs that are individually addressable (Adafruit NeoPixels)
Remote operation (Simple 315mHz RF Transmitter/Receiver)
Rechargable (Adafruit Powerboost 500 + Charger)
Controller (Pro Trinket 5V)
I am using two downloaded libraries, EnableInterrupt and Adafruit_Neopixel, along with some avr libraries to get the hardware interrupt to work. I am using the hardware interrupt on pin 3 to activate a sleep mode.
For some reason, when I try to Verify or Upload the sketch, it just gets stuck and does not complete the process. The status bar just kind of hangs at around 30%. The software is not frozen, because I can still work in the environment and save. I have been using this software for a while and have never had this problem before.
I am thinking there is something in my code that is hanging up the compiler, but I am not sure what it could be. The code is definitely not completely thought out yet, and I am sure there are problems with it, but I can not find them because the compiler will not give me error messages. This is the first time I have ever used interrupts, or a sleep function, so if you see something strange in my code, it would love to know so I can address it. Thanks in advance!
#include <EnableInterrupt.h>
#include <Adafruit_NeoPixel.h>
#include <avr/interrupt.h>
#include <avr/power.h>
#include <avr/sleep.h>
#include <avr/io.h>
#define sleepPin 3
#define upPin 4
#define modePin 5
#define downPin 6
#define ledQty 4
#define NEEDFORSPEED
volatile byte flashSpeed = 127;
volatile byte mode = 63;
byte randNumber;
byte modeInc = 64;
byte speedInc = 32;
#define ledDataPin 8
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
// NEO_KHZ800 800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
// NEO_KHZ400 400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
// NEO_GRB Pixels are wired for GRB bitstream (most NeoPixel products)
// NEO_RGB Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(ledQty, ledDataPin, NEO_GRB + NEO_KHZ800);
void setup() {
strip.begin();
strip.show(); //initialize all pixels to off state
pinMode(upPin, INPUT);
pinMode(modePin, INPUT);
pinMode(downPin, INPUT);
pinMode(9,INPUT_PULLUP);
pinMode(10,INPUT_PULLUP);
pinMode(11,INPUT_PULLUP);
pinMode(12,INPUT_PULLUP);
pinMode(13,INPUT_PULLUP);
pinMode(sleepPin, INPUT_PULLUP);
enableInterrupt(sleepPin, sleepNow, CHANGE);
pinMode(upPin, INPUT_PULLUP);
enableInterrupt(upPin, upFunction, CHANGE);
pinMode(modePin, INPUT_PULLUP);
enableInterrupt(modePin, modeFunction, CHANGE);
pinMode(downPin, INPUT_PULLUP);
enableInterrupt(downPin, downFunction, CHANGE);
}
void loop() {
if (mode == 63)
{
pulse(flashSpeed);
}
else if (mode == 127)
{
blinking();
}
else if (mode == 191)
{
solid();
}
else
{
randomized();
}
}
byte upFunction() {
int val;
val == flashSpeed + speedInc;
flashSpeed == val;
}
byte modeFunction() {
int val;
val == mode + modeInc;
mode == val;
}
byte downFunction() {
int val;
val == flashSpeed - speedInc;
flashSpeed = val;
}
void pulse(uint8_t flashSpeed) {
uint16_t i, j;
for(j=0; j<256; j++) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, j,0,0);
}
strip.show();
millis(flashSpeed);
}
for(j=255; j>0; j--) {
for(i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, j,0,0);
}
strip.show();
millis(flashSpeed);
}
}
void blinking() {
strip.setPixelColor(j,0,0);
strip.show();
millis(flashSpeed);
strip.setPixelColor(0,0,0);
strip.show();
millis(flashSpeed);
}
void solid() {
strip.setPixelColor(j,0,0);
strip.show();
millis(1000);
}
void randomized() {
randNumber = random(ledQty);
pixels.setPixelColor(randNumber, pixels.Color(255,0,0));
pixels.show(); // This sends the updated pixel color to the hardware.
millis(flashSpeed); // Delay for a period of time (in milliseconds).
}
void sleepNow(void)
{
// Set pin 3 as interrupt and attach handler:
attachInterrupt(1, pinInterrupt, LOW);
delay(100);
//
// Choose our preferred sleep mode:
set_sleep_mode(SLEEP_MODE_IDLE);
//
// Set sleep enable (SE) bit:
sleep_enable();
//
// Put the device to sleep:
strip.show(); // turn LED strip off
sleep_mode();
//
// Upon waking up, sketch continues from this point.
sleep_disable();
}
//
void pinInterrupt(void)
{
detachInterrupt(1);
}