I am using 2 pushbuttons, an analog Sensor and a motor.
Conditions:
When pushbutton 1 is pressed if sensor value >500 the motor has to be turned on. If the pushbutton is pressed second time the motor has to go off.
When pushbutton 2 is pressed if sensor value <500 the motor has to be turned on. If the pushbutton is pressed second time the motor has to go off.
Help me to run this. Motor starts itself with my code
int buttonPin = 7;
int waterPump=12;
int enable = 11;
int pumpState = HIGH;
int buttonState;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT);
pinMode(enable, OUTPUT);
pinMode(waterPump, OUTPUT);
digitalWrite(waterPump, pumpState);
}
void loop() {
int reading = digitalRead(buttonPin);
int SensorValue=analogRead(A0);
Serial.println(SensorValue);
delay(1000);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == HIGH) {
pumpState = !pumpState;
}
}
}
{
digitalWrite(enable, HIGH);
digitalWrite(waterPump, pumpState);
lastButtonState = reading;
}}