Go Down

Topic: help phrasing code to have multible buttons and pots (Read 786 times) previous topic - next topic


slightly frustrated lol

Code: [Select]
/* Arduino USB MIDI demo */

#define NOTE_OFF       0x80
#define NOTE_ON        0x90

/* The format of the message to send via serial */
typedef struct {
 uint8_t command;
 uint8_t channel;
 uint8_t data2;
 uint8_t data3;

int ledPin = 11;                //choose the pin for the LED - needs to be (3,5,6,9,10, or 11)
int buttonPin = 2;      //choose the input pin for a pushbutton
int MAX_PINS = sizeof(buttonPin)/2-1;
int potPin = 0;                  //choose the input pin for a potentometer
int buttonVal = 0;                    //variable for reading the button status
int buttonState = 0;            //variable to hold the buttons current state
int bounceCheck = 0;            //variable for debouncing
int potVal = 0;                //variable for reading potentiometer value
int mappedPotVal = 0;          //variable for holding remapped pot value
int prevPotVal = 0;               //variable for storing our prev pot value
int THRESHOLD = 2;            //threshold amount

int channel = 1;
void setup()
 pinMode(ledPin, OUTPUT);      //declare LED as output
 pinMode(buttonPin, INPUT);     //declare pushbutton as input

/* List of notes to play, zero terminated */

void loop()
  buttonVal = digitalRead(buttonPin);     //read input value from button
 delay(10);                              //wait 10ms
 bounceCheck = digitalRead(buttonPin);   //check again
 if(buttonVal == bounceCheck){           //if val is the same then not a bounce
   if (buttonVal == HIGH && buttonState == 1) {   //check if the input is HIGH
     digitalWrite(ledPin, LOW);         //turn LED OFF
     sendmidi(NOTE_OFF, 60, 127); //Note ON (CH 1), middle C, zero velocity turns note off
     buttonState = 0;
   if(buttonVal == LOW && buttonState == 0){
     digitalWrite(ledPin, HIGH);       //turn LED ON
     sendmidi(NOTE_ON, 60, 127); //Note ON (CH 1), middle C, velocity 127
     buttonState = 1;


  potVal = analogRead(potPin);         //read input from potentiometer
  mappedPotVal = map(potVal, 0, 1023, 0, 127);  //map value to 0-127
  if(abs(mappedPotVal - prevPotVal) >= THRESHOLD){
    sendmidi(0xB0, 1, mappedPotVal); //Control Change (Ch 1), Controller 7 - default control for volume.
    digitalWrite(ledPin, HIGH);
    prevPotVal = mappedPotVal;
     digitalWrite(ledPin, LOW);


void sendmidi(int command,int data2, int data3){
 t_midiMsg msg;
 int ind;
 /* Send the notes */

 msg.command = command;   //noteon
 msg.channel = channel;
 msg.data2   = data2;  //note
 msg.data3   = data3; /* Velocity */
 Serial.write((uint8_t *)&msg, sizeof(msg));


this is the code im using ti read the state of 1 pushbutton  with debounce and state change and one pot and they out put midi messages i would like to know if some one could show me how to phrase this code so that i may have multiple buttons and multible pots? my set up us 12 buttons and 4 pots thank you so much


Are you familiar with arrays ?
If not, then it is probably time to read up on them.  Using an array you can hold a series of related values such as a series of button reads.  The advantage of using an array over a separate variable for each button is that you can run through them using a for loop.

Have a look at this for some ideas http://www.adafruit.com/blog/2009/10/20/example-code-for-multi-button-checker-with-debouncing/
Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.


thank you that helped me go to were i was trying to go i have another question though i tryed to do the same with the pots but the out put isnt right
heres the code segment
Code: [Select]
  int potPin[] = {0,1,2};                 //choose the input pin for a potentometer
int potVal[] = {0,0,0};                 //variable for reading potentiometer value
int mappedPotVal[] = {0,0,0};           //variable for holding remapped pot value
int prevPotVal[] = {0,0,0};             //variable for storing our prev pot value
int THRESHOLD = 5;     
int MAX_POT = sizeof(potPin);

    for (int mypots = 0; mypots < MAX_POT - 1; mypots++){

//begin potcode
      potVal[mypots] = analogRead(potPin[mypots]);         //read input from potentiometer
      mappedPotVal[mypots] = map(potVal[mypots], 0, 1023, 0, 127);  //map value to 0-127
      if(abs(mappedPotVal[mypots] - prevPotVal[mypots]) >= THRESHOLD){
        sendmidi(0xB0, mypots, mappedPotVal[mypots]); //Control Change (Ch 1), Controller 7 - default control for volume.
        digitalWrite(ledPin, HIGH); 
        prevPotVal[mypots] = mappedPotVal[mypots];
        digitalWrite(ledPin, LOW);
      // end pot code 


when i run the midi messages the output is constently chainging without any input amung all  3 pots i suspect its this part of the code but i dont know how to fix it

Code: [Select]
  sendmidi(0xB0, mypots, mappedPotVal[mypots]);


Code: [Select]
    for (int mypots = 0; mypots < MAX_POT - 1; mypots++){
What value does MAX_POT have?

Code: [Select]
int MAX_POT = sizeof(potPin);
The int array potPin has 3 elements. Therefore, MAX_POT has a value of 6. My guess is that that is not what you expect.

Code: [Select]
int MAX_POT = sizeof(potPin)/sizeof(potPin[0];
is the correct way to determine the number of elements in an array.

With your code, the loop will iterate while mypots is 0, 1, 2, 3, and 4. These are NOT all valid indexes into the arrays, so you are reading garbage sometimes, and writing outside the bounds of other arrays.

If you change the valuation of MAX_POT, the for loop is still wrong. If MAX_POT is 3, the loop will iterate for mypots equal 0 and 1 (since 2 is not less than 3-1). Again, not the correct (as in not the complete range) of values for the array indexes.
The art of getting good answers lies in asking good questions.

Go Up