1

If I press my button once, I get characters on all of the 32 character places and I get an infinite tone with my piezo. Can someone help me with this?

Arduino circuit

#include <LiquidCrystal.h>

const int buttonPin = A0;
int buttonState = 0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
byte run1c[8] = {
  B00000,
  B01100,
  B01100,
  B00000,
  B01110,
  B11100,
  B01110,
  B10010,
};

byte run2c[8] = {
  B00000,
  B01100,
  B01100,
  B00000,
  B11100,
  B01110,
  B11100,
  B10010,
};

byte jumpc[8] = {
  B01100,
  B01100,
  B00000,
  B11110,
  B01101,
  B11111,
  B10000,
  B00000,
};

byte jumpLowerc[8] = {
  B01110,
  B11100,
  B01110,
  B10000,
  B00000,
  B00000,
  B00000,
  B00000,
};

byte groundc[8] = {
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
  B11111,
};

byte rightGroundc[8] = {
  B00011,
  B00011,
  B00011,
  B00011,
  B00011,
  B00011,
  B00011,
  B00011,
};

byte leftGroundc[8] = {
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
  B11100,
};

void setup() {
  lcd.createChar(0, run1c);
  lcd.createChar(1, run2c);
  lcd.createChar(2, jumpc);
  lcd.createChar(3, jumpLowerc);
  lcd.createChar(4, groundc);
  lcd.createChar(5, rightGroundc);
  lcd.createChar(6, leftGroundc);
  Serial.begin(9600);
  lcd.begin(16, 2);
  pinMode(buttonPin, INPUT);
}

void loop() {
  buttonState = digitalRead(buttonPin);
  if(buttonState == HIGH){
    tone(8, 400);
    lcd.setCursor(1,0);
    lcd.write(byte(2));
    digitalWrite(buttonState, LOW);
  } else {
    lcd.setCursor(1,1);
    lcd.write(byte(0));
  }
}
  • digitalWrite(buttonState, LOW); That line is writing either pin 0 or pin 1 low. Did you mean to just set buttonState = LOW; – Delta_G May 21 '17 at 18:35
  • @Delta_G yes, I thought that would fix the problem, so i just have to write: buttonState = LOW; – Termatinator May 21 '17 at 18:43
  • You shouldn't have to write that at all. It should set buttonState from the digitalRead at the top of loop right before it compares it. But either way, trying to digitalWrite with another pins state as the pin number is definitely not right. – Delta_G May 21 '17 at 18:48
  • @Delta_G And if I move that white wire, connected to A0, to pin number 13, and change that in the code. Will that make a difference? – Termatinator May 21 '17 at 18:53
  • Sure, it will make it so that the code you have is reading the wrong pin. If you change the pin number in the code as well then you're in the same boat. Why don't you try an active LOW arrangement for the button like I described in my answer below. – Delta_G May 21 '17 at 18:55
1

From what I can tell from your drawing, you have both sides of your button tied to 5V. The green wire on one side and the blue wire through the resistor on the other. So yes, the button will always read HIGH.

Instead, remove the resistor, tie one side of the button to the pin and the other side to ground. Use INPUT_PULLUP in your pinMode cal instead of just INPUT. Now a pressed button will read LOW and not pressed will read HIGH, so you'll have to reverse your logic in your code. But the wiring will be much nicer.

  • the blue wire is connected to the lcd display with a 220 ohm resistor, the 10k ohm resistor is connected to ground – Termatinator May 21 '17 at 18:42
0

In your code you check the button state and if it is high you set the tone.

In the else section you do nothing with the tone.

Hence, once on, it will always stay on.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.