Ribbon Pot Sample & Hold via MIDI

Hi,

I am hoping someone will be able to point me in the right direction to work out what it is I am getting wrong with my code.

I need to read a value from a ribbon pot and hold it until a push button is activated and then send the value (mapped to a MIDI note number) to a synth via MIDI.

The code reads the value and sends as expected but the value is not retained when the button is released and when it is pressed again (without pressing ribbon pot) it seems to ramp slowly either up or down to what seems like a middle (of the pot) value.

What I would like is for the ribbon value to remain constant during successive button pushes and only change when a new value is sent from the ribbon pot.

I have searched previous posts for some clues and have read -

but it didn’t help me a lot as I am a novice coder. I didn’t understand the use of >> 2 in Grumpy_Mike’s reply.

Any help or suggestions very much appreciated.

#include <MIDI.h>


const int analogInPin = A3;  // Analog input pin that the ribbon potentiometer is attached to
const int buttonPin = 2;     // the number of the noteplay button pin

int noteVal = 0;             // MIDI note number for output
int sensorValue1 = 0;        // Value from ribbon pot
int buttonState = 0;         // variable for reading the pushbutton status

byte Channel;                // MIDI bytes
byte NoteNumber;
byte Velocity;

void setup() {
  Serial.begin(31250);       //  Set MIDI baud rate
  pinMode(buttonPin, INPUT); // initialise the pushbutton pin as an input
}
void loop() {
  
  sensorValue1 = analogRead(analogInPin);              
  noteVal = map(sensorValue1, 0, 1023, 44, 75);  
  // Ribbon pot range mapped to MIDI note range 44 - 75

 // read the state of the pushbutton value
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH
  if (buttonState == HIGH
  ) {     
    // play note on    
   MIDI.sendNoteOn(noteVal,120,1);    
   
  } 
  else {
    // play note off
   MIDI.sendNoteOff(noteVal,0,1); 
  } 
}
  if (buttonState == HIGH
  ) {

Can you find a single Arduino example, or any code online that has not been ridiculed to death, where the ) is not on the same line as the rest of the if statement?

You read the pot state on every pass through loop. That is wrong. You should ONLY read the pot state if the switch is pressed.

What I would like is for the ribbon value to remain constant during successive button pushes and only change when a new value is sent from the ribbon pot.

I wish to win the lottery, without buying a ticket, too. But what I want is not possible. Neither is what you want. the pot has no way of returning one value if pressed and another if not. You need to rethink what you want to do.

PaulS,

Must make you feel mighty superior pointing out the typos of people who have been coding for a couple of weeks.

The weather is getting nice now, why don't you get out for a run and work off some off that pent up aggression and self loathing :slight_smile: ?

Somewhere in all that nastiness is your desire to help another person, so I thank you for that.

I didn't understand the use of >> 2 in Grumpy_Mike's reply

It means shift to the right.
If you shift to the right one place it is the same as dividing by two.

If you shift to the right two places ( which was the example ) it is the same as dividing by four.
Shifting is an easier thing for the processor to do than dividing.