I just got my adafruit FONA to work properly with a button to call a number when pushed. yay. Now that is all fine and dandy, but I would like to code in another button that whenever you push the first button to call it starts a 15 second timer then after the 15 seconds it calls the number I coded it to call. What I would like the second button to do is once the first button is pressed, during those 15 seconds if you push the second button it will cancel the timer and not call. Is this possible? I know I didn't explain it well so if you have any more questions to clear this up don't be afraid to ask.
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 buttonPin1 = 5; // button for auto emergency distress
const int buttonPin2 = 6; // button for cancel distress
const int ledPin1 = 12; // LED for distress will be called
const int ledPin2 = 13; // LED for distress cancelled
// variables will change:
int buttonState1 = 1; // start button HIGH
int buttonState2 = 0; // start button LOW
void setup() {
fonaSerial->begin(4800);
fona.begin(*fonaSerial);
pinMode(ledPin1, OUTPUT); // initialize the LED pin as an output
pinMode(ledPin2, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input
pinMode(buttonPin2, INPUT); //initialize the pushbutton as an input
}
void loop() {
// read the state of the pushbutton value:
buttonState1 = digitalRead(buttonPin1);
// check if the pushbutton is pressed. If it is, the buttonState is HIGH:
if (buttonState1 == LOW) {
digitalWrite(ledPin1, HIGH);
delay(15000);
fona.callPhone("5555555555"); }
else {
// turn LED off:
digitalWrite(ledPin1, LOW);}
}
Thanks in advance.
Im sorry I just realized I did not put my code in there...my bad. I have updated my original post with my code right now.
so if I use the millis function then I would be able to easily cancel what happens after I push the first button?
Im sorry...I am just new to this and needed help. I appreciate you helping me but I don't think there is a need to be that way towards me. I believe you but I'm just clarifying to help me understand all of this. I am 16 and trying to get into these kind of things but when someone says that kind of things to the wrong kid then you could drive him or her out of the hobby. Think positive my friend.
you didnt hurt my feelings, Im just vouching for other people. Anyways...Ive been researching this and it seems that the millis function runs almost like its in the background while the other functions run. But my question is that do I just put the
if(currentMillis - previousMillis > interval) {
// save the last time you blinked the LED
previousMillis = currentMillis;
in front of the code if else statements with the calls? I have already stated the long interval and the long previous millis above before the void setup.
Consider the use of a state machine.
Compiles but not tested. YMMV...
#include "Adafruit_FONA.h"
#include <SoftwareSerial.h>
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
#define CALL_DELAY_TIME 15000U //15-sec delay
#define STCALL_WAITB1 0
#define STCALL_WAIT15S 1
#define STCALL_MAKECALL 2
#define STCALL_RESET 3
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
// constants won't change:
const int buttonPin1 = 5; // button for auto emergency distress
const int buttonPin2 = 6; // button for cancel distress
const int ledPin1 = 12; // LED for distress will be called
const int ledPin2 = 13; // LED for distress cancelled
// variables will change:
int
buttonState1,
buttonState2;
void setup()
{
fonaSerial->begin(4800);
fona.begin(*fonaSerial);
pinMode(ledPin1, OUTPUT); // initialize the LED pin as an output
pinMode(ledPin2, OUTPUT); // initialize the LED pin as an output
pinMode(buttonPin1, INPUT); // initialize the pushbutton pin as an input
pinMode(buttonPin2, INPUT); //initialize the pushbutton as an input
}//setup
void loop()
{
CallStateMachine();
}//loop
int ReadButton( int pinButton )
{
int
butState;
//a crude debounce to ensure a valid button press
butState = digitalRead( pinButton );
delay(10);
if( butState == LOW )
{
if( digitalRead( pinButton ) == LOW )
return LOW;
else
return -1;
}//if
else
{
if( digitalRead( pinButton ) == HIGH )
return HIGH;
else
return -1;
}//else
}//ReadButton
void CallStateMachine( void )
{
static byte
stateCall = STCALL_WAITB1;
static unsigned long
timeCall;
unsigned long
timeNow;
switch( stateCall )
{
case STCALL_WAITB1:
//waiting for button 1 to be pushed
if( ReadButton(buttonPin1) == LOW )
{
//when pressed
// - set a timer for the 15-sec delay
// - turn on the LED
// - move to the next state
stateCall = STCALL_WAIT15S;
timeCall = millis();
digitalWrite(ledPin1, HIGH);
}//if
break;
case STCALL_WAIT15S:
//Here was wait for either
// - the 15-sec delay to expire; if so,
// we go forward and make the call
// - button 2 to be pressed; if so,
// we cancel the timer by moving out
// to the reset state
if( (millis() - timeCall) >= CALL_DELAY_TIME )
stateCall = STCALL_MAKECALL;
else
{
if( ReadButton(buttonPin2) == LOW )
{
//turn off the LED, then go to reset
digitalWrite(ledPin1, LOW);
stateCall = STCALL_RESET;
}//if
}//else
break;
case STCALL_MAKECALL:
//make the call, turn off the LED etc
fona.callPhone("5555555555");
digitalWrite(ledPin1, LOW);
//whatever is required to finish here...
stateCall = STCALL_RESET;
break;
case STCALL_RESET:
//here we just make sure both buttons are
//released (read 'HIGH') before going back
//and looking for button 1 to be pressed
if( (ReadButton(buttonPin1) == HIGH) &&
(ReadButton(buttonPin2) == HIGH) )
stateCall = STCALL_WAITB1;
break;
default:
//if we somehow end up here (undefined state),
//just go back to reset
stateCall = STCALL_RESET;
break;
}//switch
}//CallStateMachine