Section in Code Repeating as if Button is Always Pressed

Hi all,

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!

Any help with this would be amazing!

Thanks very much,

Zeb

GoBuddy_Work_in_Progress.ino (9.18 KB)

How is the input wired ?

Is it held LOW by a pulldown resistor when the button is not pressed or is it floating at an unknown, possibly HIGH, voltage ?

Investigate the use of INPUT_PULLUP in pinMode()

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.

Out of fairness the code in his post was code op got from somewhere else. Another reason to hate sites that allow crap like that to be posted.

Yes I agree that one should never use goto it usually indicates poor design.

However I did scan the code and for the most part op code is not bad.

As I am on my phone I can not give it a good look but I did not notice any goto. As to op issue hopefully someone can help.

Romonaga:
As I am on my phone I can not give it a good look but I did not notice any goto.

Last line of loop. And all it does is send it back to the top of loop. Which was going to happen anyway.

Hi Delta_G,

Thanks for pointing that out!

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!

Thanks again,

Zeb

Study this page:

http://www.gammon.com.au/switches

It will help you get your button wired up where it works right.

Hi all,

I have changed the pinMode line to pinMode(9, INPUT_PULLUP);.

It still has the same problem!!!

I have attached a diagram of the buzzer and button wiring.

Any help with this would be amazing as I am working on a school project with a deadline coming up!

Thanks for all your help!

Zeb

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.

Hi all,

I have used the "INPUT_PULLUP" command and wiring! (So no resistor, and no connection to the 5V line).

And it works!!!

Thanks heaps for all your help!

Zeb

Hi again,

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

Code:
Attached :slight_smile:

Any help or suggestions would be amazing!

Thanks,

Zeb

GoBuddy_Work_in_Progress.ino (9.07 KB)

  if (SendLoRaData = 1)

Wrong !

Ahh do you mean I should replace "=" with "=="?

Thanks,

Zeb

ZebH:
Ahh do you mean I should replace "=" with "=="?

Thanks,

Zeb

Yes

Hi UKHeliBob,

Thanks for your help! I have changed "=" to "==" but unfortunately the problem still persists!

If (SendLoRaData == 1)

Do you have any other ideas why this might be?

Thanks again,

Zeb

Thanks for your help! I have changed “=” to “==” but unfortunately the problem still persists!

What is the pattern of beeps/tone that you hear when you press the button?

I suggest that you reduce you code to achieve a “MCVE” (Minimal,Complete,Verifiable,Example) which demonstrates the dual send.

You are likely to find the root cause doing this.

Hi cattledog,

Thanks for the link!

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)!

Thanks,

Zeb

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

Hi cattedog,

I have tried what you suggested:

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

Any help would great!!!

Thanks,

Zeb