My code runs as desired when plugged into the USB, but not when powered only by battery. I tried it on an identically wired project and got the same results, so I know it's not a wiring/hardware issue. I also eliminated any calls to the serial port.
When plugged in, the LED strip LEDRing is gray and black, which is the default resting state. When the acceleratometer breaks threshold, the LEDRing lights do a 2 part, 100ms each animation of alternating orangered and aqua before reverting back to the black and grey resting state.
However when it runs from battery power, the functionality of the buttons and sensors all still work, but the resting state of the LEDRing is Redorange and Aqua, as if it is permanently stuck in one of the animation frames.
/*Wiring notes
*
* buttons wired from Digital Pin thru button to GND
*
* ACCELEROMETER
* AD0 and GND to GND
* VCC to 5V
* SDA & SCL to Arduino SDA & SCL
*
*/
//include Ultrasonic library
#include "SR04.h"
//define ultrasonic pins
#define TRIG_PIN 15
#define ECHO_PIN 16
//initialize ultrasonic
SR04 sr04 = SR04(ECHO_PIN,TRIG_PIN);
long swish;
//define button pins
#define PinT1Up 2
#define PinT1Dn 3
#define PinT2Up 4
#define PinT2Dn 5
//include accelerometer libraries
#include "I2Cdev.h"
#include "MPU6050.h"
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
#include "Wire.h"
#endif
//initialize accel parameters
MPU6050 accelgyro;
long az;
long azoff=-15800;
long accelSens = 1200;
unsigned long timeSlam;
//define LED strip parameters
#include "FastLED.h"
#define NUM_LEDS_SCORE 21
#define NUM_LEDS_RING 29
#define T1LED_PIN 6
CRGB T1leds[NUM_LEDS_SCORE];
#define T2LED_PIN 7
CRGB T2leds[NUM_LEDS_SCORE];
#define RINGLED_PIN 8
CRGB Ringleds[NUM_LEDS_RING];
// initiate scores and buttons
int T1Score = 0;
int T2Score = 0;
byte T1UpState = HIGH;
byte T1DnState = HIGH;
byte T2UpState = HIGH;
byte T2DnState = HIGH;
int buttonDelay = 10;
unsigned long ms = 0;
void setup() {
// Serial.begin(9600);
//set pins up
pinMode(PinT1Up, INPUT_PULLUP);
pinMode(PinT1Dn, INPUT_PULLUP);
pinMode(PinT2Up, INPUT_PULLUP);
pinMode(PinT2Dn, INPUT_PULLUP);
//set up LED strips
FastLED.addLeds<NEOPIXEL, T1LED_PIN>(T1leds, NUM_LEDS_SCORE);
FastLED.addLeds<NEOPIXEL, T2LED_PIN>(T2leds, NUM_LEDS_SCORE);
FastLED.addLeds<NEOPIXEL, RINGLED_PIN>(Ringleds, NUM_LEDS_RING);
// join I2C bus (I2Cdev library doesn't do this automatically)
#if I2CDEV_IMPLEMENTATION == I2CDEV_ARDUINO_WIRE
Wire.begin();
#elif I2CDEV_IMPLEMENTATION == I2CDEV_BUILTIN_FASTWIRE
Fastwire::setup(400, true);
#endif
// initialize device
//Serial.println("Initializing I2C devices...");
accelgyro.initialize();
// verify connection
//Serial.println("Testing device connections...");
//Serial.println(accelgyro.testConnection() ? "MPU6050 connection successful" : "MPU6050 connection failed");
//get accel offset from resting average accel
{
unsigned int q = abs(accelgyro.getAccelerationZ());
delay (10);
unsigned int r = abs(accelgyro.getAccelerationZ());
delay (10);
unsigned int s = abs(accelgyro.getAccelerationZ());
azoff = ((q + r + s) / 3);
}
}
//LLLLLLLLLLLLOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOPPPPPPPPPPPPPPPPP
void loop() {
//get button states
T1UpState = digitalRead(PinT1Up);
T1DnState = digitalRead(PinT1Dn);
T2UpState = digitalRead(PinT2Up);
T2DnState = digitalRead(PinT2Dn);
//set current millis time to variable
ms = millis();
//check for bag landing and flash ring
az = long(accelgyro.getAccelerationZ());
if (abs(abs(az)-azoff) > accelSens){
timeSlam = ms;
}
//check for bag swish and rainbow ring
swish=sr04.Distance();
if (swish < 25 && swish > 0){
for (int n = 0; n < NUM_LEDS_RING+40; n++){
Ringleds[max(n,0)] = CRGB::Red;
Ringleds[max(n-1,0)] = CRGB::OrangeRed;
Ringleds[max(n-2,0)] = CRGB::Yellow;
Ringleds[max(n-3,0)] = CRGB::Green;
Ringleds[max(n-4,0)] = CRGB::Blue;
Ringleds[max(n-5,0)] = CRGB::Purple;
Ringleds[max(n-6,0)] = CRGB::Black;
Ringleds[max(n-13,0)] = CRGB::Red;
Ringleds[max(n-14,0)] = CRGB::OrangeRed;
Ringleds[max(n-15,0)] = CRGB::Yellow;
Ringleds[max(n-16,0)] = CRGB::Green;
Ringleds[max(n-17,0)] = CRGB::Blue;
Ringleds[max(n-18,0)] = CRGB::Purple;
Ringleds[max(n-19,0)] = CRGB::Black;
Ringleds[max(n-27,0)] = CRGB::Red;
Ringleds[max(n-28,0)] = CRGB::OrangeRed;
Ringleds[max(n-29,0)] = CRGB::Yellow;
Ringleds[max(n-30,0)] = CRGB::Green;
Ringleds[max(n-31,0)] = CRGB::Blue;
Ringleds[max(n-32,0)] = CRGB::Purple;
Ringleds[max(n-33,0)] = CRGB::Black;
FastLED.show();
delay(20);
}
timeSlam = (ms - 200L); //cancel slam animation
}
//check button presses and increment scores
if ((T1UpState == LOW) && (T1DnState == HIGH))
{
T1Score++;
T1Score = constrain(T1Score, 0, 21);
while ((digitalRead(PinT1Up) == LOW) && (digitalRead(PinT1Dn) == HIGH));
delay(buttonDelay);
}
if ((T1UpState == HIGH) && (T1DnState == LOW))
{
T1Score--;
T1Score = constrain(T1Score, 0, 21);
while ((digitalRead(PinT1Up) == HIGH) && (digitalRead(PinT1Dn) == LOW));
delay(buttonDelay);
}
if ((T2UpState == LOW) && (T2DnState == HIGH))
{
T2Score++;
T2Score = constrain(T2Score, 0, 21);
while ((digitalRead(PinT2Up) == LOW) && (digitalRead(PinT2Dn) == HIGH));
delay(buttonDelay);
}
if ((T2UpState == HIGH) && (T2DnState == LOW))
{
T2Score--;
T2Score = constrain(T2Score, 0, 21);
while ((digitalRead(PinT2Up) == HIGH) && (digitalRead(PinT2Dn) == LOW));
delay(buttonDelay);
}
//show scores on LED strips
for (int i = 0; i < T1Score; i++) {
T1leds[20-i] = CRGB::Aqua;
if ((20-i-1) % 5 == 0) {
T1leds[20-i] = CRGB::OrangeRed;
}
}
for (int j = T1Score; j<21; j++) {
T1leds[20-j] = CRGB::Black;
}
for (int k = 0; k < T2Score; k++) {
T2leds[20-k] = CRGB::OrangeRed;
if ((20-k-1) % 5 == 0) {
T2leds[20-k] = CRGB::Aqua;
}
}
for (int m = T2Score; m<21; m++) {
T2leds[20-m] = CRGB::Black;
}
if((ms - timeSlam) < 100){
for (int n = 0; n < NUM_LEDS_RING; n++){
if (n % 2 == 0) Ringleds[n] = CRGB::Aqua;
else Ringleds[n] = CRGB::OrangeRed;//Slammer animation frame 1
}
} else if((ms - timeSlam) < 200){
for (int n = 0; n < NUM_LEDS_RING; n++){
if (n % 2 == 0) Ringleds[n] = CRGB::OrangeRed;
else Ringleds[n] = CRGB::Aqua;//Slammer animation frame 2
}
} else {
for (int n = 0; n < NUM_LEDS_RING; n++){
if (((n+1) % 4) == 0) Ringleds[n] = CRGB::DarkGrey;
else Ringleds[n] = CRGB::Black;
}
}
FastLED.show();
}
