reading ultrasonic sensor

I’ve made a midi controller which reads a switch and does on of two things depending on a switch:

  1. send a CC midi message value 127 and switches and LED on then the next time it detects movement, it does the opposite, sending value 0, LED off

  2. switches LED on and sends a midi value between 0 and 127 depending on what it senses.

I’ve tried to smooth the reading (it’s not a huge success), since it will control a parameter on the computer and to rule out any values outside 12" of movement.

It’s working, but I’d really value any thoughts about my approach and code, I’m still a relative beginner…

#include <MIDI.h>
MIDI_CREATE_DEFAULT_INSTANCE();

int current;
int ModeState = 0;
int ModeLatch = 0;
int ModeButton = 0;         // variable for reading the pushbutton status
long duration;
int distance;
int variable_CC_val;
int latchedVal;

const int max_distance = 50; // ignore readings ouside this range
const int CC_num = 88;      // CC for on/off
const int CC_num_2 = 89;    // CC for "theremin"
const int maxi = 20;        // max value for hand distance
const int midi_channel = 8;
const int LEDpin = 4 ;
const int switchPin = 5;     // the number of the switch pin
const int trigPin = 3;        // sensor read
const int echoPin = 2;        // sensor send
const int wait = 20;        // delay time
void setup() {
pinMode(9, OUTPUT);  
pinMode(LEDpin, OUTPUT);
pinMode(switchPin, INPUT);
digitalWrite(switchPin, HIGH);
digitalWrite(LEDpin, LOW);
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // for testing
// MIDI.begin(1);  // set up midi
}

void loop() 
  {
  ReadDistance();  
  ModeButton = digitalRead(switchPin);
  if (ModeButton == HIGH)  // latching on/off mode
   {
     Serial.println(ModeButton);
          
    if (ModeState == 0 && distance > maxi)
       {
       ModeState = 1; // hand swipe has started
       }
    if (ModeState == 1 && distance < maxi) // swipe over
       {
       ModeState = 0;  
       if (ModeLatch == 1)
         {
         MIDI.sendControlChange(CC_num,0,midi_channel);
         ModeLatch = 0;
         digitalWrite(LEDpin, HIGH);   // LED on
         }
      else 
         {
         MIDI.sendControlChange(CC_num,127,midi_channel);
         digitalWrite(LEDpin, LOW); // LED off
         ModeLatch = 1;
         }
      }   // end value low
    }
  else // mode 2 - continuous controller mode
    {
     Serial.println(ModeButton);
     digitalWrite(LEDpin, HIGH);   // LED on
     if (distance > max_distance)  // ignore anything above this range
        {
        distance=max_distance;
        }
     variable_CC_val = (distance * 4)-10;
     if (variable_CC_val < 0)  // ignore anything below zero
        {
        variable_CC_val = 0;
        }     
     MIDI.sendControlChange(CC_num,latchedVal,midi_channel);

    }  // end mode 2
     delay(wait);   
} // end main loop


void ReadDistance()
{
  digitalWrite(trigPin, LOW);   // Clears the trigPin
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance= duration*0.034/2;
}

Changes to the data types that you use could save some SRAM and maybe improve readability. Do you need a 2 byte int to hold a boolean state? Do you need a 2 byte int to hold a pin number? Variables that deal with time (duration) should probab!y be unsigned long.