DIY software midi controller, pots + switches help needed, please

Hello,
I am new to Arduino. Just got my first board a few days ago, been having fun wih it sinec :slight_smile:

Anyways, I am trying to make a DIY midi controller with a few pots and switches to control Traktor, Virtual DJ, Ableton etc
I’ve been looking around and found this:

which is what I wanted, but it’s not running on Arduino

and this:

this is perfect for what I want it for, and I’d also like to use a software to convert to MIDI for now.
So, I got switches set up, I can map them and everything is ok, but I have no idea on how to add analog input to the above setup, for the pots. If anyone could give me some ideas or help me with the code that would be great.

/*
  Arduino Controlled (LED)Strobe
 This code was made from various example codes that come with the Arduino IDE and some of my own.
 
 - It has an LED connected to Pin 13 (could use the output with some NPN transistors to hook up an LED arrey)
 - There is a 10K pot connected to Analog 0 (for controlling the off time)
 - There is a switch connected to pin 2 as an input with a 10K resistor (pressing the switch turns on the Strobe, long pressin it turns it off)
 - Can as well monitor the output on the serial monitor
 
 */

const int sensorPin = A0;    // select the input pin for the potentiometer
const int ledPin = 13;      // select the pin for the LED
const int  buttonPin = 2;   // select the input pin for the switch
int sensorValue = 0;  // variable to store the value coming from the sensor
int outputValue = 0;

int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0;     // previous state of the button


void setup() {
  // declare the ledPin as an OUTPUT and declare the buttonPin as an INPUT:
  pinMode(buttonPin, INPUT);
  pinMode(ledPin, OUTPUT);  
  Serial.begin(9600); 
}

void loop() {

  buttonState = digitalRead(buttonPin);
  if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button
      // wend from off to on:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes:  ");
      Serial.println(buttonPushCounter);
    } 
    else {
      // if the current state is LOW then the button
      // wend from on to off:
      Serial.println("off"); 
    }
  }
  // save the current state as the last state, 
  //for next time through the loop
  lastButtonState = buttonState;

  if (buttonPushCounter % 2 == 0) {

    // read the value from the sensor:
    sensorValue = analogRead(sensorPin);    
    outputValue = map(sensorValue, 0, 1023, 25, 1000);
    // 25 is the minimum off time, 1000 is the maximum off time 
    // (I think it's in ms, changing these values will change
    //  the behaviour of the pot controllin the speed

    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      
    Serial.print("\t output = ");      
    Serial.println(outputValue);  // turn the ledPin on

    digitalWrite(ledPin, HIGH);  
    // stop the program for <sensorValue> milliseconds:
    delay(30);          
    // turn the ledPin off:        
    digitalWrite(ledPin, LOW);   
    // stop the program for for <sensorValue> milliseconds:

    // print the results to the serial monitor:
    Serial.print("sensor = " );                       
    Serial.print(sensorValue);      
    Serial.print("\t output = ");      
    Serial.println(outputValue);   
    delay(outputValue);                  
  } 
  else {
    digitalWrite(ledPin, LOW);
  }
}

This is the sample code I’d like to stat working with, so please help me to be able to integrate some pots in the setup.
Thanks

use CTRL-T before copying the code it does an autoformat which makes the code more readable!

robtillaart: use CTRL-T before copying the code it does an autoformat which makes the code more readable!

Done! Any suggestions?

I found this: http://starfiretech.wordpress.com/2009/12/08/simple-arduino-midi-controler/

It was working once with being able to use one pot mapped to midi in my dj software. But when I connected the second pot I started to receive invalid values. It looked like it is switching between pot 1 and 2 really quick, so couldn't map the pots. I had it setup with same method as the switches were, running midi-yoke and the processing sketch to convert serial to midi.

After thinking about it for a bit, could I just use any midi code on the arduino, as the software will pick up the values, even though I don't have a hardware midi port on my arduino?

The code is here in case anyone would like to try it, just use the same method in my 1st post with the 2 switches.

// Super Simple Midi Contoler

int val = 0;
int val2 = 0;

void setup()
{
   Serial.begin(31250);       // Default speed of MIDI serial port
}

void loop()
{
   val = analogRead(1)/8;      // Divide by 8 to get range of 0-127 for midi
   MIDI_TX(176,1,val);         // 176 = CC command, 1 = Which Control, val = value read from Potentionmeter
   delay(10);

   val2 = analogRead(0)/8;     // Control point 2
   MIDI_TX(176,2,val2);
   delay(10);

}

void MIDI_TX(unsigned char MESSAGE, unsigned char CONTROL, unsigned char VALUE) //pass values out through standard Midi Command
{
   Serial.print(MESSAGE);
   Serial.print(CONTROL);
   Serial.print(VALUE);
}

That code does work, I used it a while back when I was starting out with Arduino. I don't think it fully complies with the official MIDI specs, meaning it might not work with some devices or programs. I've never used Traktor or Virtual DJ, but I've found Ableton to be really flexible with what it accepts.

I can't really help you with the potentiometers without seeing your schematic... but my guess is that it's because you aren't averaging the incoming analog data. See, all sensors (even simple buttons) don't return a simple "yes" or "no," but rather a numerical value. This is especially relevant to analog sensors like potentiometers, you are adjusting the resistance of the pot by turning it, and then whatever is left passes through and into your analog-in pin on your arduino. The arduino then converts this to a digital value. The problem here, is that the arduino is actually "too sensitive" and it's picking up the little nuances of the current (which is of course fluctuating ever so slightly), causing the digital value to bounce around. So then, it's sending this midi data that's constantly shifting, and ableton is reading that data, thinking that you're moving the knob. Get it?

The way to fix this is to write code that takes that incoming value, compares it to the previous reading, and gives an average. If you use arrays, you can even store a whole bunch of values and get a really accurate reading (using too many, however, will slow things down).

If you want, you can just send MIDI through the built in USB port and use something like hairless (or the processing sketch) to convert the incoming serial data.

There is a MIDI library that makes using midi really easy. I had 8 pot’s hooked up through a 4051 multiplexer in no time sending out midi.
3 output pins for your multiplexer and analog input. You could have more. I did find the map function to be a bit slow sometimes though i think i used divide or bit shift instead. The midi was really easy though notes controllers anything really.

Thank for the reply eVP! Finally I found what I want, and it works great. Didn't experience any lags so far. Going to order some 4051 multiplexers/ demultiplexers and integrate them in the circuit and code. Here is the link to the great find: http://www.instructables.com/id/Arcade-Button-MIDI-Controller/?ALLSTEPS#step1

My initial problem was that I had to use different codes for hardware and software midi. But I think they are the same :)