halfbyte / arduino-seq

A few experiments for using the arduino platform as a standalone sequencer.

This URL has Read+Write access

halfbyte (author)
Wed Aug 20 23:09:57 -0700 2008
arduino-seq / seq2.pde
100644 120 lines (99 sloc) 2.672 kb
1
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
* Button
* by DojoDave <http://www.0j0.org>
*
* Turns on and off a light emitting diode(LED) connected to digital
* pin 13, when pressing a pushbutton attached to pin 7.
*
* http://www.arduino.cc/en/Tutorial/Button
*/
 
#define SER_PIN 8
#define LATCH_PIN 9
#define CLK_PIN 10
#define SPEED_PIN 5
 
int ledPins[8] = {2,3,4,5,6,7,11,12};
 
 
unsigned int val = 0; // variable for reading the pin status
unsigned int position = 0;
 
unsigned char patterns[7] = {1,0,0,0,0,0,0};
unsigned char notes[6] = {0x24, 0x26, 0x27, 0x2A, 0x2C, 0x31};
 
long time = 0;
int selectedTrack = 0;
int oldVal = 0;
void setup() {
  int i = 0;
  pinMode(LATCH_PIN, OUTPUT); // declare LED as output
  pinMode(CLK_PIN, OUTPUT); // declare LED as output
  pinMode(SER_PIN, INPUT); // declare pushbutton as input
  Serial.begin(31250);
  //Serial.begin(9600);
  digitalWrite(LATCH_PIN, LOW);
  digitalWrite(CLK_PIN, LOW);
  for(i=0;i<8;i++) pinMode(ledPins[i], OUTPUT);
  time = millis();
}
 
 
 
int readButtons(void) {
  int i=0;
  int buttonField = 0;
  digitalWrite(LATCH_PIN, HIGH);
  asm("nop\n nop\n");
 
  for(i=15;i>=0;i--) {
    int bit = 0;
    bit = digitalRead(SER_PIN);
    if (bit == LOW) buttonField = buttonField | (1 << i);
    digitalWrite(CLK_PIN, LOW);
    asm("nop\n nop\n");
    digitalWrite(CLK_PIN, HIGH);
    asm("nop\n nop\n");
 
  }
  digitalWrite(LATCH_PIN, LOW);
  //asm("nop\n nop\n");
  return buttonField;
}
 
void noteOn(char cmd, char data1, char data2) {
  Serial.print(cmd, BYTE);
  Serial.print(data1, BYTE);
  Serial.print(data2, BYTE);
}
 
 
  void updateLeds(void) {
    int i=0;
    for(i=0;i<8;i++) {
      digitalWrite(ledPins[i], ((patterns[selectedTrack] & (1 << i))) ? HIGH : LOW);
    }
  }
 
 
void loop(){
  int i=0;
  int speedVal = 0;
  //int notes[8] = {24,25,26,27,28,29,30,31};
  val = readButtons();
  if(val!=oldVal) {
    // trigger buttons
    for(i=0;i<8;i++) {
      if((val & (1 << i)) && (!(oldVal & (1 << i)))) {
         patterns[selectedTrack] ^= (1 << i);
         updateLeds();
      }
    }
    for(i=8;i<15;i++) {
      if((val & (1 << i)) && (!(oldVal & (1 << i)))) {
         selectedTrack = i - 8;
         updateLeds();
      }
    }
    
    oldVal = val;
  }
 
  
  // here goes seq code
  
  speedVal = analogRead(SPEED_PIN) / 2;
  if (millis() > (time + speedVal)) {
    //seq code;
    for(i=0;i<6;i++) {
     if(patterns[i] & (1 << position)) {
        int velo = 0x40;
        if (patterns[6] & (1 << position)) velo = 0x7F;
        noteOn(0x99, notes[i], velo);
        noteOn(0x99, notes[i], 0x00);
     }
    }
    position = (position +1) % 8;
    time = millis();
  }
}