Debreczeni Tamas
Published © GPL3+

Arduino Step Sequencer

A toy to create "music" or to annoy someone.

IntermediateFull instructions provided6,586
Arduino Step Sequencer

Things used in this project

Hardware components

Arduino Nano R3
Arduino Nano R3
×1
5 mm LED: Green
5 mm LED: Green
×4
Resistor 221 ohm
Resistor 221 ohm
×4
Rotary potentiometer (generic)
Rotary potentiometer (generic)
×6
Speaker: 0.25W, 8 ohms
Speaker: 0.25W, 8 ohms
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Schematic

Code

Arduino sequencer

Arduino
// Declaration of variables
const byte speaker = 13;  //  Speaker pin
const byte pat0 = 4;      //  Led pins
const byte pat1 = 5;
const byte pat2 = 6;
const byte pat3 = 7;

bool debug = false; // Used to print values to Serial Monitor
bool isOn = false;  // Checks if the device should work or not
long counter = 0;   // Used for speed
long tempo;         // Used for reading the speed
int lowFreq = 10;    // The lower frequency boundary
int highFreq = 2000;// The higher frequency boundary
int pattern = 0;    // Current potentiometer
unsigned int note;  // Note that will be played

void switchLEDs(int pattern) {  // Turn on and off the leds for specific patterns
  switch (pattern) {
    case 0:
      digitalWrite(pat3, LOW);
      digitalWrite(pat0, HIGH);
      break;
    case 1:
      digitalWrite(pat0, LOW);
      digitalWrite(pat1, HIGH);
      break;
    case 2:
      digitalWrite(pat1, LOW);
      digitalWrite(pat2, HIGH);
      break;
    case 3:
      digitalWrite(pat2, LOW);
      digitalWrite(pat3, HIGH);
      break;
  }
}

int getNote(int pattern) {  //  Get notes for specific patterns
  if (pattern == 0) {
    note = map(analogRead(A0), 0, 1023, lowFreq, highFreq);
  } else if (pattern == 1) {
    note = map(analogRead(A1), 0, 1023, lowFreq, highFreq);
  } else if (pattern == 2) {
    note = map(analogRead(A2), 0, 1023, lowFreq, highFreq);
  } else if (pattern == 3) {
    note = map(analogRead(A3), 0, 1023, lowFreq, highFreq);
  }
  return note;
}


void setup() {
  
  pinMode(pat0, OUTPUT);
  pinMode(pat1, OUTPUT);
  pinMode(pat2, OUTPUT);
  pinMode(pat3, OUTPUT);
  digitalWrite(pat0, LOW);
  digitalWrite(pat1, LOW);
  digitalWrite(pat2, LOW);
  digitalWrite(pat3, LOW);
}


void loop() {


  tempo = map(analogRead(A4), 0, 1023, 4000, 100);
  if (tempo < 3900) { // Enter if speed still acceptajk  ble
    if (counter > tempo) {
      if (debug) {
        Serial.println(pattern);
      }
      noTone(speaker);
      counter = 0;

      switchLEDs(pattern);
      note = getNote(pattern);

      pattern = (pattern + 1) % 4;
      if (note > lowFreq + 10) {
        tone(speaker, note);
      }
    }
    counter++;
  } else {  // Stopped on a specific potentiometer
    switchLEDs(pattern);
    unsigned int note = map(analogRead(pattern), 0, 1023, lowFreq, highFreq);
    
    if (note > lowFreq + 10) {
      tone(speaker, note);
    }else{
      noTone(speaker);
    }
  }
}

Credits

Debreczeni Tamas

Debreczeni Tamas

6 projects • 33 followers

Comments