Hello Community.
Im in the process of learning to program and im wondering what best practise i should use.
I have this code. it will in a future when im done check value of water and report if its bad.
#include "clickButton/clickButton.h"
const int pinRelay_RedLed = D0; // Relay and RedLed is on same, if relay turns on the water is bad.
const int pinResetButton = D1; //the pin where we connect the reset button
const int pinOsmosis_Stage2 = D2; //the pin where we connect the Osmosis EC meter stage 2 cable
const int pinOsmosis_Stage3 = D3; //the pin where we connect the Osmosis EC meter stage 3 cable
const int pinGreenLed = D6; //the pin where we connect the Green led
const int pinYellowLed = D7; //the pin where we connect the Yellow led
//------------------------------------------------------------------------
//---- Here i declare the state check for the inputs and also activate the pin --------------------
ClickButton stateResetButton(pinResetButton, HIGH, true)
ClickButton stateOsmosis_Stage2(pinOsmosis_Stage2, HIGH, true);
ClickButton stateOsmosis_Stage3(pinOsmosis_Stage3, HIGH, true);
//-----------------------------------------------------------------------
//---- Herer i declare the function i will use ---------------------------
void TurnOnYellowLed();
void TurnOnRelay_RedLed();
void PressResetButton();
//-----------------------------------------------------------------------
void setup() {
//---- We declare respective digital point as outputs and inputs here ----
pinMode(pinRelay_RedLed, OUTPUT); // Its an output since it need to send a voltage to Relay and Red Light if water is bad
pinMode(pinGreenLed, OUTPUT); // Its an output since it need to send a voltage Green led
pinMode(pinYellowLed, OUTPUT); // Its an output since it need to send a voltage to Yellow led
//----------------------------------------------------------------------
//---- We declare the state of each variable when the units gets powered on ----
digitalWrite(pinGreenLed, HIGH); // The GreenLed should always be on if water is good, if water is going bad This one will be LOW
//----------------------------------------------------------------------
//---- For troubleshooting ---------------------------------------------
Serial.begin(9600); // Open a serial port
//----------------------------------------------------------------------
//---- Here is some criterias for the inputs ----
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
stateResetButton.debounceTime = 20; // Debounce timer in ms
stateResetButton.multiclickTime = 250; // Time limit for multi clicks
stateResetButton.longClickTime = 1000; // time until "held-down clicks" register
stateOsmosis_Stage2.debounceTime = 20; // Debounce timer in ms
stateOsmosis_Stage2.multiclickTime = 250; // Time limit for multi clicks
stateOsmosis_Stage2.longClickTime = 5000; // time until "held-down clicks" register
stateOsmosis_Stage3.debounceTime = 20; // Debounce timer in ms
stateOsmosis_Stage3.multiclickTime = 250; // Time limit for multi clicks
stateOsmosis_Stage3.longClickTime = 5000; // time until "held-down clicks" register
//----------------------------------------------------------------------
}
void loop() {
//---- Here we update the status of all Input pin to see if they are 1 or 2,3,-1,-2,-3-----
stateResetButton.Update();
stateOsmosis_Stage2.Update();
stateOsmosis_Stage3.Update();
//----------------------------------------------------
//---- Here we tell the controller what to do if the status of the inputs meet certain criterias ----
switch(stateResetButton.clicks != 0){ // Checks all possible values and as long its not "0" call function
case 0:
// do nothing for 0
break;
//case x:
// do whatever for x
// break;
default: // all non explicitly dealt cases, The function should be called whatever value
PressResetButton();
break;
}
if(stateOsmosis_Stage3.clicks == -1) TurnOnRelay_RedLed(); // If HIGH for 60 seconds call function, -1 is waiting for a long press (60 sec)
if (stateOsmosis_Stage3 == LOW){
if(stateOsmosis_Stage2.clicks == -1) TurnOnYellowLed(); // If HIGH for 60 seconds call function, -1 is waiting for a long press (60 sec)
}
//-----------------------------------------------------
}
//-----------------------------------------------------------------------------
//---- Here we write down all functions thats gets called under the loop() ----
void TurnOnYellowLed(){ // This function is called when EC meters stage 2 is HIGH
digitalWrite(pinGreenLed, LOW); // Turn off green led
digitalWrite(pinYellowLed, HIGH); // Turn on yellow led
Serial.println("please arrange for a Osmosis filter change");
Particle.publish("Osmosis filter soon bad", NULL, 60, PRIVATE);
}
void TurnOnRelay_RedLed(){ // This function is called when EC meter stage 3 is HIGH
digitalWrite(pinGreenLed, LOW); // Turn off green led
digitalWrite(pinYellowLed, LOW); // Turn off yellow led
digitalWrite(pinRelay_RedLed, HIGH); // Turn on relay and red led
Serial.println("Please check/change Osmosis filter");
Particle.publish("System shut down", NULL, 60, PRIVATE);
}
void PressResetButton(){ // This function is called when reset button is pressed
digitalWrite(pinGreenLed, HIGH); // Turn on green led
digitalWrite(pinYellowLed, LOW); // Turn off yellow led
digitalWrite(pinRelay_RedLed, LOW); // Turn off relay and red led
Serial.println("All values are set to default");
Particle.publish("Reset press detected", NULL, 60, PRIVATE);
}
//-----------------------------------------------------------------------------
- I have seen on some references that everything in the loop is very clean and everything i have here should be called under functions instead, is there any pro/con for this?.
- i have some if statements, im not sure if this is the best way to go, What i want to happen is that if red led is already active there is no use to activate yellow led, however the yellow led should have been activated before the red led, its like a traffic light. Water is good = green led, water is going bad = yellow led, Water is bad = red led
- Any obvious way i can improve the code or make it more readable?
Many thanx
/Dimi