Remembering a previous value?

I'm having a little problem and I'm hoping someone can help!

What I'm trying to do is create an electric melodeon (like an accordion). I need to take a pressure value from between a set of bellows (which I have done) and compare it to an atmospheric pressure value (which I also have done).

Then I want to create a MIDI note depending on this pressure value. Essentially, if a button is being pressed, I want one note if the pressure value is lower than the last pressure value, and another different note if it's higher.

I'm having trouble getting my Arduino to remember the previous pressure value. Is there a technique to achieve this?

Here's my code [there are also bits of code for a key matrix, but ignore them :wink: ]:

int analogValue1 = 0;        // value read from the pot
int differenceBob = 0;
int oldBob = 0;
int Bob = 0;

void setup() {
  Serial.begin(31250); // opens serial port, sets data rate to 9600 bps
  pinMode(1, OUTPUT);    // 
  pinMode(2, OUTPUT);    //
  pinMode(3, OUTPUT);    // 
  pinMode(4, OUTPUT);    // 
  pinMode(5, OUTPUT);    //
  pinMode(6, OUTPUT);   //
  pinMode(7, INPUT);    // 
  pinMode(8, INPUT);    //
  pinMode(9, INPUT);    // 
  pinMode(10, INPUT);    //
  pinMode(11, INPUT);   //
  pinMode(12, INPUT);   //
}



void loop() {
  
  Bob = analogRead(A0) / 6;
  oldBob = Bob;
  
  delay (10);
  
  digitalWrite(2, HIGH);
  digitalWrite(3, LOW);
  digitalWrite(4, LOW);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  delay(5);
  
 if (Bob < oldBob); {
  if (digitalRead(7) == HIGH) {
    SendMidi (0x90, 40, Bob);
  }
  if (digitalRead(7) == LOW) {
    SendMidi (0x80, 0x18, Bob);
    }}
 if (Bob > oldBob); {
    if (digitalRead(7) == HIGH) {
    SendMidi (0x90, 45, Bob);
  }
  if (digitalRead(7) == LOW) {
    SendMidi (0x80, 0x18, Bob);
    }}
   
  }

void SendMidi (int cmd, int value, int vel) {
  Serial.write (cmd);
  Serial.write (value);
  Serial.write (vel);
}

I'm having trouble getting my Arduino to remember the previous pressure value. Is there a technique to achieve this?

Store it in a global or static variable called "previousPressureValue"

I think this:

Bob = analogRead(A0) / 6;
oldBob = Bob;

Was probably intended to be this:

oldBob = Bob;
Bob = analogRead(A0) / 6;

Otherwise all your comparisons below are pointless. Bob & oldBob will always be the same. Unless it's an acronym I'm missing, Bob is a pretty poor name for a variable - gives no idea what it's for.

I'm having trouble getting my Arduino to remember the previous pressure value. Is there a technique to achieve this?

Hi,
the usual technique is:

   // 1) Acquire the current value.
   Bob = analogRead(A0) / 6;
   
   // 2) Perform computation using current value and OLD value.
   /// ...
   
   // 3) Update the OLD value.
   oldBob = Bob;

Also, your use of semi-colons after the if statements is going to break the intended logic:

Wrong:

 if (Bob < oldBob); {

Right:

 if (Bob < oldBob) {