Pages: [1]   Go Down
Author Topic: help phrasing code to have multible buttons and pots  (Read 317 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

slightly frustrated lol


Code:
/* 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;
}
t_midiMsg;




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
  
  Serial.begin(115200);
  delay(200);
}

/* 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;
   }
   else{
      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
Logged

East Anglia (UK)
Offline Offline
Faraday Member
**
Karma: 89
Posts: 3487
May all of your blinks be without delay()
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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/
Logged

Please do not send me PMs asking for help.  Post in the forum then everyone will benefit from seeing the questions and answers.

Offline Offline
Jr. Member
**
Karma: 1
Posts: 71
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
  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];
      }
      else{
        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:
  sendmidi(0xB0, mypots, mappedPotVal[mypots]);
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 549
Posts: 46113
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
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:
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.
Logged

Pages: [1]   Go Up
Jump to: