Distance Sensor & NeoPixel

For this week, I assembled together a distance sensor with a NeoPixel light display, with the lights extending out the closer the user had his hand to the distance sensor.

I wanted to start working with some equipment/code we used for past projects, in preparation for the final project for TUI.

I wanted to try connecting output through the Serial Monitor to Ableton Live, but couldn’t get that piece to work just yet. I attached the Piezo speaker to make a clicking sound every time an object (etc. my hand) was detected.

Some useful resources for others doing similar things in the class:
MIDI controller output through arduino – http://forum.arduino.cc/index.php/topic,22336.0.html

https://youtu.be/HZ-NZvULZiU

/*
HC-SR04 Ping distance sensor]
VCC to arduino 5v GND to arduino GND
Echo to Arduino pin 13 Trig to Arduino pin 12
Red POS to Arduino pin 11
Green POS to Arduino pin 10
560 ohm resistor to both LED NEG and GRD power rail
More info at: http://goo.gl/kJ8Gl
Original code improvements to the Ping sketch sourced from Trollmaker.com
Some code and wiring inspired by http://en.wikiversity.org/wiki/User:Dstaub/robotcar
*/

#include 

#define trigPin 13
#define echoPin 12
int speakerPin = 7;
 int velocity = 100;//velocity of MIDI notes, must be between 0 and 127
 //higher velocity usually makes MIDI instruments louder
 
 int noteON = 144;//144 = 10010000 in binary, note on command
 int noteOFF = 128;//128 = 10000000 in binary, note off command

#define PIN 6
 
// Parameter 1 = number of pixels in strip
// Parameter 2 = pin number (most are valid)
// Parameter 3 = pixel type flags, add together as needed:
//   NEO_KHZ800  800 KHz bitstream (most NeoPixel products w/WS2812 LEDs)
//   NEO_KHZ400  400 KHz (classic 'v1' (not v2) FLORA pixels, WS2811 drivers)
//   NEO_GRB     Pixels are wired for GRB bitstream (most NeoPixel products)
//   NEO_RGB     Pixels are wired for RGB bitstream (v1 FLORA pixels, not v2)
Adafruit_NeoPixel strip = Adafruit_NeoPixel(60, PIN, NEO_GRB + NEO_KHZ800);

void setup() {
  Serial.begin (31250);
  pinMode(speakerPin, OUTPUT); 
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);

  strip.begin();
  strip.show(); // Initialize all pixels to 'off'
}


//send MIDI message
void MIDImessage(int command, int MIDInote, int MIDIvelocity) {
  Serial.write(command);//send note on or note off command 
  Serial.write(MIDInote);//send pitch data
  Serial.write(MIDIvelocity);//send velocity data
}


void loop() {
  
  long duration, distance;
  digitalWrite(trigPin, LOW);  // Added this line
  delayMicroseconds(2); // Added this line
  digitalWrite(trigPin, HIGH);
//  delayMicroseconds(1000); - Removed this line
  delayMicroseconds(10); // Added this line
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  delay(100);
  if (distance < 43) {  // This is where the LED On/Off happens

    long k = distance % 7;
    long k2 = 6-k; 

  
  
    for( int i=0; i<5; i++ ) {  // play it for 50 cycles
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(1);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(1);
  }
    int j;
    //Serial.println("on stage"); 
    for(j=0;jk-1;j--){
      //Serial.print("off: ");
      //Serial.println(j);
    strip.setPixelColor(j, 0,0,0);    
        
strip.show(); 
    }
     

}
else {
  //Serial.println("external off stage"); 
  int j; 
 for(j=0;j<7;j++){
      //Serial.print("off: ");
      //Serial.println(j);
    strip.setPixelColor(j, 0,0,0);    
        
strip.show(); 
    }
    
};
  
}




 

 

 

 

Leave a Reply