Section in Code Repeating as if Button is Always Pressed

This is the space to ask all your burning tech questions and discuss the Skills Bright Sparks competition. We are here to help!
Post Reply
ZebH
Posts: 99
Joined: Thu May 12, 2016 4:50 pm

Section in Code Repeating as if Button is Always Pressed

Post by ZebH » Wed Aug 07, 2019 7:41 am

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!

Code: Select all

// 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: Select all

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/* INITIALISE HARDWARE */
const int buttonPin = 9;     // set the pushbutton pin number
const int buzzerPin = 8;        // set the buzzer pin
static const int RXPin = 6, TXPin = 7; // set pins for GPS serial rx and tx
static const uint32_t GPSBaud = 9600;   // set GPS Baud rate
TinyGPSPlus gps;    // The TinyGPS++ object
SoftwareSerial ss(RXPin, TXPin);    // Define the serial connection to the GPS device
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);   //Assign a unique ID to the accelerometer
/* INITIALISE CONSTANTS AND VARIABLES */
int rxtime1 = 0;    // initialise a variable  to record the time a LoRa packet is received
int ago1 = 54321;   // initialise a variable for how long ago a LoRa packet was received
int countdownSeconds = 0;   //variable that keeps track of how many times the beeper has beeped for the Countdown
int SendLoRaData = 0;   //variable to show if a Help message needs to be sent with the Lat and Long
int buttonState = 0;         // initialise a variable for pushbutton status, starts at 0 (LOW)
const byte numChars = 32; // initialise a constant to use for setting the length of variables
char receivedChars[numChars];   // initialise a variable to store data received from LoRa (no initial value provided????????????????)
char lat1[numChars];    // initialise a variable to store latitude coordinate (no initial value provided??????????????????)
char lng1[numChars];    // initialise a variable to store longitude coordinate (no initial value provided????????????????)
char BT_input=' ';  // set initial Bluetooth input string
boolean newData = false;  // tell it initially that there is no new data available from LoRa

void setup() {
  Serial.begin(9600);   //start arduino serial comms
  pinMode(buttonPin, INPUT);    //Set button pin as an input
  ss.begin(GPSBaud);    //start gps serial comms
  
/* WAIT FOR ARDUINO SERIAL COMMS TO BECOME AVAILABLE */
  while (!Serial);
 
/* WAIT FOR LORA SERIAL COMMS TO BECOME AVAILABLE */
  //Serial.println("Starting LoRa Sender and Receiver");
  if (!LoRa.begin(915E6)) {
  Serial.println("Starting LoRa failed!");
  while (1);    // continue saying LoRa failed as long as LoRa begin has not happened
  }
 
{
/* REMOVE THIS PIECE OF CODE? */
 #ifndef ESP8266 //LoRa, What exactly is this achieving?  can it be removed? *******************
 while (!Serial); // for Leonardo/Micro/Zero
 #endif
 
 /* WAIT FOR THE ACCELEROMETER TO BECOME AVAILABLE BEFORE PROCEEDING */
 if(!accel.begin()) {
 Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
 while (1); // wait until accelerometer becomes available
 }
 
 }
 
 Serial.println ("Check if receivedChars initialised correctly "); //debug
 Serial.println (receivedChars); //debug
 }
 
 
void loop() {
    
 /*If the Countdown Has Finished*/ 
 if (SendLoRaData = 1) {
  while (ss.available() > 0){   // wait until data is available from the GPS
  if ((gps.encode(ss.read())) and (gps.location.isValid())){ //???????????????? and the GPS location is valid/availble
  SendLatandLong(); //goto the section where it sends the latitude and longitude
  }
  }
 }

 /*If the Button is Pressed*/
 buttonState = digitalRead(buttonPin);
 Serial.print ("Button State");
 Serial.print (buttonState);
 if (buttonState == HIGH) {
 countdownSeconds = 0; //set the variable that keeps track of how many times the beeper has beeped for the Countdown
 Countdown(); //goto the section that does the Slow Countdown before sending the Latitude and Longitude
 }
 if (countdownSeconds > 2){ //if it has already done the slow countdown section goto the fast countdown section before sending the Latitude and Longitude
  LastChance(); //goto the section that does the fast countdown before sending the Latitude and Longitude
 }
 /*Receive a LoRa Packet*/
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;
 /*try to parse a LoRa packet*/
 int packetSize = LoRa.parsePacket();
  if (packetSize) {
  while (LoRa.available()) {
  rc = LoRa.read(); //whatever is availble in the LoRa buffer goes into the variable rc
  if (rc != endMarker) { //if variable rc does not include "/n"
  receivedChars[ndx] = rc; //stores the received data into received Chars
  ndx++; //adds 1 to ndx
  if (ndx >= numChars) { //if received characters above or equals 32
  ndx = numChars - 1; //variable ndx = 32 - 1 which equals 31
  }
  }
  else {
  receivedChars[ndx] = '\0'; // terminate the string
  ndx = 0;
  newData = true; //variable for testing whether new data has been recieved == true which means there has been new LoRa data received
  }
  }
  }
  /*if New LoRa Data Received*/
  if (newData == true) { //if new data recieved
  NewData(); //goto the section that stores the Latitude, Longitude and the time the packet was received
  }

   /*If Missed Messages Button clicked on phone app and there is a Help message that has not been read goto the section that sends the Latitude, Longitude and Time Received*/
   if (Serial.available())
   {
   BT_input = Serial.read(); //variable BT_input = incoming Serial Data
   /*if the refresh button is pressed on the phone app*/
   if (( BT_input == 48) and (ago1 == 54321))      //if incoming BT data = 0 (ascii code for 0 is dec 48) but all Help messages that have been read
   {
   Serial.print ("No Messages");
   delay (20);
  }
 
  if (( BT_input == 48) and (ago1 != 54321)){  //if incoming BT data = 0 (ascii code for 0 is dec 48) and there is a Help messages that has not been read
  BTData();
  }
  }
  /*Check the accelerometer for an accident event*/
  sensors_event_t event;
  accel.getEvent(&event);
 
  /* Display the results (acceleration is measured in m/s^2) */
  if (event.acceleration.y + event.acceleration.x > 15 || event.acceleration.y + event.acceleration.x < -15) {
  countdownSeconds = 0; //set the variable that keeps track of how many times the beeper has beeped for the Countdown
  Countdown(); //goto the section where the beeper does the slow countdown
  }
  delay (10);
}

void SendLatandLong(){
  /*Send the Latitude via LoRa*/
  LoRa.beginPacket(); //begin the LoRa packet
  LoRa.print ("lat");
  delay (20);
  LoRa.print (gps.location.lat(), 6); //latitude
  delay (20);
  LoRa.print ("\n"); //refered to as an endMarker in this program
  delay (20);
  LoRa.endPacket(); //end the LoRa packet
  delay (20);
 
  /*Beep to show that a packet has been sent*/
  tone (buzzerPin,1000,20);
  delay(1000);
  noTone (buzzerPin);
  
  /*Send the Longitude via LoRa*/
  LoRa.beginPacket(); //begin the LoRa packet
  LoRa.print ("lng");
  delay(20);
  LoRa.print (gps.location.lng(), 6); //longitude
  delay (20);
  LoRa.print ("\n"); //refered to as an endMarker in this program
  delay(20);
  LoRa.endPacket(); //end the LoRa packet
  delay (20);

   /*Beep to show that a packet has been sent*/
  tone (buzzerPin,1000,20);
  delay(1000);
  noTone (buzzerPin);
  SendLoRaData = 0; //set the variable that detects if a LoRa packet should be sent to "0"
  }
  
void Countdown(){
  /*Section that Countsdown Slowly With a Buzzer*/
  tone (buzzerPin,1000,20);
  delay(1000);
  countdownSeconds++;
  if (countdownSeconds < 3) Countdown(); //if the buzzer has beeped less than 3 times go back to top of Countdown Section
}
  void LastChance(){
  /*Section that Countsdown Fast With the Buzzer*/
  if (countdownSeconds > 2) countdownSeconds = 0; //if the variable countdownSeconds is not reset from the Countdown section, reset the variable countdownSeconds 
  tone (buzzerPin,1000,20);
  delay (500);
 
  tone (buzzerPin,1000,20);
  delay(500);
 
  countdownSeconds++;
  if (countdownSeconds < 2) LastChance(); //if beeped less than twice, goto the top of the LastChance section
  SendLoRaData = 1; //set the variable SendLoRaData to "1" to show that a Help message needs to be sent with the Lat and Long
  }
  
  
  void NewData(){
  /*If New LoRa Data Has Been Received Send this Data to the phone and Store the Lat and Long Data in Variables*/
  ago1 = 0;
  Serial.print(receivedChars); //send received data over bluetooth to the phone
 
  if (strstr(receivedChars, "lat")){ //if data received include the word "lat" (received the latitude)
  strcpy (lat1,receivedChars); //make variable lat1 = the received data
  }
 
  if (strstr(receivedChars, "lng")){ //if data recieved include the word "lng" (received the lng)
  strcpy (lng1,receivedChars); //make variable lng2 = the received data
  rxtime1 = millis(); //time since arduino powered up (for setting how long ago the message was received)
  }
  newData = false; //make the variable that detects if new LoRa data has been received = false to show that there is no new LoRa data
  }
  
  
  void BTData(){
  /*If Missed Messages Button Clicked on Phone App*/
  ago1 = (millis() - rxtime1) /1000 / 60; //the variable that stores how long ago the packet was received = the millis now minus the millis when the packet was received then convert this into minutes
  delay (20);
  Serial.print (lat1); //send the Latitude over BT
  delay (1000);
 
  Serial.print (lng1); //send the Longitude over BT
  delay(1000);
 
  Serial.print (ago1); //send the minutes since received over BT
  delay(20);
  ago1 = 54321; //set the variable ago1 to "54321" to show last Help message has been read
  }
Button and beeper wiring diagrams can be supplied if needed!

Any help with this would be amazing!

Thanks very much!
~Zeb

Ben10
Posts: 21
Joined: Tue Nov 10, 2015 4:50 pm

Re: Section in Code Repeating as if Button is Always Pressed

Post by Ben10 » Wed Aug 07, 2019 9:18 am

Appears that you might need to debounce the input. Debouncing checks the button twice in a short period of time to make sure the button is actually pressed.

Here is the official arduino post:
https://www.arduino.cc/en/tutorial/debounce

ZebH
Posts: 99
Joined: Thu May 12, 2016 4:50 pm

Re: Section in Code Repeating as if Button is Always Pressed

Post by ZebH » Wed Aug 07, 2019 4:49 pm

Hi Ben10,

Thanks for your help!

I this example possibly have a fault? The variable "reading" never seems to be initialized!

Code: Select all

/*
  Debounce

  Each time the input pin goes from LOW to HIGH (e.g. because of a push-button
  press), the output pin is toggled from LOW to HIGH or HIGH to LOW. There's a
  minimum delay between toggles to debounce the circuit (i.e. to ignore noise).

  The circuit:
  - LED attached from pin 13 to ground
  - pushbutton attached from pin 2 to +5V
  - 10 kilohm resistor attached from pin 2 to ground

  - Note: On most Arduino boards, there is already an LED on the board connected
    to pin 13, so you don't need any extra components for this example.

  created 21 Nov 2006
  by David A. Mellis
  modified 30 Aug 2011
  by Limor Fried
  modified 28 Dec 2012
  by Mike Walters
  modified 30 Aug 2016
  by Arturo Guadalupi

  This example code is in the public domain.

  http://www.arduino.cc/en/Tutorial/Debounce
*/

// constants won't change. They're used here to set pin numbers:
const int buttonPin = 2;    // the number of the pushbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(buttonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;
}
Or am I missing something......

Thanks again Ben10!
~Zeb

ZebH
Posts: 99
Joined: Thu May 12, 2016 4:50 pm

Re: Section in Code Repeating as if Button is Always Pressed

Post by ZebH » Wed Aug 07, 2019 4:50 pm

..............oops I did miss something!

Code: Select all

  int reading = digitalRead(buttonPin);
Thanks and sorry about that!
~Zeb

ZebH
Posts: 99
Joined: Thu May 12, 2016 4:50 pm

Re: Section in Code Repeating as if Button is Always Pressed

Post by ZebH » Wed Aug 07, 2019 5:07 pm

Hi again Ben10!

I have followed the tutorial you provided and used the code/concept in my program.

Unfortunately it still has the same problem!

Here is my code:

Code: Select all

#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_ADXL345_U.h>
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
/* INITIALISE HARDWARE */
const int buttonPin = 9;     // set the pushbutton pin number
const int buzzerPin = 8;        // set the buzzer pin
static const int RXPin = 6, TXPin = 7; // set pins for GPS serial rx and tx
static const uint32_t GPSBaud = 9600;   // set GPS Baud rate
TinyGPSPlus gps;    // The TinyGPS++ object
SoftwareSerial ss(RXPin, TXPin);    // Define the serial connection to the GPS device
Adafruit_ADXL345_Unified accel = Adafruit_ADXL345_Unified(12345);   //Assign a unique ID to the accelerometer
/* INITIALISE CONSTANTS AND VARIABLES */
int rxtime1 = 0;    // initialise a variable  to record the time a LoRa packet is received
int ago1 = 54321;   // initialise a variable for how long ago a LoRa packet was received
int countdownSeconds = 0;   //variable that keeps track of how many times the beeper has beeped for the Countdown
int SendLoRaData = 0;   //variable to show if a Help message needs to be sent with the Lat and Long
//int buttonState = 0;         // initialise a variable for pushbutton status, starts at 0 (LOW)
const byte numChars = 32; // initialise a constant to use for setting the length of variables
char receivedChars[numChars];   // initialise a variable to store data received from LoRa (no initial value provided????????????????)
char lat1[numChars];    // initialise a variable to store latitude coordinate (no initial value provided??????????????????)
char lng1[numChars];    // initialise a variable to store longitude coordinate (no initial value provided????????????????)
char BT_input=' ';  // set initial Bluetooth input string
boolean newData = false;  // tell it initially that there is no new data available from LoRa

unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

void setup() {
  Serial.begin(9600);   //start arduino serial comms
  pinMode(buttonPin, INPUT);    //Set button pin as an input
  ss.begin(GPSBaud);    //start gps serial comms

  
  
/* WAIT FOR ARDUINO SERIAL COMMS TO BECOME AVAILABLE */
  while (!Serial);
 
/* WAIT FOR LORA SERIAL COMMS TO BECOME AVAILABLE */
  //Serial.println("Starting LoRa Sender and Receiver");
  if (!LoRa.begin(915E6)) {
  Serial.println("Starting LoRa failed!");
  while (1);    // continue saying LoRa failed as long as LoRa begin has not happened
  }
 
{
/* REMOVE THIS PIECE OF CODE? */
 #ifndef ESP8266 //LoRa, What exactly is this achieving?  can it be removed? *******************
 while (!Serial); // for Leonardo/Micro/Zero
 #endif
 
 /* WAIT FOR THE ACCELEROMETER TO BECOME AVAILABLE BEFORE PROCEEDING */
 if(!accel.begin()) {
 Serial.println("Ooops, no ADXL345 detected ... Check your wiring!");
 while (1); // wait until accelerometer becomes available
 }
 
 }
 
 Serial.println ("Check if receivedChars initialised correctly "); //debug
 Serial.println (receivedChars); //debug
 }
 
 
void loop() {

  int reading = digitalRead(buttonPin);
    
 /*If the Countdown Has Finished*/ 
 if (SendLoRaData = 1) {
  while (ss.available() > 0){   // wait until data is available from the GPS
  if ((gps.encode(ss.read())) and (gps.location.isValid())){ //???????????????? and the GPS location is valid/availble
  SendLatandLong(); //goto the section where it sends the latitude and longitude
  }
  }
 }

 /*If the Button is Pressed*/
 //buttonState = digitalRead(buttonPin);
 //Serial.print ("Button State");
 //Serial.print (buttonState);
 //if (buttonState == HIGH) {
 //countdownSeconds = 0; //set the variable that keeps track of how many times the beeper has beeped for the Countdown
 //Countdown(); //goto the section that does the Slow Countdown before sending the Latitude and Longitude
 //}

 if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
      Countdown();
      }
    }
  }
  lastButtonState = reading;
  
 if (countdownSeconds > 2){ //if it has already done the slow countdown section goto the fast countdown section before sending the Latitude and Longitude
  LastChance(); //goto the section that does the fast countdown before sending the Latitude and Longitude
 }
 /*Receive a LoRa Packet*/
 static byte ndx = 0;
 char endMarker = '\n';
 char rc;
 /*try to parse a LoRa packet*/
 int packetSize = LoRa.parsePacket();
  if (packetSize) {
  while (LoRa.available()) {
  rc = LoRa.read(); //whatever is availble in the LoRa buffer goes into the variable rc
  if (rc != endMarker) { //if variable rc does not include "/n"
  receivedChars[ndx] = rc; //stores the received data into received Chars
  ndx++; //adds 1 to ndx
  if (ndx >= numChars) { //if received characters above or equals 32
  ndx = numChars - 1; //variable ndx = 32 - 1 which equals 31
  }
  }
  else {
  receivedChars[ndx] = '\0'; // terminate the string
  ndx = 0;
  newData = true; //variable for testing whether new data has been recieved == true which means there has been new LoRa data received
  }
  }
  }
  /*if New LoRa Data Received*/
  if (newData == true) { //if new data recieved
  NewData(); //goto the section that stores the Latitude, Longitude and the time the packet was received
  }

   /*If Missed Messages Button clicked on phone app and there is a Help message that has not been read goto the section that sends the Latitude, Longitude and Time Received*/
   if (Serial.available())
   {
   BT_input = Serial.read(); //variable BT_input = incoming Serial Data
   /*if the refresh button is pressed on the phone app*/
   if (( BT_input == 48) and (ago1 == 54321))      //if incoming BT data = 0 (ascii code for 0 is dec 48) but all Help messages that have been read
   {
   Serial.print ("No Messages");
   delay (20);
  }
 
  if (( BT_input == 48) and (ago1 != 54321)){  //if incoming BT data = 0 (ascii code for 0 is dec 48) and there is a Help messages that has not been read
  BTData();
  }
  }
  /*Check the accelerometer for an accident event*/
  sensors_event_t event;
  accel.getEvent(&event);
 
  /* Display the results (acceleration is measured in m/s^2) */
  if (event.acceleration.y + event.acceleration.x > 15 || event.acceleration.y + event.acceleration.x < -15) {
  countdownSeconds = 0; //set the variable that keeps track of how many times the beeper has beeped for the Countdown
  Countdown(); //goto the section where the beeper does the slow countdown
  }
  delay (10);
}

void SendLatandLong(){
  /*Send the Latitude via LoRa*/
  LoRa.beginPacket(); //begin the LoRa packet
  LoRa.print ("lat");
  delay (20);
  LoRa.print (gps.location.lat(), 6); //latitude
  delay (20);
  LoRa.print ("\n"); //refered to as an endMarker in this program
  delay (20);
  LoRa.endPacket(); //end the LoRa packet
  delay (20);
 
  /*Beep to show that a packet has been sent*/
  tone (buzzerPin,1000,20);
  delay(1000);
  noTone (buzzerPin);
  
  /*Send the Longitude via LoRa*/
  LoRa.beginPacket(); //begin the LoRa packet
  LoRa.print ("lng");
  delay(20);
  LoRa.print (gps.location.lng(), 6); //longitude
  delay (20);
  LoRa.print ("\n"); //refered to as an endMarker in this program
  delay(20);
  LoRa.endPacket(); //end the LoRa packet
  delay (20);

   /*Beep to show that a packet has been sent*/
  tone (buzzerPin,1000,20);
  delay(1000);
  noTone (buzzerPin);
  SendLoRaData = 0; //set the variable that detects if a LoRa packet should be sent to "0"
  }
  
void Countdown(){
  /*Section that Countsdown Slowly With a Buzzer*/
  tone (buzzerPin,1000,20);
  delay(1000);
  countdownSeconds++;
  if (countdownSeconds < 3) Countdown(); //if the buzzer has beeped less than 3 times go back to top of Countdown Section
}
  void LastChance(){
  /*Section that Countsdown Fast With the Buzzer*/
  if (countdownSeconds > 2) countdownSeconds = 0; //if the variable countdownSeconds is not reset from the Countdown section, reset the variable countdownSeconds 
  tone (buzzerPin,1000,20);
  delay (500);
 
  tone (buzzerPin,1000,20);
  delay(500);
 
  countdownSeconds++;
  if (countdownSeconds < 2) LastChance(); //if beeped less than twice, goto the top of the LastChance section
  SendLoRaData = 1; //set the variable SendLoRaData to "1" to show that a Help message needs to be sent with the Lat and Long
  }
  
  
  void NewData(){
  /*If New LoRa Data Has Been Received Send this Data to the phone and Store the Lat and Long Data in Variables*/
  ago1 = 0;
  Serial.print(receivedChars); //send received data over bluetooth to the phone
 
  if (strstr(receivedChars, "lat")){ //if data received include the word "lat" (received the latitude)
  strcpy (lat1,receivedChars); //make variable lat1 = the received data
  }
 
  if (strstr(receivedChars, "lng")){ //if data recieved include the word "lng" (received the lng)
  strcpy (lng1,receivedChars); //make variable lng2 = the received data
  rxtime1 = millis(); //time since arduino powered up (for setting how long ago the message was received)
  }
  newData = false; //make the variable that detects if new LoRa data has been received = false to show that there is no new LoRa data
  }
  
  
  void BTData(){
  /*If Missed Messages Button Clicked on Phone App*/
  ago1 = (millis() - rxtime1) /1000 / 60; //the variable that stores how long ago the packet was received = the millis now minus the millis when the packet was received then convert this into minutes
  delay (20);
  Serial.print (lat1); //send the Latitude over BT
  delay (1000);
 
  Serial.print (lng1); //send the Longitude over BT
  delay(1000);
 
  Serial.print (ago1); //send the minutes since received over BT
  delay(20);
  ago1 = 54321; //set the variable ago1 to "54321" to show last Help message has been read
  }
Here is a diagram of my wiring of the button and buzzer part!
Arduino-Buzzer-and-Button-Wiring.jpg
Arduino-Buzzer-and-Button-Wiring.jpg (168.19 KiB) Viewed 217 times
Please let me know if I have made any mistakes implementing the new code or any other mistakes!

Thanks again Ben10!
~Zeb

ZebH
Posts: 99
Joined: Thu May 12, 2016 4:50 pm

Re: Section in Code Repeating as if Button is Always Pressed

Post by ZebH » Thu Aug 08, 2019 4:27 pm

Hi all,

I have followed the Internal pull-up Resistor section of this tutorial http://www.gammon.com.au/switches

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

Thanks for all your help!
~Zeb

Post Reply