Millis Help

Hello,

I'm trying to sync led flashes with an audio routine. As you can see there are lot of delays used to turn the led's on and off. My question is how would I convert these to millis? Thanks.

#include "SoftwareSerial.h"
#include "DFRobotDFPlayerMini.h"
#include <PololuMaestro.h>

//Pin where positive wire of LED connects.
int led1=3; //Gun
int led2=7; //shield
int led3=2; //Ground Explosion

//Arduino: 5v to 5v, Gnd to Gnd
//MP3 Module: Vcc to 5v, Gnd to Gnd, TX to pin 7, RX to pin 8 

SoftwareSerial mySoftwareSerial(7,8); //RX-8,TX-7 (Optional: Add resistor (100 Ohm) from Rx mp3 to pin 11) 
DFRobotDFPlayerMini myDFPlayer;
void printDetail(uint8_t type, int value);

#define SENSORPIN 9
#define PAUSETME 20000

//Connect Maestro pin RX to Pin 11 of Ardunio 
//Conect Maestro pin TX to Pin 10 of Arduino
//Conect Maestro GND pin to GND of Arduino 
//In this case give Masetro it's own 6V power supply
#ifdef SERIAL_PORT_HARDWARE_OPEN
  #define maestroSerial SERIAL_PORT_HARDWARE_OPEN
#else
  #include <SoftwareSerial.h>
  SoftwareSerial maestroSerial(10, 11);
#endif

MicroMaestro maestro(maestroSerial);
 
void setup() 
{
  //Define LED pin as output
  pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  
  maestroSerial.begin(9600);
   
  mySoftwareSerial.begin(9600);
  Serial.begin(9600);
  pinMode(SENSORPIN, INPUT);

  Serial.println();
  Serial.println(F("Initializing DFPlayer..."));

  //Use softwareSerial to communicate with MP3
  if (!myDFPlayer.begin(mySoftwareSerial)) {
    Serial.println(F("Unable to begin:"));
     Serial.println(F("1.Please recheck connections!"));
      Serial.println(F("2.Please insert the SD card!"));
      while(true);
  }
      Serial.println(F("DFPlayer Mini online"));

      //Set volume from 0-30
      myDFPlayer.volume(25);

}

void loop ()
 {
  
  int pirSensor=digitalRead(SENSORPIN);
  if(pirSensor==HIGH)

  {
     Serial.println(F("Sensor Activated"));
     Serial.println(F("DFPlayer Working..."));
     myDFPlayer.play(1);
      maestro.restartScript(02);
  delay(4000);

     delay(47000); //Delay for intro (47sec)
     digitalWrite(led1,HIGH); //Turns on Gun LED 
     delay(300);
     digitalWrite(led1,LOW); //Turns off Gun LED
     delay(700);
     digitalWrite(led1,HIGH); //Turns on Gun LED 
      delay(300);
     digitalWrite(led1,LOW); //Turns off Gun LED
     delay(700);
     digitalWrite(led1,HIGH); //Turns on Gun LED
     delay(300);
     digitalWrite(led1,LOW); //Turns off LED connected to pin 4
     delay(700);
      digitalWrite(led3,HIGH); //Ground Expolsion 
     delay(300);
       digitalWrite(led3,LOW); //Ground Explosion 
     delay(200);
     digitalWrite(led3,HIGH); //Ground Expolsion 
     delay(300);
     digitalWrite(led3,LOW); //Ground Expolsion 
     delay(200);
     

     delay(180000); //adds a 3 min delay-lockout 
    
   }
     
}

Unless you've got some other use for the wasted machine cycles, and the code works as you want, why bother?

“ My question is how would I convert these to millis? ”

30 posts and you still use delay() :o

Sounds like you know about using millis() to create non blocking delays.

What do you not understand about the technique ?


Have you looked at Robin2’s discussion ?

https://forum.arduino.cc/index.php?topic=223286.0


Have you looked at State Machines ?

https://forum.arduino.cc/index.php?topic=505805.0

https://forum.arduino.cc/index.php?topic=525240.0

If I add the other 2 led's (led 2 and led3) the code doesn't work. But if I have it with just led1 it works (but only with one led) I'm thinking delay is causing the other 2 leds to not work when i define them in the code. that's why I want to see if millis will make a difference.

pinMode(led1, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
pinMode(led1, OUTPUT);

The links offered to you in post #2 describe how you write non blocking code.

It's very unlikely that delay is affected simply by the presence of extra LEDs, or vice versa

I really like a Finite State Machine, but in this situation I would read the data from a table: millis_rhythm.ino.

Nevermind, two of my wires were flip flopped
The code works with delays now

But I will look into millis for future references.

Although, I have one more question.
How can I get the gun LED and the Ground Explosion LED to go at the same time while keeping there on/off (blink) duration. (This is important because its a timed routine).
At around the 50 sec mark of my routine I would like to have both the Gun LED and Explosion LED go on togther.

digitalWrite(led1,HIGH); //Turns on Gun LED
     delay(300);
     digitalWrite(led1,LOW); //Turns off Gun LED
     delay(700);//48 sec
     digitalWrite(led1,HIGH); //Turns on Gun LED 
      delay(300);
     digitalWrite(led1,LOW); //Turns off Gun LED
     delay(700);//49sec
     digitalWrite(led1,HIGH); //Turns on Gun LED
     delay(300);
     digitalWrite(led1,LOW); //Turns off Gun LED
     delay(700);//50sec
     digitalWrite(led1,HIGH); // Gun LED
     delay(300);
     digitalWrite(led1,LOW); //Gun LED
     delay(700);//51sec
     digitalWrite(led1,HIGH); // Gun LED
     delay(300);
     digitalWrite(led1,LOW); //Gun LED
     delay(700);//52sec
      
      digitalWrite(led3,HIGH); //Ground Expolsion 
     delay(300);
       digitalWrite(led3,LOW); //Ground Explosion 
     delay(200);
     digitalWrite(led3,HIGH); //Ground Expolsion 
     delay(300);
     digitalWrite(led3,LOW); //Ground Expolsion 
     delay(200);//53sec
     digitalWrite(led3,HIGH); //Ground Expolsion 
     delay(300);
       digitalWrite(led3,LOW); //Ground Explosion 
     delay(200);
     digitalWrite(led3,HIGH); //Ground Expolsion 
     delay(300);
     digitalWrite(led3,LOW); //Ground Expolsion 
     delay(200);//54sec

If only the gun and ground explosion LEDs had their own unique identifiers, obvious to outsiders
{Sigh}

delay(700);//48 sec Nope.

You really should stop and review how State Machine programming works.

When you understand the SM process come back to your current project.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.