I have a section in my code that detects when the button is pressed and if it is it gotos a section of code which does a countdown with the beeper.
The problem is that it is repeating the Countdown again and again as if the button is always pressed down. When debugging to Serial Monitor it always puts out a “1” no matter what you do with the button.
I uploaded this code here and the button and beeper worked great!
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 9; // the number of the pushbutton pin
int piezoPin = 8;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
Start: {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
tone(piezoPin, 1000);
}
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
noTone(piezoPin);
goto Start;
}
}
}
Then I uploaded the section of code I have been working on that has the Countdown section in it doesn’t work! (problem described at top of post:)
The button and beeper pins are the same of course!
Code: Code is attached as I exceeded the maximum character limit!
Button and beeper wiring diagrams can be supplied if needed!
What’s the purpose of the goto besides stopping people from trying to figure out your problem? Don’t you know that the loop function already repeats itself?
There is never any good reason for a goto in C or C++ code. If you find yourself typing goto, just stop because you’re doing it wrong.
Here is the updated version of the test buzzer and button code:
// constants won't change. They're used here to set pin numbers:
const int buttonPin = 9; // the number of the pushbutton pin
int piezoPin = 8;
// variables will change:
int buttonState = 0; // variable for reading the pushbutton status
void setup() {
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}
void loop() {
// read the state of the pushbutton value:
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
tone(piezoPin, 1000);
}
buttonState = digitalRead(buttonPin);
if (buttonState == LOW) {
noTone(piezoPin);
}
}
Thanks for your suggestions UKHeliBob I am researching them now!
I have changed the pinMode line to
Code: [Select]
pinMode(9, INPUT_PULLUP);
.
Did you change the code logic to reflect that when the button is not pressed the digitalRead() value is HIGH and when the button is pressed the digitalRead()value is LOW.
you need to go study that link I gave you some more. There’s more to it than just putting INPUT_PULLUP. You also have to wire your button the right way.
When you’re done it will read LOW when it is pressed and HIGH when it is not pressed. It won’t have any resistors and it won’t be connected to 5V line at all.
Go look at that link again and study it. Keep at it until you understand why things have to be done that way.
I have got another problem where if the button is pressed or accelerometer hit it sends the Latitude and Longitude twice. (I think it is repeating the “SendLatandLong” section twice.)
When I press the button it does 4 slow beeps and 3 fast beeps. It then goes to the section that sends the lat and long and it beeps once when the lat has been sent and once when the long has been sent.
The problem is that it is repeating the send lat and long section twice! So sends the lat and long over LoRa once, then it send the lat and long again! So the buzzer beeps 4 times in total (lat, long, lat, long)!
Try separating the gps conditions in the SendLoRaData conditional statement
if (SendLoRaData = 1)
{
while (ss.available() > 0)
{
gps.encode(ss.read());
}
if (gps.location.isValid()) //could also try gps.location.isUpdated()
{
SendLatandLong(); //goto the section where it sends the latitude and longitude
}
}
if (SendLoRaData = 1)
{
while (ss.available() > 0)
{
gps.encode(ss.read());
}
if (gps.location.isValid()) //could also try gps.location.isUpdated()
{
SendLatandLong(); //goto the section where it sends the latitude and longitude
}
}
This code for some reason makes "SendLoRaData" always equal "1". (Discovered through Serial.read commands).