Need some guidance swiching from delay to milli() - Here's my code.

I'm in need of some guidance.

I'm building the an Arduino based Cyclone Arcade game with a twist.
Using a 2560. LEDs are pins 2-42. LEDs chase each other based on dt, Delay Time.

Right now I would just like some help switching from delay to mill. But maybe there is a better way.

Once I get the mili figured out, here's what I'm ultimately trying to do.

Would appreciate any assistance.

Pin 21 has a different delay variable delay time called Win.

I'm trying to modify my code from delay () to milli(). I'm stuck.

Ultimately I need to add the button to make it a game. That's pbA.

Logic
When pgA = high && pin 21 = HIGH goto/jump/break to WIN routine.

Win routine has not been added. But it's essentially chase LEDs 21, then 20 & 22, 19 & 21, 18 & 22.....

If pbB pressed and LED 21 is low or not high goto/jump/break to loose routine.

(Not sure what loose routine will be, but it needs to be waiting for pbB input.

There's a second pushbutton, pbB. This button when pressed once resets Arduino and starts the game all over again.

If double pressed will win variable is set to lower 8 and starts game.

If long press win variable is set to 100 and starts game.

Original code

  //  Cyclone Game 
byte PBA = 0 ;
byte PBB = 0 ;
byte DT = 20 ;
int WIN =  8;


void setup()  {
  // Iinitialize pins 2 - 43 as an output:
  for (byte thisPin = 2; thisPin < 43; thisPin++) {
    pinMode(thisPin, OUTPUT);
  }
  pinMode (44, INPUT)   ;
  pinMode (45, INPUT)   ;
}

void loop()  {
  // put your main code here, to run repeatedly:
  PBA = digitalRead (44) ;
  PBB = digitalRead (45) ;

  digitalWrite (21, PBA) ;
  digitalWrite (22, PBB) ;

  digitalWrite (42, HIGH)  ;

  digitalWrite (2,  HIGH)  ;
  delay (DT);
  digitalWrite (2,  LOW)  ;
  digitalWrite (3,  HIGH)  ;
  delay (DT);
  digitalWrite (3,  LOW)  ;
  digitalWrite (4,  HIGH)  ;
  delay (DT);
  digitalWrite (4,  LOW)  ;
  digitalWrite (5,  HIGH)  ;
  delay (DT);
  digitalWrite (5,  LOW)  ;
  digitalWrite (6,  HIGH)  ;
  delay (DT);
  digitalWrite (6,  LOW)  ;
  digitalWrite (7,  HIGH)  ;
  delay (DT);
  digitalWrite (7,  LOW)  ;
  digitalWrite (8,  HIGH)  ;
  delay (DT);
  digitalWrite (8,  LOW)  ;
  digitalWrite (9,  HIGH)  ;
  delay (DT);
  digitalWrite (9,  LOW)  ;
  digitalWrite (10, HIGH)  ;
  delay (DT);
  digitalWrite (10,  LOW)  ;
  digitalWrite (11, HIGH)  ;
  delay (DT);
  digitalWrite (11,  LOW)  ;
  digitalWrite (12, HIGH)  ;
  delay (DT);
  digitalWrite (12,  LOW)  ;
  digitalWrite (13, HIGH)  ;
  delay (DT);
  digitalWrite (13,  LOW)  ;
  digitalWrite (14, HIGH)  ;
  delay (DT);
  digitalWrite (14,  LOW)  ;
  digitalWrite (15, HIGH)  ;
  delay (DT);
  digitalWrite (15,  LOW)  ;
  digitalWrite (16, HIGH)  ;
  delay (DT);
  digitalWrite (16,  LOW)  ;
  digitalWrite (17, HIGH)  ;
  delay (DT);
  digitalWrite (17,  LOW)  ;
  digitalWrite (18, HIGH)  ;
  delay (DT);
  digitalWrite (18,  LOW)  ;
  digitalWrite (19, HIGH)  ;
  delay (DT);
  digitalWrite (19,  LOW)  ;
  digitalWrite (20, HIGH)  ;
  delay (DT);
  digitalWrite (20, LOW)  ;
  digitalWrite (21, HIGH)  ;
  delay (WIN);  //<-----------------Note this delay time can be modified.  Win variable. 
  digitalWrite (21, LOW)  ;
  digitalWrite (22, HIGH)  ;
  delay (DT);
  digitalWrite (22, LOW)  ;
  digitalWrite (23, HIGH)  ;
  delay (DT);
  digitalWrite (23, LOW)  ;
  digitalWrite (24, HIGH)  ;
  delay (DT);
  digitalWrite (24, LOW)  ;
  digitalWrite (25, HIGH)  ;
  delay (DT);
  digitalWrite (25, LOW)  ;
  digitalWrite (26, HIGH)  ;
  delay (DT);
  digitalWrite (26, LOW)  ;
  digitalWrite (27, HIGH)  ;
  delay (DT);
  digitalWrite (27, LOW)  ;
  digitalWrite (28, HIGH)  ;
  delay (DT);
  digitalWrite (28, LOW)  ;
  digitalWrite (29, HIGH)  ;
  delay (DT);
  digitalWrite (29, LOW)  ;
  digitalWrite (30, HIGH)  ;
  delay (DT);
  digitalWrite (30, LOW)  ;
  digitalWrite (31, HIGH)  ;
  delay (DT);
  digitalWrite (31, LOW)  ;
  digitalWrite (32, HIGH)  ;
  delay (DT);
  digitalWrite (32, LOW)  ;
  digitalWrite (33, HIGH)  ;
  delay (DT);
  digitalWrite (33, LOW)  ;
  digitalWrite (34, HIGH)  ;
  delay (DT);
  digitalWrite (34, LOW)  ;
  digitalWrite (35, HIGH) ;
  delay (DT);
  digitalWrite (35, LOW)  ;
  digitalWrite (36, HIGH) ;
  delay (DT);
  digitalWrite (36, LOW)  ;
  digitalWrite (37, HIGH) ;
  delay (DT);
  digitalWrite (37, LOW)  ;
  digitalWrite (38, HIGH)  ;
  delay (DT);
  digitalWrite (38, LOW)  ;
  digitalWrite (39, HIGH)  ;
  delay (DT);
  digitalWrite (39, LOW)  ;
  digitalWrite (40, HIGH)  ;
  delay (DT);
  digitalWrite (40, LOW)  ;
  digitalWrite (41, HIGH)  ;
  delay (DT);
  digitalWrite (41, LOW)  ;


}

Updated code

//  Cyclone Game
byte pbA = 0;
byte pbB = 0;

//Time varibles 
unsigned long startTime;  //Initial start time.
unsigned long currentTime;
unsigned long dt = 40;  //delay time <------- Change this value for faster/slower chase.


//============= SETUP =====================================================
void setup()  {

 startTime = millis();  // Set startTime to Arduino clock. 
  
  // Iinitialize pins 2 - 43 as an outputs:
  for (byte LEDS = 2; LEDS < 43; LEDS++) {
    pinMode(LEDS, OUTPUT);
  }
  pinMode (44, INPUT);
  pinMode (45, INPUT);

  digitalWrite (42, HIGH);  // Turn on Push buttonA (pbA) LED on
}

//============ LOOP =======================================================
void loop()  {
  // put your main code here, to run repeatedly:


  for (byte LED = 2; LED < 42; LED++) {   //chase LEDs 2-41
    digitalWrite (LED, HIGH);
    delay (dt);
    digitalWrite (LED, LOW);
  }


  pbA = digitalRead (44);
  pbB = digitalRead (45);
  digitalWrite (21, pbA);
  digitalWrite (22, pbB);

}

Ok, well let's start by putting all of the pin numbers into an array. While you currently have all your LEDs on pin 2-41, there is going to be a reason in the future which will force you to change some pin numbers around. Maybe you need to add an SPI device, which will use some of the pins out of the middle of that range or it vastly simplifies the wiring to put the second half of the LEDs on in reverse order.

const int NumLEDs = 40; //must match the length of the array below
const byte pinsLED[NumLEDs] = {2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41};

void setup(){
  for(int i=0; i<NumLEDs; i++) {
    //make each LED pin an output
    pinMode(pinsLED[i], OUTPUT);
  }
}

So you can already see with the little snippet of setup() that it's going to be easy to run through the list of LEDs and do things with that list.

So the machine needs to have 'states', that is, it needs to have internal memory that remembers where it is up to. If you know where you are then it's easy to know where to go.

While it is running you need to know which LED is on, so you know which LED to turn on next. You also need to know how long it was on, so you move to the next one at the right time.

I think there are several high-level states the machine can be in. "Attract mode" which flashes all the lights to encourage someone to start the game, "Running", "Winning", maybe there's a "Losing" mode too. Do those later. First concentrate on making it run.

Does this give you enough ideas to move forwards?

Appreciate the assistance but not really helping me. Your array example is using the a For loop which is what I am already using.

I'm trying to figure out the delay to milli() so I can add the pushbutton == code.

I'm stuck trying to figure out how to replace delay with millis.

Do use a nested loop?

Set LED to high, enter a for loop doing the clock math and testing to see if the PB has been pressed. Once time has expired nested loop ends. Set LED to low, increment to next LED, set to high and enter the clock loop.

I'm wondering if instead of the nesting a for loop maybe I should use a while loop?

for (byte LED = 2; LED < 42; LED++) { //chase LEDs 2-41
digitalWrite (LED, HIGH);
delay (dt);
digitalWrite (LED, LOW);
}

Doug101:
I'm stuck trying to figure out how to replace delay with millis.

[.....]

for (byte LED = 2; LED < 42; LED++) { //chase LEDs 2-41
digitalWrite (LED, HIGH);
delay (dt);
digitalWrite (LED, LOW);
}

Don't use either FOR or WHILE because both block the Arduino until they complete. Let loop() do the iteration and use variables and IF tests to keep track of where you are. Something like

if (ledNum <= 42) {
   if (millis() - ledONmillis() >= dt) {
      digitalWrite(ledNum, LOW);
      ledNum ++;
    }
    else {
      digitalWrite(ledNum, HIGH);
      ledONmillis = millis();
    }
}

This is just to get you thinking. You may need to extend it to have an OFF interval.

...R

I'm stuck trying to figure out how to replace delay with millis.

See if this helps: Duino-hacks/delay_without_delay/delay_without_delay.ino at master · WestfW/Duino-hacks · GitHub

basically, it lets you write code that looks like:

  if (delay_without_delaying(atime, 100)) {
     // thing to do every 100ms
  }
  if (delay_without_delaying(ctime, 30)) {
    // thing to do every 30ms
  }
  // other things to do, perhaps all the time.
}

Here's the important part...

MorganS:
So the machine needs to have 'states', that is, it needs to have internal memory that remembers where it is up to. If you know where you are then it's easy to know where to go.

While it is running you need to know which LED is on, so you know which LED to turn on next. You also need to know how long it was on, so you move to the next one at the right time.

westfw:
See if this helps: Duino-hacks/delay_without_delay/delay_without_delay.ino at master · WestfW/Duino-hacks · GitHub

basically, it lets you write code that looks like:

  if (delay_without_delaying(atime, 100)) {

// thing to do every 100ms
  }
  if (delay_without_delaying(ctime, 30)) {
    // thing to do every 30ms
  }
  // other things to do, perhaps all the time.
}

Thanks for helpful