16 step sequencer - Interrupt/timer question.

Hello forum.

I have searched the internet for months without any recent result and I am beginning to think that I should discard the project. (And my future projects. Hehe.)

My intention was to use a modified version of an LED chaser code, but I realised that the delay() function is no good for my needs. I know that I have to use interrupt or timer (Or is it the same?), but I can not make it work. Sometimes only one part of the code works and sometimes nothing works - So I always ending up deleting it and trying to find something else that are supposed to work.

The main program only has to make the 16 outputs HIGH one at a time, and the others LOW.

I hope it is also acceptable for me to ask for code-snippets/links/approaches for the following things as well:

  • 1 rotary encoder for BPM (Maybe more options if possible. But KISS)
  • An LCD display for BPM (Maybe more options if possible. But KISS)
  • Start/Stop/Pause
  • Forward/Backwards in pause mode
  • Random mode

I would be really grateful if any of you could help me.

Thanks. :)

but I realised that the delay() function is no good for my needs. I know that I have to use interrupt or timer

The first part is correct. The second part is wrong.

How would YOU perform the task, given a watch, a pencil, and a pad of paper?

The pencil corresponds to the Arduino's ability to write to memory. The pad of paper corresponds to memory. The watch corresponds to the millis() or micros() functions.

I cannot understand what you want to do based on the description in your post.

I suspect your project will work perfectly well using millis() for timing as illustrated in several things at a time. It will be much easier to do that that to use interrupts of hardware timers.

The Thread planning and implementing a program may also be useful.

It would also be useful if you post the code that is giving you trouble together with a description of what it actually does and what you want it to do.

...R

PaulS: The first part is correct. The second part is wrong.

How would YOU perform the task, given a watch, a pencil, and a pad of paper?

The pencil corresponds to the Arduino's ability to write to memory. The pad of paper corresponds to memory. The watch corresponds to the millis() or micros() functions.

The second part is right when I want to use a rotary encoder?

Okay. I will go for the millis() then.

Robin2: I cannot understand what you want to do based on the description in your post.

I suspect your project will work perfectly well using millis() for timing as illustrated in several things at a time. It will be much easier to do that that to use interrupts of hardware timers.

The Thread planning and implementing a program may also be useful.

It would also be useful if you post the code that is giving you trouble together with a description of what it actually does and what you want it to do.

...R

I want to make a device like this: https://www.youtube.com/watch?v=EajfnpJ-v_I But without the sound generating. Only the part making each step one after another go HIGH and the others LOW.

I will look at the link.

The problem is that I have tried so many things that I do not know which option I want to count on. Therefor I do not have any code to show you.

Thanks both of you. :)

The second part is right when I want to use a rotary encoder?

Maybe. Maybe not. It depends on how many pulses per revolution, what else the Arduino is doing, and how critical a missed pulse is.

Okay. I will go for the millis() then.

Excellent choice.

Only the part making each step one after another go HIGH and the others LOW.

Looks pretty easy. The thing to think about is this. On any given pass through loop(), it may, or may not, be time to do something. If is is, what to do?

The millis() function and recording when the last thing happened will take care of the first part. Keeping track of what was done last time, and using that to decide what to do this time is going to be important.

PolyGrafik: The problem is that I have tried so many things that I do not know which option I want to count on. Therefor I do not have any code to show you.

I hope planning and implementing a program will give you some ideas for organizing your thoughts.

The important thing is to deal with the project as a series of little pieces rather than one single mammoth that has to be eaten whole.

...R

Hi again.

Finally I got things to work, now the steps work! Thanks guys. :smiley:

But I have a problem. I use a library called “TimedAction.h” - But I can not make the timer adjustable. Is there a way to do this, or should I use an other library or millis() in is pure form?

“TimedAction counterscriptAction = TimedAction(100,counterscript);”

Thanks. :slight_smile:

#include <TimedAction.h>

TimedAction counterscriptAction = TimedAction(100,counterscript);

int buttonPin = 2;
int buttonState = 0;
boolean ran = true;
int counter = 0;
int ran1;
int stepamount = 16;

void setup(){
  for (int thisPin = 22; thisPin < 38; thisPin++)  {
    pinMode(thisPin, OUTPUT);   
    
  }  
  pinMode(buttonPin, INPUT);
  for (int thisPin = 22; thisPin < 38; thisPin++)  {
    digitalWrite(thisPin, LOW);
  }
}

void loop(){
  counterscriptAction.check();
  pattern();
  pushbutton();
}

void counterscript(){
  ran1 = random(0, stepamount -1);
  if(ran == true){
    counter = ran1;
    if(counter > stepamount - 1) {
      counter = 0;
    }
  }

  else{
    counter ++;
    if(counter > stepamount -1) {
      counter = 0;
    }
  }
}

void pushbutton(){
  buttonState = digitalRead(buttonPin);
  if (buttonState == HIGH) {
    ran = true;
  }

  else{
    ran = false;
  }

}

void pattern(){
//Cut it out - ITS OVER 9000!
}

PolyGrafik: I use a library called "TimedAction.h" - But I can not make the timer adjustable. Is there a way to do this, or should I use an other library or millis() in is pure form?

I don't use timing libraries I just use millis(). I figured out I could spend a long time trying to make sense of a library OR I could spend the same amount of time working on my own project.

...R

Well, actually. I just got it to work with millis(). In some way it is easier when you have the control over everything. ;)

PolyGrafik: In some way it is easier when you have the control over everything. ;)

I agree completely.

...R