Sequencer groove

Hey there,

I build sequencer using the arduino mega 1280. I started with very simple code just too quickly hear some beats i programmed on it and I was very happy with that danceable groove which came out. now I added alot of functions and control options for the sequencer.

now I every time the arduiono processes the loop function it has to process a big function which checks every button and so on…

when it does that, the sequencer just doesnt groove anymore:

this is the button check function which is called everytime the loop function is called:

boolean Button_Check() {
boolean any = false;  
// _______________________________________________________________________________________ProgrammButtons
  for (unsigned int i = 0; i < 16; i = i + 1) { 
    button = digitalRead(ProgramButtonPINS[i]);
    if (button == HIGH) {
      ProgramButton_ok[i]=1;
    } else {
      if (ProgramButton_ok[i]==1) {
        if ((millis() - ProgramButton_last[i]) > 120) {  
          if (Mode) {          
            if (program_sequence == i) { 
              if (Paste != 0) {
                copySequence((Paste-1), program_sequence);
                Paste = 0;
              } else {
                if (NButton_pressed == false) {            
                  play_sequence = i;
                }
                  next_play_sequence = i;
              }
            } else {
              if (NButton_pressed) {
                next_play_sequence = i;
                
              }
              program_sequence=i;
              ProgramSequenceTakt = first[program_sequence];
              display_bars(ProgramSequenceTakt);
            }
            if (PlaySequenceTakt > bars[program_sequence]) {
                PlaySequenceTakt = bars[program_sequence];
              }
            if (ProgramSequenceTakt > bars[program_sequence]) {
                ProgramSequenceTakt = bars[program_sequence];
                display_bars(ProgramSequenceTakt);
            } 
            setLeds();
            display_values(PlayMode,first[play_sequence],bars[play_sequence], active_channel);
          } else {
            if (second) {
              ReadValue(program_sequence,ProgramSequenceTakt,active_channel,(i*Faktor+16), true);
            } else {
              ReadValue(program_sequence,ProgramSequenceTakt,active_channel,(i*Faktor), true);
            }
          }
        }
        ProgramButton_last[i] = millis();
        ProgramButton_ok[i]=0;
        any=true;
      }
    }  
  }
  button = HIGH;
  
// _______________________________________________________________________________________ PlayPause 
  button = digitalRead(54);
  if (button == HIGH) {
    PlayPause_ok = HIGH;
    if (PlayPause_pressed == true && wheel == false) {
      if ((millis()-wheel_last)>1000 && NButton_pressed == false) {
        Play=inv(Play);
        if (Play) {
          digitalWrite(PlayPauseLedPIN, HIGH);
        } else {
          if (NButton_pressed == false) {
            Step = 0;
            digitalWrite(SetStepsLedPINS[3], HIGH);
            digitalWrite(SetStepsLedPINS[2], HIGH);
            digitalWrite(SetStepsLedPINS[1], HIGH);
            digitalWrite(SetStepsLedPINS[0], HIGH);
          }
          digitalWrite(PlayPauseLedPIN, LOW);
        }
      }
    }  
    PlayPause_pressed=false;
  } 
  if (button == LOW) {
    PlayPause_pressed=true;
    if (PlayPause_ok == HIGH) {
      if ((millis() - PlayPause_last) > 120) {
        wheel=false;
        if (NButton_pressed==true) {
          if (Step != 0) {
            Step = 0;
            Timer = 0;
            digitalWrite(SetStepsLedPINS[3], stepper[Step][0]);
            digitalWrite(SetStepsLedPINS[2], stepper[Step][1]);
            digitalWrite(SetStepsLedPINS[1], stepper[Step][2]);
            digitalWrite(SetStepsLedPINS[0], stepper[Step][3]);
          }
        }
      }
      PlayPause_last = millis();
      PlayPause_ok = 0;
      any = true;
    }  
  }
// _______________________________________________________________________________________ ButtonA 
  button = digitalRead(ButtonAPIN);
  if (button == HIGH) {
    if (ButtonA_pressed) {
      if (NButton_pressed==false&&ButtonB_pressed==false&&active_channel > 0&&wheel==false) {
        if ((millis() - ButtonA_last) > 50) {
          active_channel--;
        }  
        display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
        setLeds();
      }
      ButtonA_pressed = false;
    }
  } 
  if (button == LOW && ButtonA_pressed == false) {
      wheel=false;
    if ((millis() - ButtonA_last) > 50) {
      ButtonA_pressed = true;
      ButtonA_last = millis();
      if (NButton_pressed==true&&ButtonB_pressed==true) { 
          clear_bar();
          setLeds();
        } else if (NButton_pressed) {
          save();
        }
    } 
  }
// _______________________________________________________________________________________ButtonB
button = digitalRead(ButtonBPIN);
if (button == HIGH) {
  if (ButtonB_pressed) {
    if (NButton_pressed==false&&ButtonA_pressed==false&&active_channel < 9&&wheel==false) {
      if ((millis() - ButtonB_last) > 50) {
        active_channel++;
      }    
      display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
      setLeds();
    }
    ButtonB_pressed = false;
  }
} 
if (button == LOW && ButtonB_pressed == false) {
  wheel=false;
  if ((millis() - ButtonB_last) > 50) {
    ButtonB_pressed = true;
    ButtonB_last = millis();
  } 
}
// _______________________________________________________________________________________NButton
button = digitalRead(NButtonPIN);
if (button == HIGH) {
  if (NButton_pressed) {  
    NButton_pressed = false;
  }
  if ((millis() - NButton_last) > 1000 && tap == true) {
    tap_tempo[0] = 0;
    tap_tempo[1] = 0;
    tap_tempo[2] = 0;
    tap_tempo[3] = 0; 
    tapper = 0;
    tap = false;
    if (show == true) {
      show = false;
    }
  }
} 
if (button == LOW && NButton_pressed == false) {
  
  wheel=false;
  if ((millis() - NButton_last) > 160) {
    tap = true;
    NButton_last = millis();  
    tap_tempo[tapper]= micros();

    if (tap_tempo[3] != 0) {
      step_intervall=((tap_tempo[3]-tap_tempo[2])+(tap_tempo[2]-tap_tempo[1])+(tap_tempo[1]-tap_tempo[0]))/24; 
      Tempo = 600000000/(step_intervall*8);  
      tempo_last=millis();
      display_tempo();
      tap_tempo[3] = 0; 
      tapper = -1;
  } 
    tapper++;
    NButton_pressed = true;
    if (Paste != 0) {
      program_sequence = (Paste-1);
      Paste = 0;
      setLeds();
    }
  }  
} 
// _______________________________________________________________________________________ Mode
  button = digitalRead(ModePIN);
  if (button != inv(Mode)) {
    Mode = inv(button);
    setLeds();
    display_values(PlayMode,first[program_sequence],bars[program_sequence], active_channel);
  } 
  // Resolution
  button = digitalRead(ResolutionPIN);
  if (button != Resolution) {
    Resolution_last = millis();
    Resolution = button;
    if (Resolution) {
      second=false;
      Faktor = 2;
    } else {
      Faktor = 1;
    }
    setLeds();
  }
// _______________________________________________________________________________________ ENCODER
ENCODER();    
//////////////////////////////////////////////////////////////////////////////////////////////////
  return any;
}

what can i do to keep the groove?

Without knowing what display_bars(), display_values(), set_Leds(), etc. do, we can't really offer any ideas.

The most important thing that you can do, though, is make sure that the string "delay(" appears nowhere in your code.

I think that you need to separate the reading of the switches from the action that is performed based on those switches. Perhaps there are things that need to be done immediately, and things that could be done every 10th pass or every 100th pass through loop (i.e. every 10 milliseconds, every 100 milliseconds, every second...).

okay
these are the other functions

void setLeds() {
  if (Mode==false) {
    for (unsigned int i = 0; i<=15; i=i+1) {
      if (second) {
        if (ReadValue(program_sequence,ProgramSequenceTakt,active_channel,((i*Faktor)+16), false) == 0) {
          digitalWrite((StepLedPINS[i]), LOW);
        } else {
          digitalWrite((StepLedPINS[i]), HIGH);
        }
      } else {
        if (ReadValue(program_sequence,ProgramSequenceTakt,active_channel,(i*Faktor), false) == 0) {
          digitalWrite((StepLedPINS[i]), LOW);
        } else {
          digitalWrite((StepLedPINS[i]), HIGH);
        }
      }
    }
  } else {    
    for (int i = 0; i <= 15; i++) {
      if (i==program_sequence || i == (Paste-1)) {
        digitalWrite((StepLedPINS[i]), HIGH);
      } else {
        if (i != play_sequence) {
          digitalWrite((StepLedPINS[i]), LOW);
        }
      }
    }
  }
}
//________________________________________________________________________DISPLAY_BARS()
void display_bars(unsigned int count) {
  for (unsigned int i = 0; i < 4; i = i + 1) {
    if ( i == (count-1)) {
      if (count == 1) {
        digitalWrite(TaktPINS[i],HIGH);
      } else {
        analogWrite(TaktPINS[i],100);
      }
    } else {
      digitalWrite(TaktPINS[i],LOW);
    }
  }
}
//________________________________________________________________________DISPLAY_VALUES()
void display_values(unsigned int a, unsigned int b, unsigned int c, unsigned int d) {
  digitalWrite(14, HIGH);
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[b][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[a][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[d][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[c][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
  refresh = false;
  //digitalWrite(14,LOW);
} 
//________________________________________________________________________DISPLAY_TEMPO()
void display_tempo() {
unsigned int a = 0;
unsigned int b = 0;
unsigned int c = 0;
unsigned int d = 0;

a = Tempo / 1000;
a = a % 10;
b = Tempo / 100;
b = b % 10;
c = Tempo / 10;
c = c % 10;
d = Tempo % 10;
if (b == 9) {

}
display_values(a,b,c,d);
tempo_last = millis();
}
//________________________________________________________________________CLEAR_BAR()
void clear_bar() {
    for (unsigned int h = 0; h < 10; h++) {
        for(unsigned int k = 0; k < 32; k++) {
          WriteValue(program_sequence,ProgramSequenceTakt,h,k,0);
        }
      }
}
//________________________________________________________________________COPYTAKT()
void copyTakt(unsigned int copy, unsigned int paste) {
  for (unsigned int h = 0; h < 10; h++) {
    for(unsigned int k = 0; k < 32; k++) {
      WriteValue(program_sequence,paste,h,k,ReadValue(program_sequence,copy,h,k,false));
    }
  }
}
//________________________________________________________________________COPYSEQUENCE()
void copySequence(unsigned int copy, unsigned int paste) {
  first[paste]=first[copy];
  bars[paste]=bars[copy];
  for (int i = 1; i < 5; i++) {  
    for (unsigned int h = 0; h < 10; h++) {
      for(unsigned int k = 0; k < 32; k++) {
        WriteValue(paste,i,h,k,ReadValue(copy,i,h,k,false));
      }
    }
  }
}
//________________________________________________________________________READVALUE()
boolean ReadValue(int seq, int takt, int channel, int p, int Switch) {
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;
int x = count % 8;
int x2 = (count-x)/8;
count=-1;
boolean BIT = bitRead(sequenzen[x2],x);
if (Switch) {
  bitWrite(sequenzen[x2],x,inv(BIT)); 
} else {
  return BIT;
}
}
//________________________________________________________________________WRITEVALU()
void WriteValue(int seq, int takt, int channel, int p, boolean value) {
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;  
int x = count % 8;
int x2 = (count-x)/8;
count=-1;
bitWrite(sequenzen[x2],x,value); 
}
//________________________________________________________________________ENCODER()
void ENCODER() {
  encoder0PinALast = n;
  n = digitalRead(encoder0PinA);
  if ((encoder0PinALast == LOW) && (n == HIGH)) {
    if (digitalRead(encoder0PinB) == LOW) {
      links();
    } else {
      rechts();
    }
  } else if ((encoder0PinALast == HIGH) && (n == LOW)) {
    if (digitalRead(encoder0PinB) == HIGH) {
      links();
    } else {
      rechts();
    }
  }
}
//________________________________________________________________________LINKS()
void links() {
  wheel = true;
  if (PlayPause_pressed) {
    if (PlayMode > 1) {
      PlayMode--;
      display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
    }
  }
  if (ButtonA_pressed == false && ButtonB_pressed == false && NButton_pressed == false && PlayPause_pressed == false) {
    tempo_last = millis(); 
    if (show==false) {  
      Tempo = Tempo - 1;
      step_intervall = 600000000/(Tempo*8); 
    }
    display_tempo();
    show = false; 
  } 
  if (NButton_pressed==true && ButtonB_pressed==false) {
    if (Faktor == 1) { 
      if (second) {
        second = false;
        setLeds();
      } 
    } else { 
      if(ProgramSequenceTakt > 1) {
        ProgramSequenceTakt--;
      }
      wheel_last = millis();
      display_bars(ProgramSequenceTakt);
    }
    setLeds();       
  }        
  if (ButtonB_pressed==true&&NButton_pressed==false) {
    if (bars[program_sequence] > 1) {
      bars[program_sequence]=bars[program_sequence]-1;     
    }
    display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
    tempo_last = millis();
    show_bars = false;  
    setLeds();
  } 
  if (NButton_pressed==true&&ButtonB_pressed==true) {
    if (Mode) {
      if (program_sequence > 0) {
        if (Paste==0) {
          Paste=program_sequence+1;
        }
        program_sequence--;
        setLeds();    
      }
    } else {
      if (ProgramSequenceTakt > 1) {
        if (Paste==0) {
          Paste=ProgramSequenceTakt;
        }
        ProgramSequenceTakt--;
        display_bars(ProgramSequenceTakt);    
      }
    }
  }      
  if (ButtonA_pressed) {
    if (first[program_sequence] > 1) {         
      first[program_sequence]=first[program_sequence]-1;
    }     
    tempo_last = millis();
    show_bars = false;         
    display_bars(bars[program_sequence]);
    display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
  }
 }
//________________________________________________________________________RECHTS()
void rechts() {
  wheel = true;
  if (PlayPause_pressed) {
    if (PlayMode < 3) {
      PlayMode++;
      display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
    }
  }
  if (ButtonA_pressed == false && ButtonB_pressed == false && NButton_pressed == false && PlayPause_pressed == false) {
    tempo_last = millis();    
    if (show==false) {  
      Tempo = Tempo + 1;
      step_intervall = 600000000/(Tempo*8);        
    }
    display_tempo();
    show = false; 
  } 
  if (NButton_pressed==true && ButtonB_pressed==false) {
    if (Faktor == 1) {
      if (second==false) {
          second=true;
        } 
    } else {
      if(ProgramSequenceTakt < 4) {           
        ProgramSequenceTakt++;        
      }
      wheel_last = millis();
      display_bars(ProgramSequenceTakt);
    }
    setLeds(); 
  }       
  if (ButtonB_pressed==true&&NButton_pressed==false) {
    if (bars[program_sequence] < 4) {        
      bars[program_sequence]=bars[program_sequence]+1;
    }      
    display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
    setLeds();
  }
  if (NButton_pressed==true&&ButtonB_pressed==true) {
    if (Mode) {
      if (program_sequence < 15) {
        if (Paste==0) {
          Paste=program_sequence+1;
        }
        program_sequence++;
        setLeds();
      }
    } else {
      if (ProgramSequenceTakt < 4) {
        if (Paste==0) {
          Paste=ProgramSequenceTakt;
        }
        ProgramSequenceTakt++;
        display_bars(ProgramSequenceTakt);
      }
    }
  }
  if (ButtonA_pressed) {
    if (first[program_sequence] < bars[program_sequence]) {         
      first[program_sequence]=first[program_sequence]+1;
    }
    display_bars(bars[program_sequence]);  
    display_values(PlayMode,first[program_sequence],bars[program_sequence],active_channel);
    tempo_last = millis();
    show_bars = false;         
  }
}

Look through that code. How many times do you call set_Leds() as a result of each call to Button_Check()?

I'll reiterate my first advice. Separate the reading of the data from the actions based on the data from the display of the final output. For example, you are modifying what the leds are displaying, it appears, far more often than is actually required.

Check which switches are pressed. Determine what that means to the program, for all switches pressed. Then, show the results.

ok I will check that now. but what about the debouncing variables for every knob, isnt it much work for the processor? or what about the digitalRead and Writestuff, does it cost much performance to read every input in every loop?

what about the debouncing variables for every knob

The only knobs I know anything about are the ones on my kitchen cabinets that I use to open the doors.

I checked Radio Shack’s web site. They have a few knobs that attach to shafts, but none of them have any wires attached.

So, I have no idea why you need to debounce a knob.

Even if there were some reason to debounce switches, you have so much stuff going on in your code, with updating LEDs and stuff, that it is unlikely that you would get back to reading the switches again before they stopped bouncing.

Reading switches on every pass through loop won’t take any more time reading them in a loop, and then processing them, than reading and processing them one at a time in a loop.

oh I just adapted the german word “knöpfe” which has many senses. one of them is button…

...don't get him started on buttons.

what general tips can you give me to increase the efficiency of the program.

I read somewhere that i can write something like PORTX ... instead of digitalWrite or so

where can i see how many processor steps are necessary for the different tasks ?

Is efficiency a concern at the moment? I'd concentrate on getting it reliable before I thought about efficient.

yeah, it is reliable. its just not as tight as it is without all these possiblities to control the sequencer

again: what is commonly done to increase the performance?

this function is also called 10times every loop:

boolean ReadValue(int seq, int takt, int channel, int p, int Switch) {
takt--;
int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;
int x = count % 8;
int x2 = (count-x)/8;
count=-1;
boolean BIT = bitRead(sequenzen[x2],x);
if (Switch) {
  bitWrite(sequenzen[x2],x,inv(BIT)); 
} else {
  return BIT;
}
}

Well, I'd change any datatype that didn't need to be an "int" to be a "byte" or "char". I'd hope that the compiler was smart enough to perform strength-reduction on the " / 8" and "% 8" operations, and hope that the bitRead and bitWrite were inlined.

I'd hope that the compiler was smart enough to perform strength-reduction on the " / 8" and "% 8" operations, and hope that the bitRead and bitWrite were inlined.

how can i check if they're inlined?

I didnt understand the first point about strength-reduction, can you explain it?

-Flo

Flub:

I'd hope that the compiler was smart enough to perform strength-reduction on the " / 8" and "% 8" operations, and hope that the bitRead and bitWrite were inlined.

how can i check if they're inlined?

I didnt understand the first point about strength-reduction, can you explain it?

-Flo

Since you dividing by a power of 2 and performing modulus by a power of 2 and count is positive, you can replace

int x = count % 8;
int x2 = (count-x)/8;
count=-1;

with

int x = count & 0x7;
int x2 = (count-x) >> 3;

Why are you setting count = -1 after the computation of x and x2 ??

Edit: Some compilers may do this optimization for you. I don't have my Arduino in front of me so I can't verify if this happens or not. Another compiler I tried does make this optimization automatically. It's still a good trick to know if the compiler will not do it automatically.

void loop() {
  if (Play) {
    if (play()) {
      Button_Check();
    }
  }
}

////////////////////////////////////////////////////////////////////////////////////
//FUNKTIONEN                               
////////////////////////////////////////////////////////////////////////////////////

//________________________________________________________________________SETLEDS()
void setLeds() {
 /* if (Mode==false) {*/
    for (unsigned int i = 0; i<=15; i=i+1) {
   //   if (second) {
      /*  if (ReadValue(program_sequence,ProgramSequenceTakt,active_channel,((i*Faktor)+16), false) == 0) {
          digitalWrite((StepLedPINS[i]), LOW);
        } else {
          digitalWrite((StepLedPINS[i]), HIGH);
        }*/
     // } else {
        if (ReadValue(program_sequence,ProgramSequenceTakt,active_channel,(i*Faktor), false) == 0) {
          digitalWrite((StepLedPINS[i]), LOW);
        } else {
          digitalWrite((StepLedPINS[i]), HIGH);
        }
     // }
    }
  /*} else {    
    for (int i = 0; i <= 15; i++) {
      if (i==program_sequence || i == (Paste-1)) {
        digitalWrite((StepLedPINS[i]), HIGH);
      } else {
        if (i != play_sequence) {
          digitalWrite((StepLedPINS[i]), LOW);
        }
      }
    }
  }*/
}
//________________________________________________________________________DISPLAY_BARS()
void display_bars(unsigned int count) {
  for (unsigned int i = 0; i < 4; i = i + 1) {
    if ( i == (count-1)) {
      if (count == 1) {
        digitalWrite(TaktPINS[i],HIGH);
      } else {
        analogWrite(TaktPINS[i],100);
      }
    } else {
      digitalWrite(TaktPINS[i],LOW);
    }
  }
}
//________________________________________________________________________DISPLAY_VALUES()
void display_values(unsigned int a, unsigned int b, unsigned int c, unsigned int d) {
  digitalWrite(14, HIGH);
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[b][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[a][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[d][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
  for (unsigned int i = 0; i < 4; i = i+1) {
    digitalWrite(DisplayDataPIN,numbers[c][i]);
    digitalWrite(DisplayClockPIN, HIGH);
    digitalWrite(DisplayDataPIN,LOW);
    digitalWrite(DisplayClockPIN, LOW);
  }
} 
//________________________________________________________________________READVALUE()
inline boolean ReadValue(int seq, int takt, int channel, int p, boolean Switch) {
  takt--;
  int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;
  int x = count % 8;
  int x2 = (count-x)/8;
  count=-1;
  boolean BIT = bitRead(sequenzen[x2],x);
  if (Switch) {
    bitWrite(sequenzen[x2],x,inv(BIT)); 
  } else {
    return BIT;
  }
}
//________________________________________________________________________WRITEVALUE()
void WriteValue(int seq, int takt, int channel, int p, boolean value) {
  takt--;
  int count = (seq*4*10*32)+(takt*10*32)+(channel*32)+p;  
  int x = count % 8;
  int x2 = (count-x)/8;
  count=-1;
  bitWrite(sequenzen[x2],x,value); 
}
//________________________________________________________________________inv()
inline boolean inv(boolean x) {
  if (x==HIGH) {
    return LOW;  
  } else {
    return HIGH;
  }
}
boolean play () {
  if ((micros() - Timer) > 66000) {
    for (int i = 0; i<10; i++) {
      channelvals[i]=ReadValue(play_sequence,PlaySequenceTakt,i,Step,false);
    }
    for (int i = 0; i<10; i++) {
      digitalWrite(55+i,channelvals[i]);
    }
    Step++;
    Timer = micros();
    lastOut = micros();
    if ((micros()-lastOut) > 50) {
      for (int i = 0; i<10; i++) {
        digitalWrite(55+i,LOW);
      }
    }
    if (Step > 31) {
      Step = 0;
    } else {
      Step = Step;
    }
    if (PlaySequenceTakt == ProgramSequenceTakt) {      
        if (Faktor == 2) {
          digitalWrite(SetStepsLedPINS[3], stepper[Step/Faktor][0]);
          digitalWrite(SetStepsLedPINS[2], stepper[Step/Faktor][1]);
          digitalWrite(SetStepsLedPINS[1], stepper[Step/Faktor][2]);
          digitalWrite(SetStepsLedPINS[0], stepper[Step/Faktor][3]); 
        } else {
          if (Step > 15) {
            StepSize=Step-16;         
          } else { 
            StepSize = Step; 
          }
            digitalWrite(SetStepsLedPINS[3], HIGH);
            digitalWrite(SetStepsLedPINS[2], HIGH);
            digitalWrite(SetStepsLedPINS[1], HIGH);
            digitalWrite(SetStepsLedPINS[0], HIGH);
        }
      } else {
        digitalWrite(SetStepsLedPINS[3], HIGH);
        digitalWrite(SetStepsLedPINS[2], HIGH);
        digitalWrite(SetStepsLedPINS[1], HIGH);
        digitalWrite(SetStepsLedPINS[0], HIGH);
      }
    return false;
  } else {
    return true;
  }  
}
//________________________________________________________________________Button_Check()
boolean Button_Check() {
  for (unsigned int i = 0; i < 16; i++) {
    button_state=digitalRead(ProgramButtonPINS[i]);  
    if (button_state==LOW) {
      if (button_ok[i]) {
        button_ok[i]= false;
        button_last = millis();
        ReadValue(program_sequence,ProgramSequenceTakt,active_channel,(i*Faktor), true);
        setLeds();
      }
    } else if ((button_ok[i]) == false) {
      if ((millis()-button_last) > 100) {
        button_ok[i] = true;
      }
    }
  }
  //________________________________________________________________ButtonA
  button_state=digitalRead(ButtonAPIN);
  if (button_state==LOW) {
    if (ButtonA_ok) {
      ButtonA_ok = false;
      button_last=millis();
      active_channel--;
      setLeds();
      display_values(0,0,0,active_channel);
    }
  } else if ((ButtonA_ok) == false) {
    if ((millis()-button_last) > 200) {
    ButtonA_ok = true;
    }
  } 
  //________________________________________________________________ButtonB  
  button_state=digitalRead(ButtonBPIN);
  if (button_state==LOW) {
    if (ButtonB_ok) {
      ButtonB_ok = false;
      button_last=millis();
      active_channel++;
      setLeds();
      display_values(0,0,0,active_channel);
    }
  } else if ((ButtonB_ok) == false) {
    if ((millis()-button_last) > 200) {
    ButtonB_ok = true;
    }
  }  
}

Thank you for the modulo stuff !
I now started to rearrange and improve the code. Thats what ive got yet. Do you see anything to improve?

Flub: Thank you for the modulo stuff ! I now started to rearrange and improve the code. Thats what ive got yet. Do you see anything to improve?

What actually are your goals ?? Are you trying to reduce the amount of C code you have to write/debug/maintain, trying to reduce final program size, trying to have the code run faster, or something else ??

I want the code to go faster, because the sequenzer triggers a Drumsynth I built and when the code was still basic to test if it worked, the rhythm had much more groove then it had after I added all the functions, display options and button combinations That I want the sequencer to have. This is the reason why I want to be as effiecient as possible to keep the beat tight. ( the difference is not really big tecnically but the whole feel is very different, when it is fast you feel your body wants to move, when it is slow it just doesnt make you feel like that.

It is very important that the trigger pulses come at the same point in time and this in a very strict time interval , so the generated sounds really clash together if you know what i mean. Perhaps I could try to set all trigger voltages up right connected to a 4066 analog switch which is later opened by a slightly delayed clock pulse from the sequencer so the trigger pulses really come out at the same time.

sorry for my bad english, if it is bad -Flo

If you want to squeeze some more cycles, you could look at replacing the "digitalWrite"s with direct port manipulation. I'd check what the compiler is doing with the division and modulo ops, then if it really is doing division, replace them with mask and shift operations.

how can i check this? i use the arduino ide