I am working on a project where I have a device which when it is tripped, you have 30 seconds to press a button, and if you don’t push it within that allotted time, it will continue the code and call my phone. A delay isn’t going to work because it holds up the loop for 30 seconds, so im thinking that I need to use Millis. I have never used Millis before and need some help getting it into my code.
Here is my code…
#include "Adafruit_FONA.h"
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
// constants won't change:
const int buttonPin = 7 ; // button for auto emergency distress
const int ledPin = 12; // LED for distress will be called
const int recordPin = 11;
unsigned long startMillis;
unsigned long currentMillis;
const unsigned long period = 1000; //the value is a number of milliseconds
// variables will change:
int buttonState = 0; // start button HIGH
char gps_string[ 140 ];
void setup() {
fonaSerial->begin(4800);
fona.begin(*fonaSerial);
fona.enableGPS(true); //enable GPS function
fona.getGPS( 0, gps_string, 140 );
pinMode(ledPin, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input
pinMode(recordPin, OUTPUT);
startMillis = millis(); //initial start time
}
void loop() {
buttonState = digitalRead(buttonPin);
fona.getGPS( 0, gps_string, 140 );
if (buttonState == LOW) {
digitalWrite(ledPin, LOW);
digitalWrite(recordPin, LOW);
fona.hangUp();}
else {
digitalWrite(ledPin, HIGH); //tripped
delay( 5000); //person-response delay
fona.sendSMS("7247147099", gps_string );
delay(5000);
fona.callPhone("7247147099");
delay(10000);
digitalWrite(recordPin, HIGH);
delay(100);
digitalWrite(recordPin, LOW);
delay(15000);
}
}
If you have any questions about that anything is in the code just feel free to ask. I code things by naming them what works best for me…so my apologies. Thanks in advance!
Josephm3502:
I am working on a project where I have a device which when it is tripped, you have 30 seconds to press a button, and if you don't push it within that allotted time, it will continue the code and call my phone.
I do that sort of thing with code like this pseudo code
if device is tripped and tripHappened == false
timeDeviceTripped = millis()
tripHappened = true
read button
if (buttonState == HIGH) { // assumes LOW when pressed
if (millis() - timeDeviceTripped >= 30 seconds) {
// timeout has happened
// make the call
else {
// button is pressed so do whatever