UniversalRemote_Header ARDUINO REMOTE CODE UniversalRemote_Header

 

#include <SPI.h> // For Radio Module - TMRh20/RF24, https://github.com/tmrh20/RF24/
#include <nRF24L01.h> // For Radio Module - TMRh20/RF24, https://github.com/tmrh20/RF24/
#include <RF24.h> // For Radio Module - TMRh20/RF24, https://github.com/tmrh20/RF24/
#include <Wire.h> // For Accelerometer Module
#include <LiquidCrystal.h> // For 1602 Display

// IO Varibles - Initialise Digitals
// Inputs
int BL1 = 2; // Pin Assignment for Button Left 1 Flush Momentary 2
int BL2 = 4; // Pin Assignment for Button Left 2 Raised Momentary 4
int BL3 = 6; // Pin Assignment for Button Left 3 Raised Momentary 6
int BL4 = 8; // Pin Assignment for Button Left 4 Flush Momentary 8
int BL5 = 10; // Pin Assignment for Button Left 5 Flush Momentary 10
int BL1Value = 0; // Stored Value for Button Left 1 Flush Momentary
int BL2Value = 0; // Stored Value for Button Left 2 Raised Momentary
int BL3Value = 0; // Stored Value for Button Left 3 Raised Momentary
int BL4Value = 0; // Stored Value for Button Left 4 Flush Momentary
int BL5Value = 0; // Stored Value for Button Left 5 Flush Momentary
int BL1ValuePrev = 0; // Previous Value for Button Left 1 Flush Momentary
int BL2ValuePrev = 0; // Previous Value for Button Left 2 Raised Momentary
int BL3ValuePrev = 0; // Previous Value for Button Left 3 Raised Momentary
int BL4ValuePrev = 0; // Previous Value for Button Left 4 Flush Momentary
int BL5ValuePrev = 0; // Previous Value for Button Left 5 Flush Momentary
int BR1 = 23; // Pin Assignment for Button Right 1 Flush Momentary 4
int BR2 = 27; // Pin Assignment for Button Right 2 Raised Momentary 3
int BR3 = 31; // Pin Assignment for Button Right 3 Raised Momentary 2
int BR4 = 22; // Pin Assignment for Button Right 4 Flush Momentary 35
int BR5 = 26; // Pin Assignment for Button Right 5 Flush Momentary 37
int BR1Value = 0; // Stored Value for Button Right 1 Flush Momentary
int BR2Value = 0; // Stored Value for Button Right 2 Raised Momentary
int BR3Value = 0; // Stored Value for Button Right 3 Raised Momentary
int BR4Value = 0; // Stored Value for Button Right 4 Flush Momentary
int BR5Value = 0; // Stored Value for Button Right 5 Flush Momentary
int BR1ValuePrev = 0; // Previous Value for Button Right 1 Flush Momentary
int BR2ValuePrev = 0; // Previous Value for Button Right 2 Raised Momentary
int BR3ValuePrev = 0; // Previous Value for Button Right 3 Raised Momentary
int BR4ValuePrev = 0; // Previous Value for Button Right 4 Flush Momentary
int BR5ValuePrev = 0; // Previous Value for Button Right 5 Flush Momentary
int BC1 = 30; // Pin Assignment for Button Center 2 Raised Latch 29
int BC2 = 32; // Pin Assignment for Button Center 3 Raised Latch 31
int BC3 = 34; // Pin Assignment for Button Center 4 Raised Latch 33
int BC1Value = 0; // Stored Value for Button Center 1 Raised Latch
int BC2Value = 0; // Stored Value for Button Center 2 Raised Latch
int BC3Value = 0; // Stored Value for Button Center 3 Raised Latch
int BC1ValuePrev = 0; // Previous Value for Button Center 1 Raised Latch
int BC2ValuePrev = 0; // Previous Value for Button Center 2 Raised Latch
int BC3ValuePrev = 0; // Previous Value for Button Center 3 Raised Latch

// Outputs
int BL1_LED = 3; // Pin Assignment for Button Left 1 LED 3
int BL2_LED = 5; // Pin Assignment for Button Left 2 LED 5
int BL3_LED = 7; // Pin Assignment for Button Left 3 LED 7
int BL4_LED = 9; // Pin Assignment for Button Left 4 LED 9
int BL5_LED = 11; // Pin Assignment for Button Left 5 LED 11
int BR1_LED = 25; // Pin Assignment for Button Right 1 LED 10
int BR2_LED = 29; // Pin Assignment for Button Right 2 LED 9
int BR3_LED = 33; // Pin Assignment for Button Right 3 LED 8
int BR4_LED = 24; // Pin Assignment for Button Right 4 LED 34
int BR5_LED = 28; // Pin Assignment for Button Right 5 LED 36


// IO Varibles - Initialise Analogues
int JLh = A2; // Pin Assignment for Joystick Left Horizontal 1
int JLv = A1; // Pin Assignment for Joystick Left Vertical 0
int JLhValue = 0; // Stored Value for Joystick Left Horizontal
int JLvValue = 0; // Stored Value for Joystick Left Vertical
int JLhValuePrev = 0; // Previous Value for Joystick Left Horizontal
int JLvValuePrev = 0; // Previous Value for Joystick Left Vertical
int JRh = A5; // Pin Assignment for Joystick Right Horizontal 13
int JRv = A6; // Pin Assignment for Joystick Right Vertical 12
int JRhValue = 0; // Stored Value for Joystick Right Horizontal
int JRvValue = 0; // Stored Value for Joystick Right Vertical
int JRhValuePrev = 0; // Previous Value for Joystick Right Horizontal
int JRvValuePrev = 0; // Previous Value for Joystick Right Vertical
int PL = A0; // Pin Assignment for Potentiometer Left 1 4
int PR = A7; // Pin Assignment for Potentiometer Right 1 11
int PLValue = 0; // Stored Value for Potentiometer Left 1
int PRValue = 0; // Stored Value for Potentiometer Right 1
int PLValuePrev = 0; // Previous Value for Potentiometer Left 2
int PRValuePrev = 0; // Previous Value for Potentiometer Right 2

// Accelerometer Varibles - MPU6050
const int MPU = 0x68; // MPU6050 I2C address
float AccX, AccY, AccZ; // Raw X, Y and Z Axis
float AngleX, AngleY; // X and Y Angles - Positive and Negative
float MemAngleX, MemAngleY; // X and Y Angles - Sored At SleepMode
int IdleTime = 2000; // Time (Cycles) that Accelerometer must see no movement before Sleep Mode is engaged
int IdleCnt = 0; // Number of Cycles counted while Accelerom,eter is Idle - Stationery
float AccTolerance = 3; // Number of Degrees that Accelerometer must register to create an event
int SleepMode = 0; // 0 = Controller is put to Sleep / 1 = Controller is put to awakened

// Rotary Encoders Varibles - Left - DF Robot - EC11
int ELa = 12; // A - Output for Left Rotary Encoder Switch
int ELb = 13; // B - Output for Left Rotary Encoder Switch
int ELCnt = 1;
int ELCntPrev = 1;
int ELaState;
int ELaStatePrev;
int ELMax = 10;

// Rotary Encoders Varibles - Right - DF Robot - EC11
int ERa = 42; // A - Output for Left Rotary Encoder Switch
int ERb = 44; // B - Output for Left Rotary Encoder Switch
int ERCnt = 1;
int ERCntPrev = 1;
int ERaState;
int ERaStatePrev;
int ERMax = 10;

// 1602 LCD Display. Parameters: (rs, enable, d4, d5, d6, d7)
LiquidCrystal lcd(39, 41, 43, 45, 47, 49);
String DevicePrefix = "";
String DeviceText = "";
String FunctionPrefix = "";
String FunctionText = "";
int BackLight = 37; // Pin Assignment for BackLight Output

// Radio Varibles
RF24 radio(46, 48); // Digital Input Pins Selected for Radio Board - CE, CSN
const byte address[6] = "00001"; // Radio Addresses - 5 Digits
String SendToRadio = ""; // Assembled String Sent to Radio For Transmission

// ................
int AnalogDamp1 = 5; // Damping Value to avoid false triggers from Analogue drifting - 1 Cycle
int AnalogDamp2 = 5; // Damping Value to avoid false triggers from Analogue drifting - 3 Cycles

void setup() { //.........................................................................................................................................................
Serial.begin(9600); // Begin Serial Monitor

// IO Config - Configuration of Digital Inputs
// Inputs
pinMode(BL1,INPUT); // Button BL1 is set to Input
pinMode(BL2,INPUT); // Button BL2 is set to Input
pinMode(BL3,INPUT); // Button BL3 is set to Input
pinMode(BL4,INPUT); // Button BL4 is set to Input
pinMode(BL5,INPUT); // Button BL5 is set to Input
pinMode(BR1,INPUT); // Button BR1 is set to Input
pinMode(BR2,INPUT); // Button BR2 is set to Input
pinMode(BR3,INPUT); // Button BR3 is set to Input
pinMode(BR4,INPUT); // Button BR4 is set to Input
pinMode(BR5,INPUT); // Button BR5 is set to Input
pinMode(BC1,INPUT); // Button BC1 is set to Input
pinMode(BC2,INPUT); // Button BC2 is set to Input
pinMode(BC3,INPUT); // Button BC3 is set to Input
pinMode(ELa,INPUT); // Rotary Encoder ReLa is set to Input
pinMode(ELb,INPUT); // Rotary Encoder ReLb is set to Input
pinMode(ERa,INPUT); // Rotary Encoder ReRa is set to Input
pinMode(ERb,INPUT); // Rotary Encoder ReRb is set to Input
// Outputs
pinMode(BL1_LED,OUTPUT); // BL1_LED is Set to Output
pinMode(BL2_LED,OUTPUT); // BL2_LED is Set to Output
pinMode(BL3_LED,OUTPUT); // BL3_LED is Set to Output
pinMode(BL4_LED,OUTPUT); // BL4_LED is Set to Output
pinMode(BL5_LED,OUTPUT); // BL5_LED is Set to Output
pinMode(BR1_LED,OUTPUT); // BR1_LED is Set to Output
pinMode(BR2_LED,OUTPUT); // BR2_LED is Set to Output
pinMode(BR3_LED,OUTPUT); // BR3_LED is Set to Output
pinMode(BR4_LED,OUTPUT); // BR4_LED is Set to Output
pinMode(BR5_LED,OUTPUT); // BR5_LED is Set to Output
pinMode(BackLight,OUTPUT); // Pin Assignment for BackLight Output

// Pull Up Digital Inputs
digitalWrite(BL1,HIGH); // Button BL1 is Set to Pullup
digitalWrite(BL2,HIGH); // Button BL2 is Set to Pullup
digitalWrite(BL3,HIGH); // Button BL3 is Set to Pullup
digitalWrite(BL4,HIGH); // Button BL4 is Set to Pullup
digitalWrite(BL5,HIGH); // Button BL5 is Set to Pullup
digitalWrite(BR1,HIGH); // Button BR1 is Set to Pullup
digitalWrite(BR2,HIGH); // Button BR2 is Set to Pullup
digitalWrite(BR3,HIGH); // Button BR3 is Set to Pullup
digitalWrite(BR4,HIGH); // Button BR4 is Set to Pullup
digitalWrite(BR5,HIGH); // Button BR5 is Set to Pullup
digitalWrite(BC1,HIGH); // Button BC1 is Set to Pullup
digitalWrite(BC2,HIGH); // Button BC2 is Set to Pullup
digitalWrite(BC3,HIGH); // Button BC3 is Set to Pullup

// IO Config - Configuration of Analog Inputs
pinMode(JLh,INPUT); // Joystick JLh is Set to Input
pinMode(JLv,INPUT); // Joystick JLv is Set to Input
pinMode(JRh,INPUT); // Joystick JRh is Set to Input
pinMode(JRv,INPUT); // Joystick JRv is Set to Input
pinMode(PL,INPUT); // Potentiometer PTL1 is Set to Input
pinMode(PR,INPUT); // Potentiometer PTR1 is Set to Input

// Rotary Encoders - Left - Configuration of Inputs
pinMode(ELa,INPUT); // Rotary Encoder Left Output A is Set to Input
pinMode(ELb,INPUT); // Rotary Encoder Left Output B is Set to Input
ELaStatePrev = digitalRead(ELa);
// Rotary Encoders - Right - Configuration of Inputs
pinMode(ERa,INPUT); // Rotary Encoder Right Output A is Set to Input
pinMode(ERb,INPUT); // Rotary Encoder Right Output B is Set to Input
ERaStatePrev = digitalRead(ERa);

// LCD 1602 - Number of Columns and Rows
lcd.begin(16, 2);
lcd.clear();
DevicePrefix = "Dev01#";
DeviceText = "Device - 01";
lcd.setCursor(0, 0);
lcd.print(DeviceText);
FunctionPrefix = "01";
FunctionText = "Function - 01";
lcd.setCursor(0, 1);
lcd.print(FunctionText);

// Initialize interface to the Acceleromter - MPU6050
Wire.begin(); // Initialize communication
Wire.beginTransmission(MPU); // Start communication with MPU6050 - MPU=0x68
Wire.write(0x6B); // Talk to the register 6B
Wire.write(0x00); // Make reset - place a 0 into the 6B register
Wire.endTransmission(true); //end the transmission
// Configure Accelerometer
Wire.beginTransmission(MPU); // Start communication with MPU6050 - MPU=0x1C
Wire.write(0x1C); //Talk to the ACCEL_CONFIG register
Wire.write(0x10); //Set the register bits as 00010000
Wire.endTransmission(true); //end the transmission

// Configure the RF24 radio Module
radio.begin(); // Activates the radio
radio.openWritingPipe(address); // 00001 - Assigns Channel to the Tx Pipe
radio.setAutoAck(false);
radio.setDataRate(RF24_1MBPS); // Set Data Rate
radio.setPALevel(RF24_PA_MIN); // Set Radio Power Level
radio.stopListening(); // Put Radio in Tx Mode

}

void loop() { //.........................................................................................................................................................

// Read All IO - Digital
BL1Value = digitalRead(BL1); // Read Value of Input Button BL1 and Store in BL1Value
BL2Value = digitalRead(BL2); // Read Value of Input Button BL2 and Store in BL2Value
BL3Value = digitalRead(BL3); // Read Value of Input Button BL3 and Store in BL3Value
BL4Value = digitalRead(BL4); // Read Value of Input Button BL4 and Store in BL4Value
BL5Value = digitalRead(BL5); // Read Value of Input Button BL5 and Store in BL5Value
if (BL1Value == 0){BL1Value = 1;} else{BL1Value = 0;} // Invert BL1 Value
if (BL2Value == 0){BL2Value = 1;} else{BL2Value = 0;} // Invert BL2 Value
if (BL3Value == 0){BL3Value = 1;} else{BL3Value = 0;} // Invert BL3 Value
if (BL4Value == 0){BL4Value = 1;} else{BL4Value = 0;} // Invert BL4 Value
if (BL5Value == 0){BL5Value = 1;} else{BL5Value = 0;} // Invert BL5 Value
if (BL1Value == 0 && SleepMode == 0){digitalWrite(BL1_LED,HIGH);} else{digitalWrite(BL1_LED,LOW);} // LED Follows Switch
if (BL2Value == 0 && SleepMode == 0){digitalWrite(BL2_LED,HIGH);} else{digitalWrite(BL2_LED,LOW);} // LED Follows Switch
if (BL3Value == 0 && SleepMode == 0){digitalWrite(BL3_LED,HIGH);} else{digitalWrite(BL3_LED,LOW);} // LED Follows Switch
if (BL4Value == 0 && SleepMode == 0){digitalWrite(BL4_LED,HIGH);} else{digitalWrite(BL4_LED,LOW);} // LED Follows Switch
if (BL5Value == 0 ){digitalWrite(BL5_LED,HIGH);} else{digitalWrite(BL5_LED,LOW);} // LED Follows Switch
if (BL1Value != BL1ValuePrev && SendToRadio == ""){SendToRadio = "BL1!" + String(BL1Value); BL1ValuePrev = BL1Value;} // Send BL1 Value to Radio on Change
if (BL2Value != BL2ValuePrev && SendToRadio == ""){SendToRadio = "BL2!" + String(BL2Value); BL2ValuePrev = BL2Value;} // Send BL2 Value to Radio on Change
if (BL3Value != BL3ValuePrev && SendToRadio == ""){SendToRadio = "BL3!" + String(BL3Value); BL3ValuePrev = BL3Value;} // Send BL3 Value to Radio on Change
if (BL4Value != BL4ValuePrev && SendToRadio == ""){SendToRadio = "BL4!" + String(BL4Value); BL4ValuePrev =BL4Value;} // Send BL4 Value to Radio on Change
if (BL5Value != BL5ValuePrev && SendToRadio == ""){SendToRadio = "BL5!" + String(BL5Value); BL5ValuePrev = BL5Value;} // Send BL5 Value to Radio on Change
BR1Value = digitalRead(BR1); // Read Value of Input Button BR1 and Store in BR1Value
BR2Value = digitalRead(BR2); // Read Value of Input Button BR2 and Store in BR2Value
BR3Value = digitalRead(BR3); // Read Value of Input Button BR3 and Store in BR3Value
BR4Value = digitalRead(BR4); // Read Value of Input Button BR4 and Store in BR4Value
BR5Value = digitalRead(BR5); // Read Value of Input Button BR5 and Store in BR5Value
if (BR1Value == 0){BR1Value = 1;} else{BR1Value = 0;} // Invert BR1 Value
if (BR2Value == 0){BR2Value = 1;} else{BR2Value = 0;} // Invert BR2 Value
if (BR3Value == 0){BR3Value = 1;} else{BR3Value = 0;} // Invert BR3 Value
if (BR4Value == 0){BR4Value = 1;} else{BR4Value = 0;} // Invert BR4 Value
if (BR5Value == 0){BR5Value = 1;} else{BR5Value = 0;} // Invert BR5 Value
if (BR1Value == 0 && SleepMode == 0){digitalWrite(BR1_LED,HIGH);} else{digitalWrite(BR1_LED,LOW);} // LED Follows Switch
if (BR2Value == 0 && SleepMode == 0){digitalWrite(BR2_LED,HIGH);} else{digitalWrite(BR2_LED,LOW);} // LED Follows Switch
if (BR3Value == 0 && SleepMode == 0){digitalWrite(BR3_LED,HIGH);} else{digitalWrite(BR3_LED,LOW);} // LED Follows Switch
if (BR4Value == 0 && SleepMode == 0){digitalWrite(BR4_LED,HIGH);} else{digitalWrite(BR4_LED,LOW);} // LED Follows Switch
if (BR5Value == 0 && SleepMode == 0){digitalWrite(BR5_LED,HIGH);} else{digitalWrite(BR5_LED,LOW);} // LED Follows Switch
if (BR1Value != BR1ValuePrev && SendToRadio == ""){SendToRadio = "BR1!" + String(BR1Value); BR1ValuePrev = BR1Value;} // Send BR1 Value to Radio on Change
if (BR2Value != BR2ValuePrev && SendToRadio == ""){SendToRadio = "BR2!" + String(BR2Value); BR2ValuePrev = BR2Value;} // Send BR2 Value to Radio on Change
if (BR3Value != BR3ValuePrev && SendToRadio == ""){SendToRadio = "BR3!" + String(BR3Value); BR3ValuePrev = BR3Value;} // Send BR3 Value to Radio on Change
if (BR4Value != BR4ValuePrev && SendToRadio == ""){SendToRadio = "BR4!" + String(BR4Value); BR4ValuePrev = BR4Value;} // Send BR4 Value to Radio on Change
if (BR5Value != BR5ValuePrev && SendToRadio == ""){SendToRadio = "BR5!" + String(BR5Value); BR5ValuePrev = BR5Value;} // Send BR5 Value to Radio on Change
BC1Value = digitalRead(BC1); // Read Value of Input Button BC1 and Store in BC2Value
BC2Value = digitalRead(BC2); // Read Value of Input Button BC2 and Store in BC3Value
BC3Value = digitalRead(BC3); // Read Value of Input Button BC3 and Store in BC4Value
if (BC1Value == 0){BC1Value = 1;} else{BC1Value = 0;} // Invert BC1 Value
if (BC2Value == 0){BC2Value = 1;} else{BC2Value = 0;} // Invert BC2 Value
if (BC3Value == 0){BC3Value = 1;} else{BC3Value = 0;} // Invert BC3 Value
if (BC1Value != BC1ValuePrev && SendToRadio == ""){SendToRadio = "BC1!" + String(BC1Value); BC1ValuePrev = BC1Value;} // Send BC1 Value to Radio on Change
if (BC2Value != BC2ValuePrev && SendToRadio == ""){SendToRadio = "BC2!" + String(BC2Value); BC2ValuePrev = BC2Value;} // Send BC2 Value to Radio on Change
if (BC3Value != BC3ValuePrev && SendToRadio == ""){SendToRadio = "BC3!" + String(BC3Value); BC3ValuePrev = BC3Value;} // Send BC3 Value to Radio on Change

// Read All IO - Analog
JLhValue=analogRead(JLh); // Read Value of Joystick JLh and Store in JLhValue
JLvValue=analogRead(JLv); // Read Value of Joystick JLv and Store in JLvValue
JLhValue = map(JLhValue, 0, 1023, 100, 0); // Map Analogue to 1-100
JLvValue = map(JLvValue, 0, 1023, 0, 100); // Map Analogue to 1-100
if ((abs(JLhValue - JLhValuePrev) >= AnalogDamp2) && SendToRadio == ""){SendToRadio = "JLh!" + String(JLhValue); JLhValuePrev = JLhValue;} // Send JLh Value to Radio on Change
if ((abs(JLvValue - JLvValuePrev) >= AnalogDamp2) && SendToRadio == ""){SendToRadio = "JLv!" + String(JLvValue); JLvValuePrev = JLvValue;} // Send JLv Value to Radio on Change
JRhValue=analogRead(JRh); // Read Value of Joystick JRh and Store in JR1hValue
JRvValue=analogRead(JRv); // Read Value of Joystick JRv and Store in JR1vValue
JRhValue = map(JRhValue, 0, 1023, 0, 100); // Map Analogue to 1-100
JRvValue = map(JRvValue, 0, 1023, 100, 0); // Map Analogue to 1-100
if ((abs(JRhValue - JRhValuePrev) >= AnalogDamp2) && SendToRadio == ""){SendToRadio = "JRh!" + String(JRhValue); JRhValuePrev = JRhValue;} // Send JRh Value to Radio on Change
if ((abs(JRvValue - JRvValuePrev) >= AnalogDamp2) && SendToRadio == ""){SendToRadio = "JRv!" + String(JRvValue); JRvValuePrev = JRvValue;} // Send JRv Value to Radio on Change
PLValue=analogRead(PL); // Read Value of Potentiometer PTL1 and Store in PTL1Value
PRValue=analogRead(PR); // Read Value of Potentiometer PTR1 and Store in PTR1Value
PLValue = map(PLValue, 0, 1023, 0, 100); // Map Analogue to 1-100
PRValue = map(PRValue, 0, 1023, 100, 0); // Map Analogue to 1-100
PRValue = (100 - PRValue); // Invert Pot Value because of incorrect wiring
if ((abs(PLValue - PLValuePrev) >= AnalogDamp1) && SendToRadio == ""){SendToRadio = "PL!" + String(PLValue); PLValuePrev = PLValue;} // Send PL Value to Radio on Change
if ((abs(PRValue - PRValuePrev) >= AnalogDamp1) && SendToRadio == ""){SendToRadio = "PR!" + String(PRValue); PRValuePrev = PRValue;} // Send PR Value to Radio on Change

// Rotary Encoder - Left
ELaState = digitalRead(ELa);
if (ELaState != ELaStatePrev && ELaState == 1){
if (digitalRead(ELb) != ELaState) {ELCnt = ELCnt + 1;} else {ELCnt = ELCnt - 1;}
if (ELCnt > ELMax) {ELCnt = 1;}
if (ELCnt < 1) {ELCnt = ELMax;}
Serial.println(ELCnt);}
ELaStatePrev = ELaState;
if (ELCntPrev != ELCnt) {
if (ELCnt == 1) {DevicePrefix = "Dev01#"; DeviceText = "Device - 01";}
if (ELCnt == 2) {DevicePrefix = "Dev02#"; DeviceText = "Device - 02";}
if (ELCnt == 3) {DevicePrefix = "Dev03#"; DeviceText = "Device - 03";}
if (ELCnt == 4) {DevicePrefix = "Dev04#"; DeviceText = "Device - 04";}
if (ELCnt == 5) {DevicePrefix = "Dev05#"; DeviceText = "Device - 05";}
if (ELCnt == 6) {DevicePrefix = "Dev06#"; DeviceText = "Device - 06";}
if (ELCnt == 7) {DevicePrefix = "Dev07#"; DeviceText = "Device - 07";}
if (ELCnt == 8) {DevicePrefix = "Dev08#"; DeviceText = "Device - 08";}
if (ELCnt == 9) {DevicePrefix = "Dev09#"; DeviceText = "Device - 09";}
if (ELCnt == 10) {DevicePrefix = "Dev10#"; DeviceText = "Device - 10";}
ELCntPrev = ELCnt;
lcd.setCursor(0, 0);
lcd.print(DeviceText);}

// Rotary Encoder - Right
ERaState = digitalRead(ERa);
if (ERaState != ERaStatePrev && ERaState == 1){
if (digitalRead(ERb) != ERaState) {ERCnt = ERCnt + 1;} else {ERCnt = ERCnt - 1;}
if (ERCnt > ERMax) {ERCnt = 1;}
if (ERCnt < 1) {ERCnt = ERMax; }
Serial.println(ERCnt);}
ERaStatePrev = ERaState;
if (ERCntPrev != ERCnt) {
if (ERCnt == 1) {FunctionPrefix = "01"; FunctionText = "Function - 01";}
if (ERCnt == 2) {FunctionPrefix = "02"; FunctionText = "Function - 02";}
if (ERCnt == 3) {FunctionPrefix = "03"; FunctionText = "Function - 03";}
if (ERCnt == 4) {FunctionPrefix = "04"; FunctionText = "Function - 04";}
if (ERCnt == 5) {FunctionPrefix = "05"; FunctionText = "Function - 05";}
if (ERCnt == 6) {FunctionPrefix = "06"; FunctionText = "Function - 06";}
if (ERCnt == 7) {FunctionPrefix = "07"; FunctionText = "Function - 07";}
if (ERCnt == 8) {FunctionPrefix = "08"; FunctionText = "Function - 08";}
if (ERCnt == 9) {FunctionPrefix = "09"; FunctionText = "Function - 09";}
if (ERCnt == 10) {FunctionPrefix = "10"; FunctionText = "Function - 10";}
ERCntPrev = ERCnt;
lcd.setCursor(0, 1);
lcd.print(FunctionText);}

// Read accelerometer data
Wire.beginTransmission(MPU);
Wire.write(0x3B); // Start with register 0x3B (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(MPU, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
AccX = (Wire.read() << 8 | Wire.read()) / 4096.0; // X-axis value
AccY = (Wire.read() << 8 | Wire.read()) / 4096.0; // Y-axis value
AccZ = (Wire.read() << 8 | Wire.read()) / 4096.0; // Z-axis value
AngleX = (atan(AccY / sqrt(pow(AccX, 2) + pow(AccZ, 2))) * 180 / PI) + 1.15; // AccErrorX ~(-1.15)
AngleY = (atan(-1 * AccX / sqrt(pow(AccY, 2) + pow(AccZ, 2))) * 180 / PI) - 0.52; // AccErrorX ~(0.5)
// Accelerometer Sleep Mode
if (IdleCnt <= IdleTime) {IdleCnt = IdleCnt + 1;} // Increment the Idle Time Counter
if (SleepMode == 0 && IdleCnt > IdleTime) {SleepMode = 1; } // If Idle Time is reached, Place in Sleep Mode
if ((abs(MemAngleX - AngleX) > AccTolerance) || (abs(MemAngleY - AngleY) > AccTolerance)) { // If movement is detected, Wake Controller
IdleCnt = 0; SleepMode = 0; MemAngleX = AngleX; MemAngleY = AngleY;}
if (SleepMode == 0){digitalWrite(BackLight,HIGH);} else{digitalWrite(BackLight,LOW);} // Disable BackLight in SleepMode

// Transmit Data Via Radio
if (SendToRadio != ""){SendToRadio = DevicePrefix + SendToRadio;}
if (SendToRadio != ""){Serial.println(SendToRadio);Serial.println();}
if (SendToRadio != ""){radio.write( SendToRadio.c_str(), SendToRadio.length()); SendToRadio = "";}

// Loop Delay
if (SleepMode == 1) {delay(500);}
if (BC3Value == 0) {delay(40);}
delay(2);
}

 

 

 

 

HomeButton