Turn ''ON'' if the button is being pressed and ''OFF'' if pressed again.

 BUTTON STATE

Button state


ARDUINO CODES


const int BUTTON_PIN = 7; // Arduino pin connected to button's pin

const int RELAY_PIN  = 3; // Arduino pin connected to relay's pin


void setup() {

  Serial.begin(9600);                // initialize serial

  pinMode(BUTTON_PIN, INPUT_PULLUP); // set arduino pin to input pull-up mode

  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode

}


void loop() {

  int buttonState = digitalRead(BUTTON_PIN); // read new state


  if (buttonState == HIGH) {

    Serial.println("The button is being pressed");

    digitalWrite(RELAY_PIN, HIGH); // turn on

    if (buttonState == HIGH) {

    Serial.println("The button is being pressed");

    digitalWrite(RELAY_PIN, LOW); // turn on

  }

  }

Post a Comment

0 Comments