Home > front end >  C Assignment - Why isn't the push button working?
C Assignment - Why isn't the push button working?

Time:07-30

I am trying to do the following on an Arduino:

Task: Create a toggle switch that controls when 3 LEDs switch on (at the same time). In a toggle switch, when the button is pressed, the LEDs switch on. When it is pressed again, the LEDs switch off.

My pushbutton isn't turning on to turn on the LEDs The code is:

// C   code
//Declare
int ledPin13 = 13;
int ledPin12 = 12;
int ledPin11 = 11;
int buttonPin = 1;
int buttonState;

//State if input or output
void setup() {
  pinMode(buttonPin , INPUT);
  pinMode(ledPin13 , OUTPUT);
  pinMode(ledPin12 , OUTPUT);
  pinMode(ledPin11 , OUTPUT);
}

//Stage 3 - Code what you want to do (according to psuedocode)
void loop() {
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH) {
    digitalWrite(ledPin13 , HIGH);
 
    digitalWrite(ledPin12 , HIGH);
  
    digitalWrite(ledPin11 , HIGH);

  }
  else {
    digitalWrite(ledPin13 , LOW);
    digitalWrite(ledPin12 , LOW);
    digitalWrite(ledPin11 , LOW);
  }
}

CodePudding user response:

bool toggleState = 0;    
buttonState = digitalRead(buttonPin);
delay(100);
if( buttonState == 1)
{
    if(toggleState)
        toggleState = 0;
    else
        toggleState = 1;
}
if(toggleState)
{
    digitalWrite(ledPin13 , HIGH);
    digitalWrite(ledPin12 , HIGH);
    digitalWrite(ledPin11 , HIGH);
}
else
{
    digitalWrite(ledPin13 , LOW);
    digitalWrite(ledPin12 , LOW);
    digitalWrite(ledPin11 , LOW);
}

CodePudding user response:

It is because your program continuously reads a state. It means the time of pressing the button in compare with the checking of the state by MCU is too mush . for fixing this problem you have two options:

  1. Checking the button. You have to check the state but It is better to use debouncing which means adding delay between two reads.
bool LEDstate = false;
void loop()
{
  if (digitalRead(buttonPin) == HIGH)
    {
      delay(500);  // to be stable
      if (digitalRead(buttonPin) == HIGH)
        {
          if (LEDstate == false)
            {
              digitalWrite(ledPin13, HIGH);
              digitalWrite(ledPin12, HIGH);
              digitalWrite(ledPin11, HIGH);
              LEDstate = true;
            }
          else
            {
              digitalWrite(ledPin13, LOW);
              digitalWrite(ledPin12, LOW);
              digitalWrite(ledPin11, LOW);
              LEDstate = false;
            }
            while (digitalRead(buttonPin) == HIGH); // wait to release
        }
    }
}
  1. Inverse reading, which means you have to read the pressing and then wait to release to change the state of LEDs
bool LEDstate = false;
void loop()
{
  if (digitalRead(buttonPin) == HIGH)
    {
      delay(100);  // to be stable
      while (digitalRead(buttonPin) == HIGH); // wait to release

      if (LEDstate == false)
        {
          digitalWrite(ledPin13, HIGH);
          digitalWrite(ledPin12, HIGH);
          digitalWrite(ledPin11, HIGH);
          LEDstate = true;
        }
      else
        {
          digitalWrite(ledPin13, LOW);
          digitalWrite(ledPin12, LOW);
          digitalWrite(ledPin11, LOW);
          LEDstate = false;
        }
    }
}

  • Related