Limited Looping

My program, below, only prints "New Phrase:" three times. As far as I can tell, it should continue printing this line to the serial port every ten seconds, forever. I will post the code in the next message.

Anyone have any ideas what's going wrong?

  • Ian

/*

int ledInputPin = 13; // choose the pin for the LED
int ledMimicPin = 12; // choose the pin for the LED
int inputPin = 2; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
int beatStart[MAXIMUM_BEATS];
int beatDuration[MAXIMUM_BEATS];
int phraseLengthMillis = 1000 * 10;
int phraseTimestamp = -1;
int currentBeat = 0;
int repeatDelay = 1000 * 5;
boolean playback = false;
boolean beatOn = false;
int pauseStart = -1;

void setup() {
Serial.begin(9600);
pinMode(ledInputPin, OUTPUT); // declare LED as output
pinMode(ledMimicPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
initArray(beatStart, MAXIMUM_BEATS, 0);
initArray(beatDuration, MAXIMUM_BEATS, 0);
}

int initArray(int a[], int arrayLength, int value) {
for(int i = 0; i < arrayLength; i++) {
a = value;

  • }*
  • return arrayLength;*
    }
    void loop(){
  • if (phraseTimestamp < 0)*
  • {*
  • phraseTimestamp = millis();*
  • currentBeat = 0;*
  • }*
  • else if (phraseTimestamp + phraseLengthMillis < millis())*
  • {*
  • phraseTimestamp = millis();*
  • Serial.print("New phrase: ");*
  • Serial.println(phraseTimestamp);*
  • currentBeat = 0;*
  • }*
  • val = digitalRead(inputPin); // read input value*
  • if (val == HIGH) { // check if the input is HIGH*
  • digitalWrite(ledInputPin, LOW); // turn LED OFF*
  • digitalWrite(ledMimicPin, LOW); // turn LED ON*
  • if (beatOn)*
  • {*
  • beatDuration[currentBeat] = millis() - beatStart[currentBeat] - phraseTimestamp;*
  • currentBeat++;*
  • beatOn = false;*
  • }*
  • if (pauseStart <= 0 && !playback)*
  • {*
  • pauseStart = millis();*
  • }*
  • else if (millis() - pauseStart > repeatDelay && !playback)*
  • {*
  • playback = true;*
  • pauseStart = -1;*
  • }*
  • }*
  • else {*
  • digitalWrite(ledInputPin, HIGH); // turn LED ON*
  • digitalWrite(ledMimicPin, HIGH); // turn LED ON*
  • if (playback)*
  • {*
  • initArray(beatStart, MAXIMUM_BEATS, 0);*
  • initArray(beatDuration, MAXIMUM_BEATS, 0);*
  • }*
  • if (!beatOn)*
  • {*
  • beatStart[currentBeat] = millis() - phraseTimestamp;*
  • beatOn = true;*
  • }*
  • playback = false;*
  • pauseStart = -1;*
  • }*
  • if (playback)*
  • {*
  • for (int i = 0; i < MAXIMUM_BEATS; i++)*
  • {*
    _ if (millis() - phraseTimestamp >= beatStart &&_
    millis() - phraseTimestamp < beatStart + beatDuration*)*
    * digitalWrite(ledMimicPin, HIGH); // turn LED ON*
    * else*
    * digitalWrite(ledMimicPin, LOW); // turn LED OFF*
    * }*
    * }*
    }
    [/quote]

I think this:

int phraseTimestamp = -1;

needs to be:

unsigned long phraseTimestamp = -1;

That way, it can hold enough bits to represent actual amount of milliseconds.
An int can hold 32,767 milliseconds (which explains the three prints because 3*10000 = 30000) as opposed to unsigned long that holds 4,294,967,295 milliseconds (enough for ~430000 loops).

You might also concider

const unsigned int phraseLengthMillis = 1000 * 10;
#define phraseLengthMillis 10000

AlphaBeta, you rock. Thanks for the fast reply.

No problem :slight_smile:

While thinking of it, you could scale the millis down resolutionwise to get more loops.

as in:

phraseTimestamp = millis()/1000

now phraseTimestamp contains data with second resolution as opposed to millisecond res. (which as it seems phraseTimestamp does not really need)

Now you can run your program for a good 136 years os so :slight_smile: