Tutorial.KnockSensor History

Show minor edits - Show changes to markup

April 14, 2013, at 09:10 AM by Scott Fitzgerald -
Added line 27:
 * edited by Scott Fitzgerald 14 April 2013
April 14, 2013, at 09:09 AM by Scott Fitzgerald -
Changed lines 9-10 from:

The code example will capture the knock and if it is stronger than a certain threshold, it will send the string "Knock!" back to the computer over the serial port. In order to see this text you could either use a terminal program, which will read data from the serial port and show it in a window, or make your own program in e.g. Processing. Later in this article we propose a program that works for the software designed by Reas and Fry.

to:

The code example will capture the knock and if it is stronger than a certain threshold, it will send the string "Knock!" back to the computer over the serial port. In order to see this text you could either use the Arduino's serial monitor, or make your own program in e.g. Processing. Later in this article we propose a program that works for the software designed by Reas and Fry.

Changed line 15 from:
 [=
to:
Changed line 37 from:
 beginSerial(9600);
to:
 Serial.begin(9600);
Changed lines 45-47 from:
    printString("Knock!");
    printByte(10);
    printByte(13);
to:
    Serial.println("Knock!");
Changed lines 50-51 from:

=]

to:
November 23, 2005, at 01:30 AM by 81.236.128.105 -
Changed lines 9-10 from:

The code example will capture the knock and if it is stronger than a certain threshold, it will send the string "Knock!" back to the computer over the serial port. In order to see this text you could either use a terminal program, which will read data from the serial port and show it in a window, or make your own program in e.g. http://www.processing.org?. Later in this article we propose a program that works for the software designed by Reas and Fry.

to:

The code example will capture the knock and if it is stronger than a certain threshold, it will send the string "Knock!" back to the computer over the serial port. In order to see this text you could either use a terminal program, which will read data from the serial port and show it in a window, or make your own program in e.g. Processing. Later in this article we propose a program that works for the software designed by Reas and Fry.

Changed line 53 from:

Representing the Knock in Processing

to:

Representing the Knock in Processing

November 23, 2005, at 01:29 AM by 81.236.128.105 -
Added lines 9-10:

The code example will capture the knock and if it is stronger than a certain threshold, it will send the string "Knock!" back to the computer over the serial port. In order to see this text you could either use a terminal program, which will read data from the serial port and show it in a window, or make your own program in e.g. http://www.processing.org?. Later in this article we propose a program that works for the software designed by Reas and Fry.

Added lines 51-117:

=]

Representing the Knock in Processing

If, e.g. we would like to capture this "knock" from the Arduino board, we have to look into how the information is transferred from the board over the serial port. First we see that whenever there is a knock bigger that the threshold, the program is printing (thus sending) "Knock!" over the serial port. Directly after sends the byte 10, what stands for EOLN or End Of LiNe, and byte 13, or CR - Carriage Return. Those two symbols will be useful to determine when the message sent by the board is over. Once that happens, the processing program will toggle the background color of the screen and print out "Knock!" in the command line.

 [=

// Knock In // by David Cuartielles <http://www.0j0.org> // based on Analog In by Josh Nimoy <http://itp.jtnimoy.com>

// Reads a value from the serial port and makes the background // color toggle when there is a knock on a piezo used as a knock // sensor. // Running this example requires you have an Arduino board // as peripheral hardware sending values and adding an EOLN + CR // in the end. More information can be found on the Arduino // pages: http://www.arduino.cc

// Created 23 November 2005 // Updated 23 November 2005

import processing.serial.*;

String buff = ""; int val = 0; int NEWLINE = 10;

Serial port;

void setup() {

  size(200, 200);

  // Open your serial port
  port = new Serial(this, "COMXX", 9600);  // <-- SUBSTITUTE COMXX with your serial port name!!

}

void draw() {

  // Process each one of the serial port events
  while (port.available() > 0) {
    serialEvent(port.read());
  }
  background(val);

}

void serialEvent(int serial) {

  if(serial != NEWLINE) { 
    buff += char(serial);
  } else {
    buff = buff.substring(1, buff.length()-1);
    // Capture the string and print it to the commandline
    // we have to take from position 1 because 
    // the Arduino sketch sends EOLN (10) and CR (13)
    if (val == 0) {
      val = 255;
    } else {
      val = 0;
    }
    println(buff);
    // Clear the value of "buff"
    buff = "";
   }

}

October 18, 2005, at 01:21 AM by 195.178.229.25 -
Added lines 1-49:

Knock Sensor

Here we use a Piezo element to detect sound, what will allow us to use it as a knock sensor. We are taking advantage of the processors capability to read analog signals through its ADC - analog to digital converter. These converters read a voltage value and transform it into a value encoded digitally. In the case of the Arduino boards, we transform the voltage into a value in the range 0..1024. 0 represents 0volts, while 1024 represents 5volts at the input of one of the six analog pins.

A Piezo is nothing but an electronic device that can both be used to play tones and to detect tones. In our example we are plugging the Piezo on the analog input pin number 0, that supports the functionality of reading a value between 0 and 5volts, and not just a plain HIGH or LOW.

The other thing to remember is that Piezos have polarity, commercial devices are usually having a red and a black wires indicating how to plug it to the board. We connect the black one to ground and the red one to the input. We also have to connect a resistor in the range of the Megaohms in parallel to the Piezo element; in the example we have plugged it directly in the female connectors. Sometimes it is possible to acquire Piezo elements without a plastic housing, then they will just look like a metallic disc and are easier to use as input sensors.

http://static.flickr.com/28/53535494_73f63436cb.jpg

Example of connection of a Piezo to analog pin 0 with a resistor

 
/* Knock Sensor
 * ----------------
 *
 * Program using a Piezo element as if it was a knock sensor.
 *
 * We have to basically listen to an analog pin and detect 
 * if the signal goes over a certain threshold. It writes
 * "knock" to the serial port if the Threshold is crossed,
 * and toggles the LED on pin 13.
 *
 * (cleft) 2005 D. Cuartielles for K3
 */

int ledPin = 13;
int knockSensor = 0;               
byte val = 0;
int statePin = LOW;
int THRESHOLD = 100;

void setup() {
 pinMode(ledPin, OUTPUT); 
 beginSerial(9600);
}

void loop() {
  val = analogRead(knockSensor);     
  if (val >= THRESHOLD) {
    statePin = !statePin;
    digitalWrite(ledPin, statePin);
    printString("Knock!");
    printByte(10);
    printByte(13);
  }
  delay(100);  // we have to make a delay to avoid overloading the serial port
}

Share