New experiments

A student’s idea prompted me to try this…..I grabbed a cheap little ultrasonic sensor (HC-SR04) and interfaced it with an Arduino, then wrote some code in Max to pull out the serial data and turn it into something musical…..

Here’s the Arduino code:

#define trigPin 12
#define echoPin 13

void setup() {
  Serial.begin (9600);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
}

void loop() {
  int duration, distance;
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(1000);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;
  if (distance >= 800 || distance = 0){
    Serial.println(“Out of range”);
  }
  else {
    Serial.print(distance);
    Serial.println(” cm”);
  }
  delay(100);
}

The Max Patch:

 

And the hardware:

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s