Im thinking that there is a better way to write this code and do the same thing
const int LTS = 2;
const int HIB = 3;
const int CEL = 4;
const int RTS = 5;
int LTSState = 0;
int HIBState = 0;
int CELState = 0;
int RTSState = 0;
void setup() {
pinMode(LTS, INPUT_PULLUP);
pinMode(HIB, INPUT_PULLUP);
pinMode(CEL, INPUT_PULLUP);
pinMode(RTS, INPUT_PULLUP);
Serial.begin(115200);
}
void loop() {
LTSState = digitalRead(LTS);
if (LTS == HIGH) {
Serial.print(F("bt1.val="));
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
} else {
Serial.print(F("bt1.val="));
Serial.print(0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
HIBState = digitalRead(HIB);
if (HIB == HIGH) {
Serial.print(F("bt3.val="));
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
} else {
Serial.print(F("bt3.val="));
Serial.print(0);
Serial.write(0xaaff);
Serial.write(0xff);
Serial.write(0xff);
}
CELState = digitalRead(CEL);
if (CEL == HIGH) {
Serial.print(F("bt2.val="));
Serial.print(0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
} else {
Serial.print(F("bt2.val="));
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
RTSState = digitalRead(RTS);
if (RTS == HIGH) {
Serial.print(F("bt4.val="));
Serial.print(0);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
} else {
Serial.print(F("bt4.val="));
Serial.print(1);
Serial.write(0xff);
Serial.write(0xff);
Serial.write(0xff);
}
}
Ive seen some different codes that use an array, but I am not advanced enough to understand how it work.
The goal I want to accomplish is make a code for this that basically changes the variable and its value whenever the state changes as I believe this code will keep sending every loop cycle and I dont need that. I could millis the code to slow down the updates, but Im unsure of how to update quickly, but only when a state changed and do it with less line of code (if possible).
Simply put, I want to take the multiple if else functions and put place holders in the Serial.print areas so I can have one "template" of code for this and just fill out the variable and its value
Any ideas?