Best way to make an LED blink in my code?

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

You can use the ezOutput library to simplify the blink without delay. See an example. This is a much better option

Josephm3502:
Would the best way to do this be to basically put what is in the blink without delay example?

Yes. For only one LED indicator. The code is already written for you. :slight_smile:

you code is awkward

why do you wait in a while() in setup() instead of testing for the condition in loop()?

your logic seems crying to be a state driven: wireGood, wireBroken, alertSent, ... and instead of testing for various conditions, simply call some sub-function depending on state

presumably that final state can wait for millis to exceed some period and toggle an LED

gcjr:
you code is awkward

why do you wait in a while() in setup() instead of testing for the condition in loop()?

your logic seems crying to be a state driven: wireGood, wireBroken, alertSent, ... and instead of testing for various conditions, simply call some sub-function depending on state

presumably that final state can wait for millis to exceed some period and toggle an LED

The code is only meant to run one time. It sits in the setup until the wire is broken, in which then once the wire is broken, it will move down to the loop and make the phone call and stuff.

i understand what your trying to do. it repeatedly waits for the wire to be broken, does something, then repeatedly does nothing or blinks the LED

setup() is for one time initialization and loop() is conventionally where repeated actions are performed. the arduino main() does do other things beside calling loop(). you can of course write your own main()