0

I have been trying to get the button to work properly for over a day, but it just won't, can you help me.

The lcd display is for another part of this project, not important at the moment, but I can't take it away.

Also, the piezo plays the sound within else{}.

const int buttonPin = A0;

int buttonState = 0;

void setup() {

  Serial.begin(9600);

  pinMode(buttonPin, INPUT);
}

void loop() {


  if (buttonState == HIGH) {
    tone(8, 400);
  }else{
    tone(8, 300);
    delay(375);
    tone(8, 200);
    delay(375);
    tone(8, 100);
    delay(750);
    tone(8, 400);
    delay(250);
    noTone(8);
    delay(5000);
  }


}

Arduino circuit

  • You seem to have forgotten to actually read the button state from the button... – Majenko May 21 '17 at 12:35
  • @Majenko you mean this? buttonState = digitalRead(buttonPin); – Termatinator May 21 '17 at 13:26
  • buttonState = digitalRead(buttonPin); – Termatinator May 21 '17 at 13:26
  • Yes. Without that how can your program know what the button is doing? – Majenko May 21 '17 at 13:27
  • 1
    So try your posted code for yourself and see if it does what it should do. Don't try and second guess yourself - the first step of debugging something is to write a program that tests that one thing and then see what the results are. – Majenko May 21 '17 at 13:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.