TinyCoder
TinyCoder

Reputation: 33

Switching a bulb ON and OFF using toggle switch in Arduino

Kindly help me in the Arduino code as I am new in this field. I have Arduino code that turns the light in bulb ON and OFF using toggle switch. It is successfully running and giving output.

The following is the code:

int buttonPinBulb = 11;
int relay1 = 10;

void setup() {
  pinMode(buttonPinBulb, INPUT_PULLUP);
  pinMode(relay1, OUTPUT);
  Serial.begin(115200);
}

void loop() {
  // put your main code here, to run repeatedly:
  int buttonBulb = digitalRead(buttonPinBulb);
  
  if(buttonBulb == HIGH){
    digitalWrite(relay1, HIGH);
  } else {
    digitalWrite(relay1, LOW);
  }
  Serial.println(buttonBulb);
}

Before following suggestion in the comment, the output was:

when switch is OFF

when switch is ON

Issue

Bulb is turning ON and OFF when I toggle switch ON and OFF, and the output is showing on serial monitor continuously. But I want only one value that is not repeated. Like if I toggle the switch ON, then the value shown on serial monitor should be 1 and not 11111111....

Please help me about that. How can I do that?

After following suggestion in the comment, the code is:

int buttonPinBulb = 11;
int relay1 = 10;
int buttonBulb;
void setup() {
  pinMode(buttonPinBulb, INPUT_PULLUP);
  pinMode(relay1, OUTPUT);
  Serial.begin(115200);
  Serial.println(buttonBulb);
}

void loop() {
  // put your main code here, to run repeatedly:
  buttonBulb = digitalRead(buttonPinBulb);
  
  if(buttonBulb == HIGH){
    digitalWrite(relay1, HIGH);
  }else{
    digitalWrite(relay1, LOW);
  }
 //Serial.println(buttonBulb);
}

And the output was: either the switch is OFF or ON the value is the same it should change when I switch ON

Upvotes: 0

Views: 1295

Answers (2)

Danny_ds
Danny_ds

Reputation: 11406

You can use a global variable to store the current status of the button.

You may also want to debounce your button (using millis() in the below example, unless the debouncing is already done in hardware) - especially when you want to update a database each time the status changes.

int buttonPinBulb = 11;
int relay1 = 10;
int currentStatus = LOW;

// Debounce
unsigned long lastMillis = 0;
const unsigned long debounceTime = 100; // 100ms wait

void setup() {

    pinMode(buttonPinBulb, INPUT_PULLUP);
    pinMode(relay1, OUTPUT);
    Serial.begin(115200);
}

void loop() {

    unsigned long currentMillis = millis();

    if ( (currentMillis - lastMillis > debounceTime)
         || (currentMillis < lastMillis)) {  // protect against overflow
        
        int buttonBulb = digitalRead(buttonPinBulb);

        if (buttonBulb != currentStatus) {

            digitalWrite(relay1, buttonBulb);
            Serial.println(buttonBulb);
            currentStatus = buttonBulb;

            // update database here
        }

        lastMillis = currentMillis; 
    }
}

Upvotes: 1

Roberto Caboni
Roberto Caboni

Reputation: 7490

As you have been told in comments, just store the current value of the pin and issue digitalWrite () only if a change is required.

ccesfully running and giving output. The following is the code:

int buttonPinBulb = 11;
int relay1 = 10;
int curRel1;

void setup()
{
    pinMode(buttonPinBulb, INPUT_PULLUP);
    pinMode(relay1, OUTPUT);
    curRel1 = digitalRead(relay1);
    Serial.begin(115200);
}

void loop()
{
    int buttonBulb = digitalRead(buttonPinBulb);
  
    if(buttonBulb == HIGH)
    {
        digitalWrite(relay1, HIGH);
    }
    else
    {
        digitalWrite(relay1, LOW);
    }
 
    if (buttonBulb != curRel1)
    {
        Serial.println(buttonBulb);
        curRel1 = buttonBulb;
    }
}

You could also update curRel1 in loop() calling digitalRead().

Upvotes: 0

Related Questions