Hello, I would like to do a button control but in two modes. I need to do it and intermittently. Change of mode would be done by pressing the button. What am I doing wrong?
int pbuttonPin = 12;// connect output to push button
int relayPin = 2;// Connected to relay (LED)
int przelacznik = 9; // connect switch
int val = 0; // push value from pin 2
int lightON = 0;//light status
int pushed = 0;//push status
int przelaczenie = 0; // switch status
int buttonPresses = 0; // counter for the number of button presses
int lastPressCount = 0; // previous state of the button
void setup() {
// Robojax.com code and video tutorial for push button ON and OFF
Serial.begin(9600);
pinMode(pbuttonPin, INPUT_PULLUP);
pinMode(relayPin, OUTPUT);
pinMode(przelacznik, INPUT);
}
void mode_one() {
val = digitalRead(pbuttonPin);// read the push button value
if (val == HIGH && lightON == LOW) {
pushed = 1 - pushed;
delay(100);
}
lightON = val;
if (pushed == HIGH) {
digitalWrite(relayPin, LOW);
} else {
digitalWrite(relayPin, HIGH);
}
}
void mode_two() {
if (pbuttonPin == LOW)
{
digitalWrite(relayPin, LOW);
} else {
digitalWrite(relayPin, HIGH);
}
}
void loop() {
if (digitalRead(przelacznik) == LOW) // check if button was pressed
{
buttonPresses++; // increment buttonPresses count
delay(250); // debounce switch
}
if (buttonPresses == 2) buttonPresses = 0; //resets button count to zero on second push
{ (lastPressCount != buttonPresses);
}
if (buttonPresses == 0)
{
Serial.println("0");
mode_one();
} else {
Serial.println("1");
mode_two();
}
}