Hey all,
I am attempting to do a simple gesture-controlled LED tape project, where “swiping right” moves the LED tape forward through the colors of the rainbow (ROYGBIV) and “swiping left” moved the tape backwards through the colors of the rainbow (VIBGYOR). I got someone to write the code for me (I’m NOT a programmer, let me come clean about that right now) and I have a little prototype going. The issue is that the entire prototype seems to “time out” after a few minutes of inactivity. Examples:
This is the prototype after first booting up:
This is the prototype after 20 minutes of inactivity:
This is the code:
#include <Wire.h>
#include <SparkFun_APDS9960.h>
#include <Adafruit_NeoPixel.h>
//Variables for Neopixel
const int ledPin = 6; //Data pin of neopixel on Arduino pin 6
const int numLeds = 60; //Number of leds on the neopixel
Adafruit_NeoPixel strip = Adafruit_NeoPixel(numLeds, ledPin, NEO_GRB + NEO_KHZ800);
// Pins
#define APDS9960_INT 2 // Needs to be an interrupt pin
// Global Variables
SparkFun_APDS9960 apds = SparkFun_APDS9960();
int isr_flag = 0;
boolean goLeft = false;
boolean goRight = false;
int currentColor = 0;
int fadeInDelay = 0; //To see the change in color increase this delay (Don't increase too much)
void setup() {
// put your setup code here, to run once:
// Set interrupt pin as input
pinMode(APDS9960_INT, INPUT);
// Initialize Serial port
Serial.begin(9600);
Serial.println();
Serial.println(F("--------------------------------"));
Serial.println(F("SparkFun APDS-9960 - GestureTest"));
Serial.println(F("--------------------------------"));
// Initialize interrupt service routine
attachInterrupt(0, interruptRoutine, FALLING);
// Initialize APDS-9960 (configure I2C and initial values)
if ( apds.init() ) {
Serial.println(F("APDS-9960 initialization complete"));
} else {
Serial.println(F("Something went wrong during APDS-9960 init!"));
}
// Start running the APDS-9960 gesture sensor engine
if ( apds.enableGestureSensor(true) ) {
Serial.println(F("Gesture sensor is now running"));
} else {
Serial.println(F("Something went wrong during gesture sensor init!"));
}
//NeoPixel:
strip.begin();
strip.setBrightness(255); //Max brightness being 255
allPixels(130,255,255); //RGB value for white
}
void loop() {
// put your main code here, to run repeatedly:
if( isr_flag == 1 ) {
detachInterrupt(0);
handleGesture();
isr_flag = 0;
attachInterrupt(0, interruptRoutine, FALLING);
}
if(goLeft || goRight){
if(goLeft){
currentColor++;
}else{
currentColor--;
}
if(currentColor == 0){ currentColor = 7;}
if(currentColor == 8){currentColor = 1;}
changeColor(currentColor);
goLeft = false;
goRight = false;
}
}
void interruptRoutine() {
isr_flag = 1;
}
void handleGesture(){
if ( apds.isGestureAvailable() ) {
switch ( apds.readGesture() ) {
case DIR_UP:
Serial.println("UP");
goLeft = false;
goRight = false;
break;
case DIR_DOWN:
Serial.println("DOWN");
goLeft = false;
goRight = false;
break;
case DIR_LEFT:
Serial.println("LEFT");
goLeft = true;
goRight = false;
break;
case DIR_RIGHT:
Serial.println("RIGHT");
goLeft = false;
goRight = true;
break;
case DIR_NEAR:
Serial.println("NEAR");
goLeft = false;
goRight = false;
break;
case DIR_FAR:
Serial.println("FAR");
goLeft = false;
goRight = false;
break;
default:
Serial.println("NONE");
goLeft = false;
goRight = false;
}
}
}
void allPixels(byte r, byte g, byte b){
for(int i=0;i<strip.numPixels();i++){
strip.setPixelColor(i, r, g, b);
strip.show();
delay(fadeInDelay);
}
strip.show();
}
void changeColor(int data){
switch(data){
case 1:
allPixels(250,0,0); //RGB value for Red
break;
case 2:
allPixels(250,250,0); //RGB value for Orange
break;
case 3:
allPixels(100,240,0); //RGB value for Yellow
break;
case 4:
allPixels(0,250,0); //RGB value for Green
break;
case 5:
allPixels(0,0,250); //RGB value for Blue
break;
case 6:
allPixels(40,0,130); //RGB value for Indigo
break;
case 7:
allPixels(100,25,250); //RGB value for Magenta
break;
default:
allPixels(100,100,255); //RGB value for White
break;
}
}
Sorry about the poor quality of the schematic…the APDS-9960 is not in the library and I couldn’t figure out how to import an SVG.
If anyone has any suggestions on where to start on this thing, it would be greatly appreciated. I’ve already tried changing out APDS-9960 units. It seems to be a good sensor, just inconsistent in the results.