Trying to expand on this Arduino Arpeggiator project for my Moog Werkstatt synth

Hello,
I a recently did a Arduino Arpeggiator project for my Moog Werkstatt synth that I found at the following link.

I got everything to work just as shown in the video and understand enough to modify the sequence to how I want it but now I want to do more. I was hoping I could do a multi position switch (rotary 8 pole) that would select 8 different sequences. Is there anyone willing to help me figure out how to do this? I'm really pretty lost when it comes to coding so please dumb any advise down as much as possible.

I plan to use a teensy 3.2 board.

I was originally thinking maybe having 8 different Arpeggiators always running on different output pins and the rotary switch would select which one would be sent to the synth. But I'm sure the better way would be to write an If/Then statement that looks for an input from the switch and then selects which sequence to run. It really seems like this code should be fairly simple but I really have no idea on how go about writing it.

Anyone feeling like helping me out on this? I'd really appreciate any help I could get.

#include <werkstatt.h>

arpeggiator arpeggiator(6); //initiate and name the arpeggiator class (Output pin)

/* 
notes are assigned in intervals: tonic, minor2nd, major2nd, minor3rd,
major3rd, fourth, tritone, fifth, minor6th, major6th, minor7th,
major7th, octave. 
*/
  int notes[] = {tonic, octave}; // VCO EXP config


// note values: w, h, q, qt, e, et, sx, sxt, th, sxf 
  int note_values[] = {e, e}; //VCO EXP config

  
void setup() {
}      


void loop() {
for (int i = 0; i < ( sizeof(notes)/sizeof(int) ); i++)
  {
   //define a BPM and run the arpeggiator.play function  
   arpeggiator.play(140, notes[i], note_values[i]); 
   }
}
arpeggiator arpeggiator(6); //initiate and name the arpeggiator class (Output pin)

Step 1: The class name and the class instance should never be the same.

that would select 8 different sequences.

What does "select 8 different sequences" mean? If you mean that loop() should read the state of the switch, and execute different for loops based on the position of the switch, say that.

How is the switch connected? Do you know how to determine what position the switch is in? Do you know how to use a switch statement, with 8 cases, to call one function per case?

Writing the 8 functions is going to be up to you, as you are the only one that knows what the other 7 functions should do.

Assuming you mean an 8-throw single-pole switch and you have 7 I/O pins to spare:

const int SWITCH_PINS[] = {2,3,4,5,7,8,9};  // Leave the 8th position unconnected. Common to Ground.
const int SWITCH_COUNT = sizeof SWITCH_PINS / sizeof SWITCH_PINS[0];

void setup() {
   Serial.begin(19200);
   for (int 1=0; i<SWITCH_COUNT; i++) {
      pinMode(SWITCH_PINS[i], INPUT_PULLUP);
    }
}

void loop() {
   static int previousPosition = 0;
   int currentPosition = GetPosition();


    // Show switch position if it has changed
   if (currentPosition != previousPosition) {
       Serial.print("Moved from position ");
       Serial.print(previousPosition);

       Serial.print(" to position ");

       Serial.println(currentPosition);

       previousPosition = currentPosition;
    }
}


int GetPosition() {
   for (int 1=0; i<SWITCH_COUNT; i++) 
      if (digitalRead(SWITCH_PINS[i]) == LOW) // LOW = connected to ground through switch
         return i;
    return SWITCH_COUNT;
}

Thank you both for replying and trying to help but I think this is a little over my head. I think I will abandon the dream of programming it for 8 different arpeggiators/sequences options and for now start with something much simpler.

What if I wanted to do my original idea and just simply have two different arpeggiators running on two different output pins? Then I could just use a toggle to select which was connected to the synth. Would this be possible to have two difference instances of the same code running on the same arduino but just on different output pins?

ZuperMike:

Quote from: ZuperMike Sat Nov 12 2016 15:43:09 GMT-0500 (EST)Would this be possible to have two difference instances of the same code running on the same arduino but just on different output pins?

The main problem is that the 'arpeggiator' object does the note timing with delay(). That means that even though they can each use a different analogWrite()/PWM pin they can't switch notes while another arpeggiator is playing a note. Thus only one can be active at a time

The actual calculations are quite simple so it is POSSIBLE to use millis() or micros() to schedule pitch changes and allow multiple arpeggiators to run 'simultaneously'. On an Arduino UNO you have 6 analog outputs so you could run 6 at a time. Other models have different numbers of outputs (Arduino MEGA has 16).

The important numbers are the values you feed to analogWrite():

#define tonic 0
#define minor2nd 5
#define major2nd 10
#define minor3rd 15
#define major3rd 20
#define fourth 25
#define tritone 30
#define fifth 35
#define minor6th 40
#define major6th 45
#define minor7th 50
#define major7th 55
#define octave 60
#define octave2 120

Since you pass the BPM value, the note, and the note duration to the arpeggiator for each note it is easy to switch between them in software. You could just use a single button to cycle through the sequences rather than using a multi-way switch. It wouldn't be as fast to get to a specific sequence but it would be easier to code. You could use a potentiometer on an analog input to select among the sequences. That would get you direct access but wouldn't have the detents of the multi-way switch unless you added hardware for it.

Thank you for trying to explain it to me but I am still lost. I know should probably sit down and read an arduino book to teach myself that is no fun. haha. I'm more into instant gratification. So my next idea is that I will just have two different Teensy boards each running their own arpeggiator and use a toggle to select which is active.