Hi, I get issues when I pressed button. I don't get any value for console. Any rare character as $,aa but not as boolean 1 or 0 as I have written on my code. I try with Serial.println
Other problem.
When I charged my code always get the relay turn on. I want my relay turn off with respect on the led if is on or off.
/* Relays */
#define PUMP_PIN D6 //PUMP (Red LED)
#define LAMP_PIN D7 //LAMP (Green LED)
boolean pumpStatus = 0;
boolean lampStatus = 0;
/* Buttons */
#define PUMP_ON_BUTTON D9 //push-button PUMP (Red)
#define LAMP_ON_BUTTON D10 //push-button LAMP (Green)
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
delay(10);
Serial.println("ArduFarmBot 2");
Serial.println(".... Starting Setup");
Serial.println(" ");
pinMode(PUMP_PIN, OUTPUT);
pinMode(LAMP_PIN, OUTPUT);
pinMode(PUMP_ON_BUTTON, INPUT_PULLUP);
pinMode(LAMP_ON_BUTTON, INPUT_PULLUP);
digitalWrite(PUMP_PIN, LOW);
digitalWrite(LAMP_PIN, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
readLocalCmd();
}
/****************************************************************
* Read local commands (Pump and Lamp buttons are normally "HIGH"):
****************************************************************/
void readLocalCmd()
{
boolean digiValue = debounce(PUMP_ON_BUTTON);
Serial.println(digiValue);
if(!digiValue)
{
pumpStatus = !pumpStatus;
aplyCmd();
}
digiValue = debounce(LAMP_ON_BUTTON);
if(!digiValue)
{
lampStatus = !lampStatus;
aplyCmd();
}
}
/***************************************************
* Receive Commands and act on actuators
****************************************************/
void aplyCmd()
{
if(pumpStatus == 1)
{
digitalWrite(PUMP_PIN, HIGH);
}
else
{
digitalWrite(PUMP_PIN, LOW);
}
if(lampStatus == 1)
{
digitalWrite(LAMP_PIN, HIGH);
}
else
{
digitalWrite(LAMP_PIN, LOW);
}
}
/***************************************************
* Debouncing a key
****************************************************/
boolean debounce(int pin)
{
boolean state;
boolean previousState;
const int debounceDelay = 30;
previousState = digitalRead(pin);
for(int counter=0; counter< debounceDelay; counter++)
{
delay(1);
state = digitalRead(pin);
if(state != previousState)
{
counter = 0;
previousState = state;
}
}
return state;
}
