As computing becomes more ubiquitous in our objects, designers need to be more aware of how to design meaningful interactions into electronically enhanced objects. At the University of Washington, a class of junior Interaction Design majors is exploring this question. These pages chronicle their efforts.

Wednesday, May 8, 2013

Randy & Charlie: Vibration Motor with Proximity Sensor

This code is a modified code from a HC-SR04 Tutorial (http://treehouseprojects.ca/ultrasonictutorial/). Using the HC-SR04 sensor to sense the proximity of an object and then activate a vibration motor that vibrates at a rate proportionate to the proximity of the object.

/*

Randy Huynh

This code is a modified code from a HC-SR04 Tutorial 
(http://treehouseprojects.ca/ultrasonictutorial/).
Using the HC-SR04 sensor to sense the proximity of an 
object and then activate a vibration motor that vibrates 
at a rate proportionate to the proximity of the object.

*/
 
//pin which triggers ultrasonic sound
const int pingPin = 13;
 
//pin which delivers time to receive echo using pulseIn()
int inPin = 12;
 
//range in cm which is considered safe to enter, anything
//coming within less than 5 cm triggers red LED
int safeZone = 50;
 
//vibration pin.
int vibrationPin = 9;
 
void setup() {
  // initialize serial communication
  Serial.begin(9600);
}
 
void loop()
{
  //raw duration in milliseconds, cm is the
  //converted amount into a distance
  long duration, cm, strength;
 
  //initializing the pin states
  pinMode(pingPin, OUTPUT);
  pinMode(vibrationPin, OUTPUT); 
 
  //sending the signal, starting with LOW for a clean signal
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
 
  //setting up the input pin, and receiving the duration in
  //microseconds for the sound to bounce off the object infront
  pinMode(inPin, INPUT);
  duration = pulseIn(inPin, HIGH);
 
  // convert the time into a distance
  cm = microsecondsToCentimeters(duration);
 
  //printing the current readings to ther serial display
//  Serial.print(cm);
//  Serial.print("cm");
//  Serial.println();

  strength = (long)centimetersToHertz(cm);
  // Serial.println(strength);
  // Serial.write(strength);
  
  Serial.write((long)centimetersToByte(cm));
  tone(vibrationPin, strength, 100);
  delay(100);
}
 
long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}

float centimetersToByte(long centimeters) {
  if (centimeters < safeZone && centimeters != 0) {
    return (float)(255 - (255 * ((float)centimeters / (float)50)));
  } else {
    return 0;
  }
} 

float centimetersToHertz(long centimeters) {
  if (centimeters < safeZone && centimeters != 0) {
    return (float)(180 - (180 * ((float)centimeters / (float)safeZone)));
  } else {
    return 0;
  }
} 


No comments:

Post a Comment