Hi all,
I have a project that when a wire is broken, it lights up an LED and sends an alert to my phone. I want it to blink every second instead of just staying lit the entire time. Would the best way to do this be to basically put what is in the blink without delay example, or is there a better option that I could go with?
Here is my code:
//buttons and lights
const byte wirePin = 5; //external pulldown resistor to take it low when the wire breaks
const byte cancelPin = 6; //wired from pin to ground, uses internal pullup
const byte wireGoodLed = 8;
const byte wireBrokenLed = 7;
const byte alertSentLed = 13;
const byte recordingPin = 9;
//millis stuff
unsigned long wireWasBrokenAt;
long timeOutInterval = 5000; //short value for testing
bool weHaveCancelled = false;
bool alertSent = false;
//GSM stuff
#include "Adafruit_FONA.h"
#define FONA_RX 3
#define FONA_TX 2
#define FONA_RST 4
#include <SoftwareSerial.h>
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
SoftwareSerial *fonaSerial = &fonaSS;
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);
//strings
char gps_string[ 140 ];
void setup()
{
Serial.begin(115200);
Serial.println("setup() started");
fonaSerial->begin(115200);
fona.begin(*fonaSerial);
fona.enableGPS(true);
fona.getGPS( 0, gps_string, 140 );
pinMode(wirePin, INPUT); // you said this is held high by the wire and has a pull down?
pinMode(cancelPin, INPUT_PULLUP); //easier for me to wire this from pin to ground, so we look for a low when pressed
pinMode(wireGoodLed, OUTPUT);
pinMode(wireBrokenLed, OUTPUT);
pinMode(alertSentLed, OUTPUT);
pinMode(recordingPin, OUTPUT);
Serial.println("The wire is good, we are sitting in setup()");
digitalWrite(wireGoodLed, HIGH);
while (digitalRead(wirePin) == HIGH) {} //it sticks here until the wire breaks and resistor pulls pin low
Serial.print("The wire was broken at ");
wireWasBrokenAt = millis();
Serial.print(wireWasBrokenAt);
Serial.println(", we are leaving setup()and heading to loop()");
delay(500); //just to see message
digitalWrite(wireGoodLed, LOW);
digitalWrite(wireBrokenLed, HIGH);
}
void loop(){
fona.getGPS(0, gps_string, 120);
Serial.print(gps_string);
if (!weHaveCancelled && millis() - wireWasBrokenAt < timeOutInterval) //haven't cancelled, and still in time
{
Serial.println("Waiting for cancel or timeout");
if (!digitalRead(cancelPin)) // ! means not, so this is looking for a low, ie a press
{
Serial.println("Cancel was pressed");
delay(500); //just to see message
weHaveCancelled = true;
}
}
if (!weHaveCancelled && !alertSent && millis() - wireWasBrokenAt >= timeOutInterval) //haven't cancelled, out of time, and not alrted before
{
digitalWrite(alertSentLed, HIGH);
delay(2500);
fona.sendSMS("5555555555", "wire broke!");
delay(2500);
fona.sendSMS("5555555555", gps_string);
delay(2500);
fona.callPhone("5555555555");
delay(5000);
digitalWrite(recordingPin, HIGH);
delay(15000);
fona.hangUp();
alertSent = true;
Serial.print("Time up: alert sent at ");
Serial.print(millis());
delay(500); //just to see message
}
if (weHaveCancelled || alertSent) Serial.println("To prove the code didn't block");
}//loop
Thank you,
Joe M