Help with timings

So, I’m building an 8-step sequencer using the Arduino IDE, and I got to the part where I configure period duration using a pot. And it does work, but it doesn’t work the way I expect. I’ve been looking at my code for hours, and I just can’t figure out what went wrong, so I was hoping that someone here could give me a hand :frowning:

Here’s the relevant code, from the “loop” section:

  // Playing speed regulation, using a pot:
  int speedPot = analogRead(POT1); // This pot determines T duration
  float T_step = 0.89; // [ms], T_step=(1000ms-100ms)/1024
  float T = 100 + speedPot * T_step; // [ms], T= t_ON + t_OFF, length of one period 


  if (millis() - T_timer < T) { // This is the step scope
   digitalWrite(LED[stepCounter], HIGH);
  } else { // time T passed, time to move on
    T_timer = millis();
    digitalWrite(LED[stepCounter], LOW);
    if (stepCounter == 7)
      stepCounter = 0;
    else
      stepCounter++;
  }

So, the first part is determining the T duration. I wanted it to go from 0.1-1 s, or, 100+[0-1023]*0.89 [ms], roughly.

My sequencer has 8 LEDs, and in the second part I wanted to test the setup by turning each LED for a period T, and then jumping to the next period/LED, where T is determined by the pot position. And it does that, but instead of 0.1-1s, I get around 2-20s. What did I do wrong?

I declared timers as type “long”, but for T I used the type “float”. Is that a problem, can those two be in an equation?

I will attach the whole code if needed, but it’s still under construction, so it’s very messy.

I declared timers as type “long”,

They should really be unsigned long to match the value returned by millis() and to avoid possible problems with negative values.

I suspect the line
T_timer = millis();
should be part of the IF and not part of the ELSE.
Just realized your code is the opposite to the way I do it.

Also note that
T_timer += T;
is more accurate as it avoids any accumulation of errors. This is discussed at great length in Several Things at a Time

…R

hi

i’m trying to do the same but on a big scale with SD card ( i also have a example with Arrays on Sram)
i can controll 512 outputs with Fade and scene time + tapsync.
not yet made a function to controll scenetime with relevant fade time with pot controll
i did made a pot master dimmer.
and its made for the DUE so you need to decrease the arrays size to fit it on a uno or mega.

take a look at my example maby it will help you.

SequencerSD.zip (2.84 KB)

What board are you using? It's not by any chance a ATmega on a breadboard that hasn't been bootloaded, is it? (they come set to run at 1mhz off internal osc, so if you're compiling sketch for 16mhz board, things will run at 1/16th of the speed you expect)

@UKHelyBob, Robin 2: Thank you, I will try those things now, and see what happens.

@spirit: My setup is much more modest, but I will take a look at yours.

@DrAzzy: My chinese seller claimed that these chips were bootloaded for Arduino Uno, so I assume they are running on 16MHz. THIS is my setup, I'm not using the Arduino board, but the USBAsp programmer, so I assume that bootloader is gone now. I never tried the chip on the Arduino board.

DrAzzy: What board are you using? It's not by any chance a ATmega on a breadboard that hasn't been bootloaded, is it? (they come set to run at 1mhz off internal osc, so if you're compiling sketch for 16mhz board, things will run at 1/16th of the speed you expect)

Hey, I just set the T to be 1000ms, recorded the video, and measured 16s, just like you said. The whole time, I was using Arduino IDE with "Arduino/Genuino UNO" and "USBAsp" settings, and it didn't report any errors. How do I switch to 16MHz using USBAsp?

EDIT:

There's a "Burn Bootloader" option in Arduino IDE, would that change the clock on a 1MHz chip to 16MHz, or I have to do the Avrdudy fusy thingy? :)

ANOTHER EDIT:

I was impatient so I just went for it, AND IT WORKS! ^^ Thanks guys!