/*
LAB08 PT3 Remote Controlled Light Show
NBlizard - 10/21/22
wired
Use the RGB LED and the IR Remote and Receiver.
Write a program that can display 10 different colors.
With each press of the forward or back button
the RGB led will display the next or previous color respectively.
Pressing any of the 0-9 numbered buttons will jump to that
specific color in the list.
Use the power button to turn on and off the led
(turning on will resume the previously displayed color).
When you first turn on the LED with the power button,
use color index 0. While the power is off,
pressing other buttons should not do anything.
Uses IRremote V3.6.1 by shirriff, z3t0, ArminJo
NOW 3.7.1 DOWNLOADED FROM GIT
HAD TO REDEFINE THE REMOTE KEY HEX VALUES FOR THE IR REMOTE I HAD FROM
WOKWI AND KITS
*/
#define DARK 10 // mode for setting LEDs dark
#define RED 0 // mode for setting LEDs RED
#define FORWARD true // direction for FForw or FRev
#define PRINTFLAG false // serial print variables
#define TESTFLAG false // diagnostic calls
#include <IRremote.h>
const byte IR_RECEIVE_PIN = 13; // Signal Pin of IR receiver
const byte LEDPinRed = 3;
const byte LEDPinGreen = 5;
const byte LEDPinBlue = 6;
// global
byte index = 0; // initialize as zero
byte lastIndex = 0; // saves the last index (color called)
bool POWER_flag = false;
bool firstPowerOn = true; // this flag calls for RED to be displayed at first poweron
void setup() {
Serial.begin(9600);
Serial.println("IR Receiver Button Decode");
IrReceiver.begin(IR_RECEIVE_PIN, DISABLE_LED_FEEDBACK);
pinMode(LEDPinRed, OUTPUT);
pinMode(LEDPinGreen, OUTPUT);
pinMode(LEDPinBlue, OUTPUT);
DarkLED();
}
void loop() {
if (TESTFLAG) {
delay(5000); // just to see what setup did
LightLED (3);
delay(5000);
LightLED(10);
delay(5000);
}
//z FIXME IMMEDIATELY GOES TO RED (0), WHY?????????????????????????????
if (IrReceiver.decode()) { // Did we receive an IR signal?
translateIR();
IrReceiver.resume(); // receive the next value
}
if (TESTFLAG) {
Serial.print(" index = ");
Serial.println(index);
//test code for rgb led calls
for (byte j = 0; j < 10; j++) {
LightLED ( j );
delay (500);
}
}
if ((POWER_flag) && (index >= 0 && index <= 10)) { // must have power on to light leds
LightLED (index); // lights the LED selected by index
// } else if (index == 10) {
// POWER_ONOFF ();
}
}
void translateIR() { // Prints the corresponding button name for the code received.
//byte index;
switch (IrReceiver.decodedIRData.decodedRawData) {
case 0x5DA2FF00: PowerFunc(); break; // WAS ?
case 0x1DE2FF00: MenuFunc(); break; // WAS B847FF00
case 0xDD22FF00: TestFunc(); break; // WAS BF40FF00
case 0xFD02FF00: PlusFunc(); break; // NEW DEFINITION
case 0x3DC2FF00: ReturnFunc(); break; // NEW DEFINITION
case 0x1FE0FF00: FastReverseFunc(); break; // WAS BB44FF00
case 0x57A8FF00: PlayFunc(); break; // NEW DEFINITION
case 0x6F90FF00: FastForwFunc(); break; // WAS BC43FF00
case 0x9768FF00: ZeroFunc(); break; // WAS E916FF00
case 0x6798FF00: MinusFunc(); break; // NEW DEFINITION
case 0x4FB0FF00: ClearFunc(); break; // NEW DEFINITION
case 0xCF30FF00: OneFunc(); break; // WAS F30CFF00
case 0xE718FF00: TwoFunc(); break; // OK
case 0x857AFF00: ThreeFunc(); break; // WAS A15EFF00
case 0xEF10FF00: FourFunc(); break; // WAS F708FF00
case 0xC738FF00: FiveFunc(); break; // WAS E31CFF00
case 0xA55AFF00: SixFunc(); break; // OK
case 0xBD42FF00: SevenFunc() ; break; // OK
case 0xAD52FF00: NineFunc(); break; // WAS 8
case 0xB54AFF00: EightFunc(); break; // WAS 9
default: DefaultFunct();
}
}
// BUTTON FUNCTION DEFINITIONS **********************
void PowerFunc() {
if (PRINTFLAG) {Serial.println("POWER"); }
index = 11; // assign 11 >>> FIXME CALL POWER_ONOFF FUNCTION
POWER_ONOFF (); // this toggles powerflag and turns leds off or on to last index
}
void MenuFunc() {
if (PRINTFLAG) { Serial.println("MENU"); }
index = 12; // assign 12
}
void TestFunc() {
if (PRINTFLAG) { Serial.println("TEST");}
index = 13; // assign 13
}
void PlusFunc() {
if (PRINTFLAG) { Serial.println("+"); }
index = 14; // assign 14
}
void ReturnFunc() {
if (PRINTFLAG) { Serial.println("RETURN"); }
index = 15; // assign 15
}
void FastReverseFunc() {
if (PRINTFLAG) { Serial.println("FRev"); }
index = 16; // assign 16
FastForwardorReverse(!FORWARD);
}
void PlayFunc() {
if (PRINTFLAG) { Serial.println("PLAY"); }
index = 17; // assign 17
}
void FastForwFunc() {
if (PRINTFLAG) { Serial.println("FForw"); }
index = 18; // assign 18
FastForwardorReverse(FORWARD);
}
void ZeroFunc() {
if (PRINTFLAG) { Serial.println("0"); }
index = 0; // assign 0
}
void MinusFunc() {
if (PRINTFLAG) { Serial.println("-"); }
index = 19; // assign 19
}
void ClearFunc() {
if (PRINTFLAG) { Serial.println("C"); }
index = 20; // assign 20
}
void OneFunc() {
if (PRINTFLAG) { Serial.println("1"); }
index = 1; // assign 1
}
void TwoFunc() {
if (PRINTFLAG) { Serial.println("2"); }
index = 2; // assign 2
}
void ThreeFunc() {
if (PRINTFLAG) { Serial.println("3"); }
index = 3; // assign 3
}
void FourFunc() {
if (PRINTFLAG) { Serial.println("4"); }
index = 4; // assign 4
}
void FiveFunc() {
if (PRINTFLAG) { Serial.println("5"); }
index = 5; // assign 5
}
void SixFunc() {
if (PRINTFLAG) { Serial.println("6"); }
index = 6; // assign 6
}
void SevenFunc() {
if (PRINTFLAG) { Serial.println("7"); }
index = 7; // assign 7
}
void EightFunc() {
if (PRINTFLAG) { Serial.println("8"); }
index = 8; // assign 8
}
void NineFunc() {
if (PRINTFLAG) { Serial.println("9"); }
index = 9; // assign 9
}
void DefaultFunct() {
if (PRINTFLAG) { Serial.print("Undefined button code: ");}
if (PRINTFLAG) { Serial.println(IrReceiver.decodedIRData.decodedRawData, HEX);}
index = 99; // assign 99
}
void LightLED (byte indexLED) {
// R, G, B
lastIndex = index; // save the index if power onoff or FForw or FRev called
int LEDArray[11][3] = {
{255, 0, 0}, // Red (index 0)
{ 0, 255, 0}, // Green
{ 0, 0, 255}, // Blue
{ 0, 255, 255}, // Cyan
{255, 0, 255}, // Magenta
{255, 255, 0}, // Yellow
{127, 0, 255}, // Purple
{ 0, 255, 128}, // Other Green
{ 0, 128, 255}, // Other Blue
{255, 255, 255}, // White (index 9)
{ 0, 0, 0} // DARK (index 10)
};
digitalWrite(LEDPinRed, LEDArray[indexLED][0]);
digitalWrite(LEDPinGreen, LEDArray[indexLED][1]);
digitalWrite(LEDPinBlue, LEDArray[indexLED][2]);
}
/*
Use the power button to turn on and off the led
(turning on will resume the previously displayed color).
When you first turn on the LED with the power button,
use color index 0. While the power is off,
pressing other buttons should not do anything.
*/ //FIXME++++++++++++++++++++++++++++++++++++++++++++++++
void POWER_ONOFF () {
POWER_flag = !(POWER_flag); // toggle power flag
if (PRINTFLAG) {
Serial.print("POWER_flag = ");
Serial.print(POWER_flag) ;
Serial.print(", last index = ");
Serial.print(lastIndex);
Serial.print(". index = ");
Serial.println(index);
}
//note if first power on, call index 0 (RED)
if (POWER_flag) {
// turn on leds at lastIndex called
if (firstPowerOn) {
firstPowerOn = !firstPowerOn; // toggle once only and set lastIndex = RED
lastIndex = RED;
index = RED; // special case for FF
}
LightLED (lastIndex);
//index = lastIndex;
} else { // if powering off, turn off LEDS and ready to turn on RED
// turn off leds
DarkLED();
index = RED;
lastIndex = RED;
//lastIndex = DARK;
}
}
// routine to initialize or call all LEDs dark
void DarkLED() {
LightLED ( DARK );
}
/*
With each press of the forward or back button
the RGB led will display the next or previous color respectively.
*/
void FastForwardorReverse(bool directionFF) {
if (POWER_flag) {
if (directionFF) { // fast forward
byte indexFF = lastIndex + 1;
if (indexFF > 9) {
indexFF = 0; // go back to lowest index
}
LightLED( indexFF);
lastIndex = indexFF;
if (PRINTFLAG) { Serial.print("FF -- "); }
if (PRINTFLAG) { Serial.println(lastIndex); }
} else { // fast reverse
byte indexFR = lastIndex - 1; // CAREFUL, -1 byte rolls over to 255!!!
if ((indexFR < 0)||(indexFR == 255)) {
indexFR = 9; // go back to lowest index
}
LightLED( indexFR);
lastIndex = indexFR;
if (PRINTFLAG) { Serial.print("FR -- "); }
if (PRINTFLAG) { Serial.println(lastIndex); }
}
} else {
if (PRINTFLAG) { Serial.println("FForw or Frev pressed but Power Off"); }
}
}
/* DATASHEET FROM LECTURE
IR Transmitter:
Encoding Scheme: NEC, upd6122 (Why is this important?) There are many encoding schemes out there. When you go looking for a library, you need to know the encoding scheme of your remote control!)
Infrared wavelength: 940nm
IR carrier frequency: 38KHz
Remote control distance: more than 8 meters
IR Receiver:
2.1V - 5.5V
B.P.F Center Frequency: 38KHz (well matched to transmitter)
Peak Wavelength: 940nm (well matched to transmitter)
Look at “Relative Transmission Distance” graph
*/