Dear all,
I have a question regarding a project that I build with a combination of Arduino and Adafruit/brandless products, using the Arduino IDE. The project in short consists of an IR sensor and remote, an Adafruit Trinket (or Arduino Uno, I also tried with that...) and a LED ring with four different light settings, linked to specific sensor values from the remote control.
Now the issue is that I want all of the functions to loop infinitely, whilst allowing me to change to another function if I press a different button on the remote.
Right now it works as following; if I press "1" it repeats the program infinitely with an unwanted delay between the repeats, but if I press another button while the program runs it doesn't react. Only when I press exactly after one cycle of the first program I am able to switch it. Can somebody help me with how I can achieve this? Is it in the break part of the switchcase function?
/* This is a sketch for the belt project, used on 24/11/2015 on the Adafruit Trinket
ATTiny85 based mini microcontroller and a PNA4602 or TSOP38238 to
read an IR code and perform a function.
This program includes modes that are IR controlled for a NeoPixel ring. The individual modes are explained in the code.
*/
#include <Adafruit_NeoPixel.h>
// We need to use the 'raw' pin reading methods because timing is very important here
// and the digitalRead() procedure is slower!
#define IRpin_PIN PINB // ATTiny85 had Port B pins
#define IRpin 2 // IR sensor - TSOP38238 on Pin GPIO #2 / D2
#define RingPin 1 // DConnects to Adafruit Neopixel Ring
#define MAXPULSE 5000 // the maximum pulse we'll listen for - 5 milliseconds
#define NUMPULSES 100 // max IR pulse pairs to sample
#define RESOLUTION 2 // // time between IR measurements
#define NumPixels 24 // number of pixels
Adafruit_NeoPixel strip = Adafruit_NeoPixel(24, 1, NEO_GRB + NEO_KHZ800);
// we will store up to 40 pulse pairs (reduce if needed)
uint16_t pulses[40][2]; // pair is high and low pulse
uint16_t currentpulse = 0; // index for pulses we're storing
uint32_t irCode = 0;
uint16_t n;
uint32_t irCodeRead;
void setup() {
pinMode(IRpin, INPUT); // Listen to IR receiver on Trinket pin D2
pinMode(RingPin, OUTPUT); // Output tones on Trinket pin D1
strip.begin();
cli(); // disables interrupts
strip.show(); // generates the NeoPixel control signals
sei(); // re-enables interrupts; // Initialize all pixels to 'off'
}
void loop() {
irCode=listenForIR(); // Wait for an IR Code
// Process the pulses to get our code
for (int i = 0; i < 32; i++) {
irCode=irCode<<1;
if((pulses[i][0] * RESOLUTION)>0&&(pulses[i][0] * RESOLUTION)<500) {
irCode|=0;
} else {
irCode|=1;
int rccode = irCode;
}
}
switch (irCode) {{
//ACTION ONE: TURNING WHITE AROUND THE CIRCLE, "STARTING ENERGY BELT"
//PRESS BUTTON "1" *Note: white color updated to softer
case(0xC03FDA25) : { // "1" on my remote
colorWipe(strip.Color(225, 250, 180), 60); // White
colorWipe(strip.Color(56.25, 56.25, 45), 60); // White
colorWipe(strip.Color(225, 250, 180), 60); // White
colorWipe(strip.Color(56.25, 56.25, 45), 60); // White
break;
strip.show();
}
//ACTION TWO: SKIN COLOR "CHARGING"
//PRESS BUTTON "2"
//IMPORTANT: to change speed, change max int i value in code since i increases at constant rate
case(0xC03FE619) : { // "2" on my remote
for (int i = 0 ; i < 51; i++) {
colorWipe(strip.Color((5*i), (1.96078431*i), (0.39215686*i)), 1); // Skin Color
}
break;
}
//ACTION THREE: FADING RED, "RUNNING OUT OF BODY FAT"
//PRESS BUTTON "3"
//IMPORTANT: to change speed, change max int i value in code since i increases at constant rate
case(0xC03FEC13) : { // "3" on my remote
for (int i = 0 ; i < 51; i++) {
colorWipe(strip.Color((5*i), (0.1*i), (0)), 1); // Full red
}
for (int i = 51 ; i >-1; i--) {
colorWipe(strip.Color((5*i), (0.1*i), (0)), 1);
}
delay(30);
for (int i = 0 ; i < 51; i++) {
colorWipe(strip.Color((5*i), (0.1*i), (0)), 1); // Full red
}
for (int i = 51 ; i >-1; i--) {
colorWipe(strip.Color((5*i), (0.1*i), (0)), 1);
}}
break;
}
}}
uint16_t listenForIR() { // IR receive code
currentpulse = 0;
while (1) {
unsigned int highpulse, lowpulse; // temporary storage timing
highpulse = lowpulse = 0; // start out with no pulse length
while (IRpin_PIN & _BV(IRpin)) { // got a high pulse
highpulse++;
delayMicroseconds(RESOLUTION);
if (((highpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
return currentpulse;
}
}
pulses[currentpulse][0] = highpulse;
while (! (IRpin_PIN & _BV(IRpin))) { // got a low pulse
lowpulse++;
delayMicroseconds(RESOLUTION);
if (((lowpulse >= MAXPULSE) && (currentpulse != 0))|| currentpulse == NUMPULSES) {
return currentpulse;
}
}
pulses[currentpulse][1] = lowpulse;
currentpulse++;
}
}
void colorWipe(uint32_t c, uint8_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}