MIDI Controller über HIDUINO --->Aus dem Poti ein MIDI Signal

Hi,
Ich möchte mit einen MIDI Controller für den PC bauen, allerdings ist das auch gleichzeitig mein erstes Arduino Projekt ^^

Als Testaufbau habe ich einen Taster und einen Poti mit jeweils einer Led auf einem Steckbrett aufgebaut. Die neue Firmware Hiduino ist auch schon drauf.
Über den Taster kann ich auch schon ein Midi Signal senden welches in der Software erkannt wird. Nur habe ich keine Ahnung wie der Programmcode für den Poti Aussehen muss das dieser auch ein Midi Signal an den Rechner sendet.

Der Code sieht zurzeit so aus:

#include <MIDI.h>


const int tasterPin = 24;
int tasterState = 0;

const int ledTasterPin = 13;
const int ledPotiPin = 12; 

const int potiPin= 6;
int potiState = 0;

int loga[64] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,
18,20,22,25,28,30,33,36,39,42,46,49,53,56,60,64,68,
72,77,81,86,90,95,100,105,110,116,121,127,132,138,
144,150,156,163,169,176,182,189,196,203,210,218,
225,233,240,248,255};

void setup () {
  pinMode (ledTasterPin, OUTPUT);
  pinMode (ledPotiPin, OUTPUT) ; 
  pinMode (tasterPin, INPUT) ; 
  MIDI.begin (4);
    
}

void loop (){
  tasterState = digitalRead (tasterPin) ; 
  potiState = analogRead (potiPin) ;
  potiState = potiState / 16 ;
  analogWrite ( ledPotiPin, loga[potiState]);
  
  if (tasterState == HIGH) {
   digitalWrite (ledTasterPin, HIGH); 
   MIDI.sendNoteOn(1,127,4);
  }
 else {
   digitalWrite (ledTasterPin, LOW) ;
   MIDI.sendNoteOff(1,127,4);
 }
}

Soo hab jetzt mich ne weile dran versucht mein Code sieht jetzt so aus:

// Midi Controller
// By ...
// Edit: 2013.5.31

#include <MIDI.h>

const int midichannel = 4; 


const int tasterPin = 24;
int tasterState = 0;

const int ledTasterPin = 13;
const int ledPotiPin = 12; 

const int potiPin= 6;
int potiState = 0;
int potiwert = 0  ;
int vergleicher = 0; 



int loga[64] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,
18,20,22,25,28,30,33,36,39,42,46,49,53,56,60,64,68,
72,77,81,86,90,95,100,105,110,116,121,127,132,138,
144,150,156,163,169,176,182,189,196,203,210,218,
225,233,240,248,255};

void setup () {
  pinMode (ledTasterPin, OUTPUT);
  pinMode (ledPotiPin, OUTPUT) ; 
  pinMode (tasterPin, INPUT) ;
  pinMode (potiPin, INPUT) ; 
  MIDI.begin (4);
  
  
}

void loop (){
  tasterState = digitalRead (tasterPin) ; 
  potiState = analogRead (potiPin) ;
  
  potiState = potiState / 16 ;
  analogWrite ( ledPotiPin, loga[potiState]);
  
  potiwert = map( potiState, 0 , 1023, 0, 127);
  
  if ( vergleicher != potiwert ) {
  MIDI.sendNoteOn(2 ,potiwert ,midichannel);
  
  vergleicher = potiwert; 
  } 
  else {
    MIDI.sendNoteOff(2 ,potiwert ,midichannel);
    vergleicher = potiwert;
  }
  
 
  if (tasterState == HIGH) {
   digitalWrite (ledTasterPin, HIGH); 
   MIDI.sendNoteOn(1,127,midichannel);
   
  }
 else {
   digitalWrite (ledTasterPin, LOW) ;
   MIDI.sendNoteOff(1,127,midichannel);
   
 }
}

Dummerweise hab ich jetzt ne Endlosschleife und kann das so nicht mappen sieht jemand den Fehler ?

Das mit der Endlosschleife versteh ich nicht ganz. Aber der Mapping-Fehler liegt vermutlich daran:

potiState = potiState / 16 ;

analogWrite ( ledPotiPin, loga[potiState]);

potiwert = map( potiState, 0 , 1023, 0, 127);

potiState hat einen Wert zwischen 0 und 63, in map wird aber ein Wert von 0-1023 erwartet.

Hi,
hab es jetzt hinbekommen :smiley: :smiley:
@Lukars
Ja das war ein Fehler hat danach leider auch nicht ganz so gut funktioniert :S

In der aktuellen Version ist die eine IF-Abfrage zu viel aber in er Praxis Ist das Schaltverhalten des Taster deutlich besser :slight_smile:
Jetzt alles in ein Array verpackt und auf die bestellten Taster & Fader warten
Der Code:

// Midi Controller
// By ...
// Edit: 2013.5.31

#include <MIDI.h>

const int midichannel = 4; 


const int tasterPin = 24;
int tasterState = 0;

const int ledTasterPin = 13;
const int ledPotiPin = 12; 

const int potiPin= 6;
int potiState = 0;
byte potiwert = 0  ;
byte vergleicherPoti = 0; 
int vergleicherTaster = 0;



int loga[64] = {
  0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,16,
  18,20,22,25,28,30,33,36,39,42,46,49,53,56,60,64,68,
  72,77,81,86,90,95,100,105,110,116,121,127,132,138,
  144,150,156,163,169,176,182,189,196,203,210,218,
  225,233,240,248,255};

void setup () {
  pinMode (ledTasterPin, OUTPUT);
  pinMode (ledPotiPin, OUTPUT) ; 
  pinMode (tasterPin, INPUT) ;
  pinMode (potiPin, INPUT) ; 
  MIDI.begin (4);


}

void loop (){
  tasterState = digitalRead (tasterPin) ; 
  potiState = analogRead (potiPin) ;

  int ledglow1 = potiState / 16 ;
  analogWrite ( ledPotiPin, loga[ledglow1]);

  potiwert = map( potiState, 0 , 1023, 0, 127);

  if ( vergleicherPoti != potiwert ) {
    MIDI.sendNoteOn(0 ,potiwert ,midichannel);

    vergleicherPoti = potiwert; 
  } 
  else {
  }

  if ( vergleicherTaster != tasterState) { 
    if (tasterState == HIGH) {
      digitalWrite (ledTasterPin, HIGH); 
      MIDI.sendNoteOn(1,127,midichannel);


    }
    else {
      digitalWrite (ledTasterPin, LOW) ;
    }
    vergleicherTaster = tasterState ;
  }
  else {
  }
}