Hello and thank you for your time,
I need a little help/direction with my IR remote code. I made a simplified working sketch of what I am trying to do. In the included sketch there are two functions that I would like to call by using an IR remote. The functions work well on their own but when I put them into a switch case statement for the IR remote it only runs the first millisecond (or so) of the function and stops. If I keep pushing the button it will run a little more with each button push. I need a way to write the sketch so it will loop the function while also check for additional IR button presses.
#include "Tlc5940.h"
#include <IRremote.h>
int RECV_PIN = 46;
IRrecv irrecv(RECV_PIN);
decode_results results;
unsigned long previousMillis1 = 0;
unsigned long interval1 = 10;
int fadeValue1 = 0;
int redfadeValue1 = 0;
int greenfadeValue1 = 0;
int bluefadeValue1 = 0;
int led1State = 0;
boolean next1 = false;
void setup(){
Serial.begin(9600);
irrecv.enableIRIn();
Tlc.init();
led1State = 0;
}
void loop()
{
if (irrecv.decode(&results)) {
switch(results.value) {
case 0xFF6897:
Serial.println("1");
test1();
break;
case 0xFF9867:
Serial.println("2");
test2();
break;
}
irrecv.resume();
}
}
void test1(){
switch (led1State) {
case 0:
{
interval1 = 4;
magentaOn1();
if (next1){
next1 = false;
led1State = 1;
}
break;
}
case 1:
{
interval1 = 6;
magentaOff1();
if (next1){
next1 = false;
led1State = 2;
}
break;
}
case 2:
{
interval1 = 7;
blueOn1();
if (next1){
next1 = false;
led1State = 3;
}
break;
}
case 3:
{
interval1 = 6;
blueOff1();
if (next1){
next1 = false;
led1State = 0;
}
break;
}
}
}
void test2(){
switch (led1State) {
case 0:
{
interval1 = 2;
whiteOn1();
if (next1){
next1 = false;
led1State = 1;
}
break;
}
case 1:
{
interval1 = 8;
whiteOff1();
if (next1){
next1 = false;
led1State = 2;
}
break;
}
case 2:
{
interval1 = 4;
blueOn1();
if (next1){
next1 = false;
led1State = 3;
}
break;
}
case 3:
{
interval1 = 8;
blueOff1();
if (next1){
next1 = false;
led1State = 0;
}
break;
}
}
}
void blueOn1(){
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 > interval1) {
previousMillis1 = currentMillis1;
if(bluefadeValue1 <= 4000, bluefadeValue1 +=5) {
Tlc.set(2, bluefadeValue1);
}
Tlc.update();
}
if(bluefadeValue1 >= 4000){
next1 = true;
}
}
void blueOff1(){
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 > interval1) {
previousMillis1 = currentMillis1;
if(bluefadeValue1 >=0, bluefadeValue1 -=5){
Tlc.set(2, bluefadeValue1);
}
Tlc.update();
}
if(bluefadeValue1 <= 0){
next1 = true;
}
}
void whiteOn1(){
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 > interval1) {
previousMillis1 = currentMillis1;
if(redfadeValue1 <= 2000, redfadeValue1 +=5, greenfadeValue1 <= 2000, greenfadeValue1 +=5, bluefadeValue1 <= 2000, bluefadeValue1 +=5) {
Tlc.set(0, redfadeValue1);
Tlc.set(1, greenfadeValue1);
Tlc.set(2, bluefadeValue1);
}
Tlc.update();
}
if(redfadeValue1 >= 2000){
next1 = true;
}
}
void whiteOff1(){
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 > interval1) {
previousMillis1 = currentMillis1;
if(redfadeValue1 >=0, redfadeValue1 -=5, greenfadeValue1 >=0, greenfadeValue1 -=5, bluefadeValue1 >=0, bluefadeValue1 -=5){
Tlc.set(0, redfadeValue1);
Tlc.set(1, greenfadeValue1);
Tlc.set(2, bluefadeValue1);
}
Tlc.update();
}
if(redfadeValue1 <= 0){
next1 = true;
}
}
void magentaOn1(){
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 > interval1) {
previousMillis1 = currentMillis1;
if(redfadeValue1 <= 4000, redfadeValue1 +=5, bluefadeValue1 <= 4000, bluefadeValue1 +=5) {
Tlc.set(0, redfadeValue1);
Tlc.set(2, bluefadeValue1);
}
Tlc.update();
}
if(bluefadeValue1 >= 4000){
next1 = true;
}
}
void magentaOff1(){
unsigned long currentMillis1 = millis();
if(currentMillis1 - previousMillis1 > interval1) {
previousMillis1 = currentMillis1;
if(redfadeValue1 >=0, redfadeValue1 -=5, bluefadeValue1 >=0, bluefadeValue1 -=5){
Tlc.set(0, redfadeValue1);
Tlc.set(2, bluefadeValue1);
}
Tlc.update();
}
if(bluefadeValue1 <= 0){
next1 = true;
}
}