Millis Timer, Stopped Working?? HELP please :)

Hey guys, for some reason my millis code has suddenly stopped working, in the sense that i can not
control the start of it anymore, it runs and executes the if statement on startup.

Below is a section of the code to trigger the timer, and the millis statement itself.

void loop()
{
if (PinTwoActive && digitalRead(PinTwo))

{
PinThreeActive = false;
PinTwoActive = false;
PinNineActive = false;
PinTenActive = false;
digitalWrite(PinSix, LOW);
digitalWrite(PinEleven, LOW);
digitalWrite(PinTwelve, LOW);

mp3.play_usb_disk(0x0001);
startTime = millis();
}

if (millis() - startTime >= intervalB)
{
digitalWrite(PinFive, HIGH);
digitalWrite(PinSix, HIGH);
digitalWrite(PinEleven, HIGH);
digitalWrite(PinTwelve, HIGH);
PinTwoActive = true;
PinThreeActive = true;
PinNineActive = true;
PinTenActive = true;
}

You need to describe the problem more fully and post all of your code.

#include <SoftwareSerial.h>
#include <MP3.h>
#include <SPI.h>

const int p1i = 24;
const int p2i = 25;
const int p3i = 26;
const int p4i = 27;
const int p5i = 28;
const int p6i = 29;
const int p7i = 30;
const int p8i = 31;
const int p1o = 33;
const int p2o = 34;
const int p3o = 35;
const int p4o = 36;
const int p5o = 37;
const int p6o = 38;
const int p7o = 39;
const int p8o = 40;
const int reset = 32;
const int buzz = 41;
const int bell = 42;

unsigned long intervalA = 500;
unsigned long intervalB = 500;
unsigned long intervalC = 1000;
unsigned long intervalD = 3000;
unsigned long startTime;
static bool p1iActive;
static bool p2iActive;
static bool p3iActive;
static bool p4iActive;
static bool p5iActive;
static bool p6iActive;
static bool p7iActive;
static bool p8iActive;

MP3 mp3;

void setup()
{

pinMode(p1i, INPUT);// player 1 button

pinMode(p2i, INPUT);// player 2 button

pinMode(p3i, INPUT);// player 3 button

pinMode(p4i, INPUT);// player 4 button

pinMode(p5i, INPUT);// player 5 button

pinMode(p6i, INPUT);// player 6 button

pinMode(p7i, INPUT);// player 7 button

pinMode(p8i, INPUT);// player 8 button

pinMode(reset, INPUT);// master reset

pinMode(p1o, OUTPUT);// player 1 light

pinMode(p2o, OUTPUT);// player 2 light

pinMode(p3o, OUTPUT);// player 3 light

pinMode(p4o, OUTPUT);// player 4 light

pinMode(p5o, OUTPUT);// player 5 light

pinMode(p6o, OUTPUT);// player 6 light

pinMode(p7o, OUTPUT);// player 7 light

pinMode(p8o, OUTPUT);// player 8 light

pinMode(buzz, OUTPUT);// players 1-4 buzzer

pinMode(bell, OUTPUT);// players 5-8 bell

digitalWrite(p1o, HIGH);
digitalWrite(p2o, HIGH);
digitalWrite(p3o, HIGH);
digitalWrite(p4o, HIGH);
digitalWrite(p5o, HIGH);
digitalWrite(p6o, HIGH);
digitalWrite(p7o, HIGH);
digitalWrite(p8o, HIGH);

p1iActive = true;
p2iActive = true;
p3iActive = true;
p4iActive = true;
p5iActive = true;
p6iActive = true;
p7iActive = true;
p8iActive = true;

mp3.begin(MP3_SOFTWARE_SERIAL); // select software serial

mp3.volume(0x1F);

mp3.set_mode(MP3::SINGLE);

}

void loop()
{
if (p1iActive && digitalRead(p1i))

{
p1iActive = false;
p2iActive = false;
p3iActive = false;
p4iActive = false;
p5iActive = false;
p6iActive = false;
p7iActive = false;
p8iActive = false;
digitalWrite(p2o, LOW);
digitalWrite(p3o, LOW);
digitalWrite(p4o, LOW);
digitalWrite(p5o, LOW);
digitalWrite(p6o, LOW);
digitalWrite(p7o, LOW);
digitalWrite(p8o, LOW);
startTime = millis();

}
{
if (millis() - startTime >= intervalD)

mp3.play_usb_disk(0x0001);
}

}

I don't want to appear picky, but the code in your original post is not in the code in your reply #2. It would also help considerably if you used code tags (the </> icon) around your code and auto formatted it before posting it.

yeah sorry that was me trying to make it smaller so you didnt have to go through everything :slight_smile:

any ideas on the millis???

;0

To be clear. Does the problem occur with the code in reply #2 ?
Can you please do as I suggest and edit the post and add code tags.

This line will only ever equate to true and get in the loop once, is this intentional?

if (p1iActive && digitalRead(p1i))

NZarduino:
const int p1i = 24;
const int p2i = 25;
const int p3i = 26;
const int p4i = 27;
const int p5i = 28;
const int p6i = 29;
const int p7i = 30;
const int p8i = 31;
const int p1o = 33;
const int p2o = 34;
const int p3o = 35;
const int p4o = 36;
const int p5o = 37;
const int p6o = 38;
const int p7o = 39;
const int p8o = 40;

What about using "data structures" and later on "algorithms" in your program?

Perhaps a data structure like putting the pin numbers into arrays:

const byte pi[]={24, 25, 26, 27, 28, 29, 30, 31};
#define NUMPLAYERS sizeof(pi)
const byte po[]={33, 34, 35, 36, 37, 38, 39, 40};

Then you possibly use some "algorithms" in your code to so something like initializing all player pins like:

for (int i=0;i<NUMPLAYERS;i++)
{
  pinMode(pi[i],INPUT);
  pinMode(po[i],OUTPUT);
}

Your code looks totally messy and error-prone.
Not structured in any way. Many redundant code, not using functions and loops where functions and loops would be useful to use.

When millis() is used as a software timer (for example 10Hz), then it is checked everytime in the loop().
When millis() is used as a delay, there must a flag to indicate that the delay is active.

The delay is not started by setting startTime to millis(), because the if-statement of millis()-startTime will always result in an increasing value, since millis() increases and startTime will be zero when the sketch is started.

You need a global flag, and only check millis() if that flag is active.
For example : boolean NZarduinosFlagToIndicateThatTheDelayIsActiveAtTheMoment = false;
Or something shorter :wink:

Set the flag true when the delay is started, and make it false when millis() has reached its time.

millis() not counting could be a library has hijacked the timer used to count milliseconds, your code is being called from an interrupt routine or interrupts are disabled in the main code.