Programming a running track system with millis

Hi,

For a school project, I need to make a running track system. It consists out of IR sensors that monitor a certain interval inbetween them, this interval is divided over a led strip in the next part of the track…

The interval timer is working great, the only problem I’m experiencing now is with the millis function and the led strip. I want it to move like in the FastLed Library as the Firstlight sketch. But it doesn’t do anything or runs really fast.

Could anyone help me on how to counter this problem I’m experiencing?

The code:

// reads two IR proximity/line-detectors.  reports time between triggers.
// uses interrupts.  First one should go high for detect on 2
// second detector should go high on detect on pin 3

#include "FastLED.h"

// How many leds are in the strip?
#define NUM_LEDS 60

// Data pin that led data will be written out over
#define DATA_PIN 8

int whiteLed;

// This is an array of leds.  One item for each led in your strip.
CRGB leds[NUM_LEDS];



int Reset;

unsigned long newTimer1;
unsigned long newTimer2;

unsigned long t1=0;
unsigned long t2=0;
void sens1() { if ((t1==0) && (t2==0)) t1=micros(); }
void sens2() { if ((t2==0) && (t1!=0)) t2=micros(); }

unsigned long Interval;
unsigned long Interval2;

uint8_t minutes, seconds, hundreds;

void setup() {
  pinMode(2,INPUT);// IR sensor 1
  pinMode(3,INPUT); // IR sensor 2
  pinMode(13,OUTPUT); // Control led
    
  attachInterrupt(0,sens1,RISING);
  attachInterrupt(1,sens2,RISING);
  Serial.begin(115200);
  //Serial.begin(57600);

  FastLED.addLeds<WS2812B, DATA_PIN, RGB>(leds, NUM_LEDS);
}

void loop() {
  digitalWrite(13,t1?HIGH:LOW);
  if (t2>0)
    {

      minutes = ((t2-t1)/60000000);
      seconds = ((t2-t1)/1000000);
      hundreds = ((t2-t1)/10000);
      
      Serial.print("minutes :");
      Serial.print(minutes );
      Serial.print("\t" );
      
      Serial.print("seconds :");
      Serial.print(seconds);
      Serial.print("\t" );
      
      Serial.print("hundreds :");
      Serial.print(hundreds);
      Serial.print("\t" );

      Serial.println(" ");

      Interval = (t2-t1)/NUM_LEDS;               
      Reset = 0;
      newTimer1 = millis();                   
      int i=0;
      while(Reset == 0){
        newTimer2 = millis ();              
        Interval2 = newTimer2 - newTimer1;    

           
           
        while (Interval < (Interval2/60)+ (i*(Interval2/60))){                  
            
            for( whiteLed = 0; whiteLed < NUM_LEDS; whiteLed = whiteLed + 1) {
            // Turn our current led on to white, then show the leds
            leds[whiteLed] = CRGB::White;

            // Show the leds (only one of which is set to white, from above)
            FastLED.show();

            // Turn our current led back to black for the next loop around
            leds[whiteLed] = CRGB::Black;
            i++;
            Reset = 1;
            
            }
      
        }
     
      }
      
      t1=t2=0;
      Interval2=Interval=0;
      newTimer2=newTimer1=0;
    }
  }
  

//This version may be safer inside an interrupt:
static unsigned long myMicros() {
   extern volatile unsigned long timer0_overflow_count;
   uint8_t oldSREG = SREG;      
   cli();      
   uint32_t t = TCNT0;
   if ((TIFR0 & _BV(TOV0)) && (t == 0))
       t = 256;
   uint32_t m = timer0_overflow_count;
   SREG = oldSREG;
   return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
}
Interval = (t2-t1)/NUM_LEDS;

Good. Now use that interval to control the leds. Have you heard about the delayMicroseconds() function? It is rarely used but I think it is going to work for you in this case.

You are calculating seconds and hundreds incorrectly. You need to store the t2-t1 value in a variable, and decrement that variable the correct number of milliseconds in the minutes value, to get the remaining time in milliseconds, before calculating seconds. You need to do the same before calculating hundreds.

Once the while (Interval < (Interval2/60)+ (i*(Interval2/60))) loop starts, Interval and Interval2 never change, so the loop will never end.

Your code DEFINITELY needs comments that explain why you are doing what you are doing.