Étape 4: Le Code
Voici le beau morceau de code simple basé sur un exemple pris en 2009 par starfiretech. Les ajustements clés que j’ai fait ce code ont été pour vérifier les nouvelles valeurs contre les anciennes valeurs, contente d’envoyer les données midi lorsqu’une modification est détectée ; et l’ajout d’autre un petit délai pour supprimer toute légères variations, données en valeur lorsque le pot n’est pas être touché, légèrement touché ou obtient un peu frapper. Avant que j’ai ajouté ceci je recevais quelques variations (de + /-1) en valeur quand ils sont légèrement touchés dont le retard est utile pour enlever.
int val = 0; //Our initial pot values. We need one for the first value and a second to test if there has been a change made. This needs to be done for all 3 pots.int lastVal = 0; int val2 = 0; int lastVal2 = 0; int val3 = 0; int lastVal3 = 0;void setup() { Serial.begin(9600); // Set the speed of the midi port to the same as we will be using in the Hairless Midi software }void loop() { val = analogRead(0)/8; // Divide by 8 to get range of 0-127 for midi if (val != lastVal) // If the value does not = the last value the following command is made. This is because the pot has been turned. Otherwise the pot remains the same and no midi message is output. { MIDImessage(176,1,val);} // 176 = CC command (channel 1 control change), 1 = Which Control, val = value read from Potentionmeter 1 NOTE THIS SAYS VAL not VA1 (lowercase of course) lastVal = val; val2 = analogRead(1)/8; // Divide by 8 to get range of 0-127 for midi if (val2 != lastVal2) { MIDImessage(176,2,val2);} // 176 = CC command, 2 = Which Control, val = value read from Potentionmeter 2 lastVal2 = val2; val3 = analogRead(2)/8; // Divide by 8 to get range of 0-127 for midi if (val3 != lastVal3) { MIDImessage(176,3,val3);} // 176 = CC command, 3 = Which Control, val = value read from Potentionmeter 3 lastVal3 = val3; delay(10); //here we add a short delay to help prevent slight fluctuations, knocks on the pots etc. Adding this helped to prevent my pots from jumpin up or down a value when slightly touched or knocked. }void MIDImessage(byte command, byte data1, byte data2) //pass values out through standard Midi Command { Serial.write(command); Serial.write(data1); Serial.write(data2); }