Introduction: Sound-producing Vending Machine

Sound-producing vending machine (by Evaline, Laurens, Glen & Sven) - CMD2
Arduino-powered project

Video:

https://vimeo.com/52416744

(note: We've edited the sounds over the actual video. The reason being was that during the recording and checking our clips we couldn't hear the actual sounds while they were there. When we rendered the video, you could hear the actual sounds that were activated by using the vending machine while we thought they weren't recorded by the camera. So, that's why you hear the sounds twice! Seemed that the camera *did* record the sounds after all! :) )


Materials required;
- SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
- Arduino jumper wires (long enough, else you’ll need to solder wires together)
- Tape
- A vending machine!
- Some speakers (or just use your laptop to play sound)

Tools required;
- Arduino (+ Processing)
- Basic Arduino & Processing knowledge
- Chances are you need to solder if your wires aren’t long enough
- Sounds!

Process;

1. First of all, find a suitable location. Remember that you need to hide your laptop, your Arduino and eventually your speakers out of sight.

2. You’ll need to place/stick the ultrasonic sensor in such a way that it’ll trigger a change of data as people use the vending machine. Now check if the wires are long enough from your sensor to the spot you’ll hide your laptop, Arduino, etc. If not, solder the wires until they are long enough.

3. Find the sounds you want to play when people trigger the code through the sensor.  MP3-format is the easiest to use. Found the sounds you want? Start a new Processing sketch, save the sketch and paste your sounds in the corresponding folder. See the Processing code in the ‘raw code’ section. Replace the .mp3 file names with your mp3 file names. Eventually, delete or add sounds as you please.

4. Copy and paste the Arduino code and connect everything to each other. See if you receive data in your Processing from Arduino. Make sure everything works! Something doesn’t work? Try and find the mistake step by step. Does your Processing receive any values? What kind of values? Make the Arduino print your values before writing them, etc.

(Chances are code has to be tweaked a little to make things work for you!)

5. Alright! Time to finetune. Start with placing your tools (sensor, arduino, laptop, speakers) where they should be. Before you tape the wires, etc. Check (by testing), what data you receive when people use the vending machine. Tweak your code in such a way that it responds to the change of data. This is usually the tricky part!

6. Tape up the wires, hide everything, run the code and enjoy!

7. Program stuck? Try restarting Processing or resetting your Arduino.

Raw code;

ARDUINO CODE --

// I2C SRF10 or SRF08 Devantech Ultrasonic Ranger Finder
// by Nicholas Zambetti
// and James Tichenor

// Demonstrates use of the Wire library reading data from the
// Devantech Utrasonic Rangers SFR08 and SFR10

// Created 29 April 2006

// This example code is in the public domain.


#include

void setup()
{
Wire.begin(); // join i2c bus (address optional for master)
Serial.begin(9600); // start serial communication at 9600bps
}

int reading = 0;

void loop()
{
int data = 0;
//int oldAddress = 0x70; // I2C gebruikt enkel 7 bit adressen (de MSB) dus 0xE0 wordt 0x70
//int newAddress = 0xE2; // is consistent with address 2 --> 0xE4
int newAddress = 0x71; // only 7-bits are used --> 0x72
boolean hold = false;

//changeAddress(oldAddress, newAddress);

while(1) {
data = readData(newAddress);
if(data!=0) {
//Serial.println(data);
if(data >100) {
Serial.write("e");


}
else if(data < 90 ) {
Serial.write("f");
}
}
else {
//Serial.write("0");
}
delay(100); // wait a bit since people have to read the output
//delay(1000);
}

}

// The following code changes the address of a Devantech Ultrasonic Range Finder (SRF10 or SRF08)
// usage: changeAddress(0x70, 0xE6);

void changeAddress(int oldAddress, int newAddress)
{
Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(byte(0xA0));
Wire.endTransmission();

Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(byte(0xAA));
Wire.endTransmission();

Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(byte(0xA5));
Wire.endTransmission();

Wire.beginTransmission(oldAddress);
Wire.write(byte(0x00));
Wire.write(newAddress);
Wire.endTransmission();
}

// The following code is to read the sensor data
int readData(int address) {
int reading = 0;

// step 1: instruct sensor to read echoes
Wire.beginTransmission(address); // transmit to device #112 (0x70)
// the address specified in the datasheet is 224 (0xE0)
// but i2c adressing uses the high 7 bits so it's 112
Wire.write(byte(0x00)); // sets register pointer to the command register (0x00)
Wire.write(byte(0x51)); // command sensor to measure in "centimeters" (0x51)
// use 0x50 for inches
// use 0x51 for centimeters
// use 0x52 for ping microseconds
Wire.endTransmission(); // stop transmitting

// step 2: wait for readings to happen
delay(70); // datasheet suggests at least 65 milliseconds

// step 3: instruct sensor to return a particular echo reading
Wire.beginTransmission(address); // transmit to device #112
Wire.write(byte(0x02)); // sets register pointer to echo #1 register (0x02)
Wire.endTransmission(); // stop transmitting

// step 4: request reading from sensor
Wire.requestFrom(address, 2); // request 2 bytes from slave device #112

// step 5: receive reading from sensor
if(2 <= Wire.available()) // if two bytes were received
{
reading = Wire.read(); // receive high byte (overwrites previous reading)
reading = reading << 8; // shift high byte to be high 8 bits
reading |= Wire.read(); // receive low byte as lower 8 bits
return reading;
}
return 0;
}


PROCESSING CODE –

import processing.serial.*;
import ddf.minim.*;

Minim minim;
AudioPlayer in;
AudioPlayer in2;
AudioPlayer in3;
AudioPlayer in4;
AudioPlayer in5;

Serial myPort;
Boolean hand = false;

int r;


void setup() {
  size(800, 600);
  minim = new Minim(this);
  in = minim.loadFile("god.mp3");
  in2 = minim.loadFile("applaus.mp3");
  in3 = minim.loadFile("babygelach.mp3");
  in4 = minim.loadFile("scheet.mp3");
  in5 = minim.loadFile("muntje.mp3");

  println(Serial.list());
  String portName = Serial.list()[6];
  myPort = new Serial(this, portName, 9600);
  background(0);
  r = 0;
}


void serialEvent (Serial myPort) {

  int inByte = myPort.read();

  println(inByte);

  if (inByte == 102 && hand == false) {

    //   for (int i = 0; i < 9000; i++) {
    int r = int(random(1, 4));
    println(r);
    hand = true;
    println(hand);
    // }

    if (r==1) {
      in.play();
    }
    else if (r==2) {
      in2.play();
    }
    else if (r==3) {
      in3.play();
    }
    else if (r==4) {
      in4.play();
    }
    else {

      in5.play();
    }
  }
if (inByte == 101){
hand = false;
println(hand);
}

}

void draw() {
}

void stop()
{
  in.close();
  minim.stop();

  super.stop();
}


DIY Audio

Participated in the
DIY Audio