Hi everyone.
I have been working on a project for about a month and inching my way through. I just started learning programming and finished the book by Simon Monk and am having a blast discovering new things.
My project involves using a IR remote control to select from 2 or 3 different patterns of 4 led bulbs.
I am using the IRremote.h library and it works great. So far I have been successful in making the LED's power up as designed and I can change from my default mode (all LEDs on) to button-one selection which has them turn on -without delay() - in sequence.
I am using a do-while control imbedded in switch/case to keep the sequence going until I push another button however it freezes in that mode and cannot receive another signal during execution of that do-while loop. I am pretty sure that it is due to the " while (results.value=true);"
command but I'm not sure how else to write that. Basically I want to allow a new button to change to a different pattern but my IR signal is being blocked from execution.
My code it below. Please excuse my amateur style.
Any ideas?
#include <IRremote.h>
/*
This set up it with 4 LEDs and one remote control.
Must have IRremote.h from Library download from https://github.com/shirriff/Arduino-IRremote
*/
#include <IRremote.h>
int RECV_PIN = 8; // pin 8 connected to left(OUTPUT) pin of IR sensor
int redPin = 3; //RED LED mode indicator
int firstLEDPin = 4; // LED connected to digital pin 4
int secondLEDPin = 5; // LED connected to digital pin 5
int thirdLEDPin = 6; // LED connected to digital pin 6
int fourthLEDPin = 7; // LED connected to digital pin 7
IRrecv irrecv(RECV_PIN);
decode_results results;
boolean poweredOn = false;
//-------------------button 1 test declarations below--------------
// we'll need some variables
// first an array of our led pins declared globally
const byte ledPin[] = {
5,6,7}; // I don't like to make the variable plural
// how many leds do we have
byte ledCount = sizeof(ledPin) / sizeof(ledPin[0]);
// now the sequence
const byte sequence[] = {
0,1,2};
// it will help to know how many elements this array holds
byte sequenceCount = sizeof(sequence) / sizeof(sequence[0]);
// time between changes
const unsigned long duration = 1000; // it's best to use unsigned longs with timing variables
//---------------------------void setup()-----------------------------------
void setup()
{
Serial.begin(9600);
unsigned char i;
for(i=1;i<=7;i++) //set pins 1-11 as output
pinMode(i,OUTPUT);
pinMode(redPin, OUTPUT);
senseFlash(); //Called senseFlash() Red LED flashes fast. Means IR signal was sensed
//Below is to turn on all lights on setup incase the battery is low on the IR remote
allOn();
irrecv.enableIRIn(); // Start the receiver
pinMode(RECV_PIN, INPUT); //sets pin 8 as input
}
//----------------------------------void loop()------------------------------------------------
void loop() {
if (irrecv.decode(&results)) {
long int decCode = results.value;
Serial.println(decCode);
switch (results.value) {
//--------------------------Button 0 (power On/Off button)---------------------------------
case 16753245: //Button 0 (Power button) pressed
senseFlash(); //Called senseFlash() Red LED flashes fast. Means IR signal was sensed
if (poweredOn == true) {
allOff(); //call allOff function. Turns all LEDs off
poweredOn = false;
idleFade(); //slowly fades red LED in/and out
}
else if(poweredOn == false) {
allOn(); //call allOn function. Turns all LEDs on
poweredOn =true;
}
break;
//--------------------------Button 1---------------------------------
case 16724175: //Button 1 pressed
senseFlash(); //Called senseFlash() Red LED flashes fast. Means IR signal was sensed
Serial.println("Button 1"); //displays "button 4" on serial monitor
allOff(); //turn off any leftover lights before starting new pattern.
digitalWrite(firstLEDPin, HIGH);
do
{
static byte pointer = 0; // we'll use this to point to the current sequence
static unsigned long lastChangeTime = 0; // for timing purposes
unsigned long currentTime = millis();
// we'll display all the leds then advance the pointer if it's time to
{
if (currentTime - lastChangeTime > duration)
{
// we have to remember this time
lastChangeTime = currentTime;
// basically all leds are off except one
for (byte index = 0; index < ledCount; index++)
{
if (index == sequence[pointer]) digitalWrite(ledPin[index], HIGH);
else digitalWrite(ledPin[index], LOW);
}
pointer = ++pointer % sequenceCount; // this will roll the pointer over to display the sequence again
poweredOn=true; //sets mode key for the powerbutton toggle 'if' statement
}
}
}
while (results.value=true);
break;
//--------------------------Button 2---------------------------------
case 16718055: //Button 2 pressed
senseFlash(); //Called senseFlash() Red LED flashes fast. Means IR signal was sensed
Serial.println("Button 3"); //displays "button 6" on serial monitor
poweredOn=false; //sets mode key for the powerbutton toggle 'if' statement
allOff();
break;
}
irrecv.resume(); // Receive the next value
}
}
//-----------------------------Called Functions ---------------------------------------
//--------------------------------void allOn()---------------------------------------
void allOn() // turn on all lights
{
unsigned char i;
for (i=4;i<=7;i++)
digitalWrite(i, HIGH);
}
//------------------------------ void allOff()---------------------------------------
void allOff() // turn off all lights
{
unsigned char i;
for (i=4;i<=7;i++)
digitalWrite(i, LOW);
}
//------------------------------------void senseFlash()-------------------------------------------------
void senseFlash() //This function is to acknowledge the fact that the IR reciever did recieve the signal. Red LED flashes fast.
//Uses delay() but only for a short time so no real hang up.
{
int flashSpeed = 50; //Red LED delay speed
digitalWrite(redPin, HIGH); // Red LED flashes fast. Means IR signal was sensed
delay (flashSpeed);
digitalWrite(redPin, LOW);
delay (flashSpeed);
digitalWrite(redPin, HIGH);
delay (flashSpeed);
digitalWrite(redPin, LOW);
delay(flashSpeed);
digitalWrite(redPin, HIGH);
delay (flashSpeed);
digitalWrite(redPin, LOW);
delay (flashSpeed);
digitalWrite(redPin, HIGH);
delay (flashSpeed);
digitalWrite(redPin, LOW);
delay (500);
}
//--------------------------------void idleFade()--------------------------------------
void idleFade() {
const int del = 10;
for (int p=0; p<=100; p++)
{
analogWrite(redPin, p);
delay(del);
}
for (int r=100; r>=0; r--)
{
analogWrite(redPin, r);
delay(del);
}
}