Pages: [1] 2   Go Down
Author Topic: getting my sequencer BPM conform  (Read 1624 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,
i build a small 8 step sequencer, how can I get it to work with real BPM?
right now I have a default delay of 50ms between each step, and another 50ms for LED blink.

If my math is right it should be 7500ms for each step to get 60 BPM. is that right?

I also have a button to increase the delay by 50ms.
Is there a usual way to do this?


thnx
Logged

Manchester (England England)
Offline Offline
Brattain Member
*****
Karma: 514
Posts: 31554
Solder is electric glue
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
If my math is right it should be 7500ms for each step to get 60 BPM. is that right?

I don't think so.
7500mS is 7.5 seconds so in 60 seconds you get 8 BPM (beats per minute)

For 60 BPM you need one beat every second or 1000mS.
Logged

Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Use a Timer, much better timing. Here's my code for the timer.

Code:
void timerStart()
{
  TCCR1A = TCCR1B = 0;
  bitWrite(TCCR1B, CS11, 1);
  bitWrite(TCCR1B, WGM12, 1);
  timerSetFrequency();
  bitWrite(TIMSK1, OCIE1A, 1);
}

void timerSetFrequency()
{
  // Calculates the Frequency for the Timer, used by the PPQ clock (Pulses Per Quarter Note) //
  // This uses the 16-bit Timer1, unused by the Arduino, unless you use the analogWrite or Tone functions //
  #define frequency (((midiClockBPM)*(PPQ))/60)
  OCR1A = (F_CPU/ 8) / frequency - 1;
}

void timerStop(void)
{
  bitWrite(TIMSK1, OCIE1A, 0);
  TCCR1A = TCCR1B = OCR1A = 0;
}

ISR(TIMER1_COMPA_vect)
{
// here's your clock code
}
Logged


Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

BTW: on my code for Beat707 I use less PPQ, so I just use a counter variable to check when a step is due or not. I like to keep things at 96 PPQ for the midi clock shuffle. I can get you more details over this after tomorrow if you want me to. ;-)

Wk
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

thnx for that code, i still dont understand a thing of that, but I will take my time to do so.
I seens some videso of your sequencer, very nice project.

solved:
Right now(after i included a play button) I have the problem that the sequence changes tempo each
time, one fast one slower. Would this be gone if I could include your code?
(it was the switch i was using, this happens only if the circuit ist closed)
« Last Edit: April 25, 2011, 04:40:01 am by Rupertt » Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

could you give me an example how I can use the above code to act as a timer and set the main frequncy to
120BPM on 8 steps?
Logged

Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Will do, give me 15 minutes... ;-)

Wk
Logged


Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
/*

  Created by Beat707 (c) 2011 - http://www.Beat707.com
 
  8 Step Mini MIDI Sequencer (4 tracks)

*/

uint8_t midiClockBPM = 120; // Max 255
uint8_t midiClockPPQCounter = 0;
uint8_t midiClockSteps[4] = { B10001000, B00001000, B11111111, B00100010 };
uint8_t midiClockStepsNote[4] = { 36, 38, 42, 46 }; // Kick, Snare, HiHatClosed, HiHatOpen
uint8_t midiClockCurStep = 0;
uint8_t midiChannel = 9;
#define PPQ 96

// ======================================================================================= //
void setup()
{
  pinMode(17,OUTPUT); digitalWrite(17,LOW); // For the Beat707 Hardware - Midi Enable //
 
  Serial.begin(31250);
  sendMidiAllNotesOff();
  midiClockBPM = 120;
  midiClockPPQCounter = midiClockCurStep = 0;
  timerStart();
}

// ======================================================================================= //
void loop()
{
  //
}

// ======================================================================================= //
void timerStart()
{
  TCCR1A = TCCR1B = 0;
  bitWrite(TCCR1B, CS11, 1);
  bitWrite(TCCR1B, WGM12, 1);
  timerSetFrequency();
  bitWrite(TIMSK1, OCIE1A, 1);
}

// ======================================================================================= //
void timerSetFrequency()
{
  #define frequency (((midiClockBPM)*(PPQ))/60)
  OCR1A = (F_CPU/8) / frequency - 1;
}

// ======================================================================================= //
void timerStop(void)
{
  bitWrite(TIMSK1, OCIE1A, 0);
  TCCR1A = TCCR1B = OCR1A = 0;
}

// ======================================================================================= //
ISR(TIMER1_COMPA_vect)
{
  midiClockPPQCounter++;
  if (midiClockPPQCounter >= 24)
  {
    midiClockPPQCounter = 0;
 
    for (uint8_t x=0; x<4; x++)
    {
      if (bitRead(midiClockSteps[x],midiClockCurStep))
      {
        sendMidiNoteOff(midiClockStepsNote[x]); // Stop Previous Note //
        sendMidiNoteOn(midiClockStepsNote[x], 127);
      }
    }
   
    midiClockCurStep++;
    if (midiClockCurStep >= 8) midiClockCurStep = 0;
  }
}

// ======================================================================================= //
void sendMidiNoteOn(char note, char velocity)
{
  Serial.write(0x90+midiChannel);
  Serial.write(note);
  Serial.write(velocity);
}

// ======================================================================================= //
void sendMidiNoteOff(char note)
{
  Serial.write(0x80+midiChannel);
  Serial.write(note);
  Serial.write((byte)0x00);
}

// ======================================================================================= //
void sendMidiAllNotesOff()
{
  Serial.write(0xB0+midiChannel);
  Serial.write(0x7B);
  Serial.write((byte)0x00);
}

Have Fun!  smiley-cool
Logged


Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Now, just add some code to change midiClockSteps[] in real-time to edit the steps for each of the 4 tracks. And its easy to add more tracks if you want to.  smiley-mr-green

Wk
Logged


Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Now, there's an important thing I forgot to mention... you can't use millis() or delay() at all. Use the following code instead, otherwise midi-timing will be bad...

Code:
// The Following is to implement millis() and delay() without messing up with the midi clock interrupt calls //
extern volatile unsigned long timer0_millis;
unsigned long millisNI(void) { return timer0_millis; }
void delayNI(unsigned long ms)
{
  unsigned long endtime;
  endtime = timer0_millis + ms;
  while (((long)endtime - (long)timer0_millis) > 0)
  {
    ;
  }
}
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

i integrated your code, but i still dont get how to set the steps/delay inside my code:

 this is one of my steps, you can see the the "  delay(currentTime);" defines the time until the next step is called.
Code:
if(x==0){
          //channel 1
            if (currentSwitchState1 == 1) {
                AnalogValue1 = analogRead(0);
                note1 = AnalogValue1/8;
                noteOn(0xB0, 0x7B, 0x00);
                noteOn(0x90, note1, 0x7F);
                //einstellen wie lange der sound ausklingt
                //  81  2B  4D
                digitalWrite(LEDpin1, HIGH);
                delay(50);
                digitalWrite(LEDpin1, LOW);
                delay(currentTime);
            } else {
              delay(currentTime);
            }
        }

sorry, but i come from the web programming and have only little experience in microcontroller and C programming.

cheers
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

i integrated your code, but i still dont get how to set the steps/delay inside my code:

 this is one of my steps, you can see the the "  delay(currentTime);" defines the time until the next step is called.
Code:
if(x==0){
          //channel 1
            if (currentSwitchState1 == 1) {
                AnalogValue1 = analogRead(0);
                note1 = AnalogValue1/8;
                noteOn(0xB0, 0x7B, 0x00);
                noteOn(0x90, note1, 0x7F);
                //einstellen wie lange der sound ausklingt
                //  81  2B  4D
                digitalWrite(LEDpin1, HIGH);
                delay(50);
                digitalWrite(LEDpin1, LOW);
                delay(currentTime);
            } else {
              delay(currentTime);
            }
        }

this is how i increase/decrease my delay:
Code:
currentTimeState = digitalRead(timePin1);
      if (currentTimeState == 1) {
        currentTime = currentTime + 50;
      }

  currentTimeStateDown = digitalRead(timePin2);
      if (currentTimeStateDown == 1) {       
        if (currentTime <= 50) {
          currentTime = 100;
        } else {     
          currentTime = currentTime - 50;
        }
      }
     

sorry, but i come from the web programming and have only little experience in microcontroller and C programming.

cheers
Logged

Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hello,

i tried to exchange my delay with your delayNI.
sadly the sequencer doenst start at all, it doesnt even start when I just enter your function and use the normal delay.

Did you realize your project alone or did you have help?
Im thinking about joining someone, i have good iedas, but my C programming lacks a bit
Logged

Brazil
Offline Offline
God Member
*****
Karma: 1
Posts: 616
Wusik Dot Com
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Maybe try posting the entire code so we can all take a look at it? ;-)

Wk
Logged


Offline Offline
Jr. Member
**
Karma: 0
Posts: 79
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

thats all i got  smiley-cool

The code was to long to post, so I attached the source file.

Still have so many ideas for this project, but one step after another (nice in this context)

* sequencer_2_8x_bpm2.pde (10.23 KB - downloaded 2 times.)
Logged

Pages: [1] 2   Go Up
Jump to: