Question

actually we're working on a project based on Arduino and Processing. To explain basically, we have multiple knock sensors (or piezzos). Our goal is to launch a sound file every time a piezzo is 'knocked' but we encountered some issues about getting data from piezzo. We work on an Arduino sketch and a Processing sketch, but we didn't manage to make a path between the two sketches. So our question is : in the processing sketch, how to get the piezzo's value of the arduino sketch and make it launching a sound file every time it is knocked ?

Arduino Sketch:

// these constants won't change:
const int ledPin = 13; // led connected to digital pin 13
const int knockSensor0 = A0 ; // the piezo is connected to analog pin 0
const int knockSensor1 = A1 ;
const int knockSensor2 = A2 ;
const int knockSensor3 = A3 ;
const int knockSensor4 = A4 ;
const int knockSensor5 = A5 ;

const int threshold = 100; // threshold value to decide when the detected sound is a knock or not

// these variables will change:
int sensorReading = 0; // variable to store the value read from the sensor pin
int ledState = LOW; // variable used to store the last LED status, to toggle the light

void setup() 
{
    pinMode(ledPin, OUTPUT); // declare the ledPin as as OUTPUT
    Serial.begin(9600); // use the serial port
}

void loop() 
{
    Ampoule(A0);
    Ampoule(A1);
    Ampoule(A2);
    Ampoule(A3);
    Ampoule(A4);
    Ampoule(A5);
}

void Ampoule (int input) 
{
    // read the sensor and store it in the variable sensorReading:
    sensorReading = analogRead(input);

    // if the sensor reading is greater than the threshold:
    if (sensorReading > threshold) 
    {
        // toggle the status of the ledPin:
        ledState = !ledState;
        // update the LED pin itself:
        digitalWrite(ledPin, ledState);
        // send the string "Knock!" back to the computer, followed by newline
        Serial.println("PiezoKnock!");
        Serial.write(10);
    }
    else 
    {
        ledState = LOW;
        digitalWrite(ledPin, ledState);
    }
    delay(10); // delay to avoid overloading the serial port buffer
}

Processing Sketch ( an open source example we found which got, in theory, exactly what we're looking for )

/**
* Arduino Sounds
*
* Play WAV or MP3 files when piezo knocks from an Arduino running the
* "PiezoKnock" sketch or when a computer keyboard key is pressed.
*
* Taken from the Minim "trigger" sketch:
*
* This sketch demonstrates how to use the <code>trigger</code> method of an <code>AudioSample</code>. <br />
* <code>AudioSample</code>s can only be triggered, not cue'd and looped
* or anything else you might do with an <code>Playable</code> object. The advantage, however, is that
* an <code>AudioSample</code> can be retriggered while it is still playing, which will cause the sample to
* overlap with itself .
*/

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

String portname = "/dev/tty.usbserial-A4001qa8"; // or "COM8"
Serial port; // Create object from Serial class

AudioSample sounds[];
String sound_names[] =
{
    "cat.wav",
    "fx.mp3",
    "electric_wrench.wav",
    "wehoa.mp3",
    "oriental_gong_2.wav",
    "yipee.wav",
    "car_brake.wav"
    // find more wav or mp3 files and put them in the "data" directory
};

void setup()
{
    size(400, 400);
    background(0);
    stroke(255);
    // always start Minim before you do anything with it
    Minim.start(this);
    Minim.debugOn();
    sounds = new AudioSample[sound_names.length];
    for( int i=0; i< sound_names.length; i++ ) 
    {
        sounds[i] = Minim.loadSample(sound_names[i], 512);
    }

    // Open the port that the board is connected to and use the same speed (19200 bps)
    port = new Serial(this, portname, 19200);
}

void draw()
{
    // do the drawing on events
}

void soundball() 
{
    int r = int(random(sounds.length));
    println("picked sound #"+r);
    sounds[r].trigger(); // play a random sound

    int x = int(random(0,300));
    int y = int(random(0,300));
    fill(240,0,0);
    ellipse(x,y, 40,40);
    fill(30,0,0);
    ellipse(x,y, 8,8);
}

void serialEvent(Serial p) 
{
    char inByte = port.readChar();
    println("received char: "+ inByte);
    if( inByte == '!' ) // '!' is end of "knock!"
    { 
        soundball();
    }
}

void keyPressed() 
{
    if(key == 't') 
    {
        background(40,40,40); // erase screen
    }
    soundball();
}

void stop()
{
    // always close Minim audio classes when you are done with them
    for( int i=0; i<sounds.length; i++ ) 
    {
        sounds[i].close();
    }
    super.stop();
}

Please excuse my poor English !

No correct solution

OTHER TIPS

In your Arduino sketch, the serial port is set with a baud rate of 9600 (Serial.begin(9600)), but in Processing the baud rate is 19200 (port = new Serial(this, portname, 19200)). You will need to change these so that the baud rates match.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top