Switching a bulb ON and OFF using toggle switch in Arduino

∥☆過路亽.° 提交于 2021-02-11 14:21:39

问题


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:

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:


回答1:


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; 
    }
}



回答2:


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().



来源:https://stackoverflow.com/questions/64181338/switching-a-bulb-on-and-off-using-toggle-switch-in-arduino

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!