Piezo!

kevinswelsen's picture

I ran totally out of inspiration today, so my work is kind of simple. I use a potentiometer to make a circle on the screen bigger or smaller. Sound is used to give auditory feedback on the size of the circle.

Arduino code:

 

int potPin = 0;    
int speakerPin = 7;
 
int val = 0;
 
void setup() {
  pinMode(speakerPin, OUTPUT); 
  Serial.begin(9600);
  Serial.println("ready");
}
 
void loop() {
  digitalWrite(speakerPin, LOW);
  
  val = analogRead(potPin);    
  val = val*2;                
 
   
  for( int i=0; i<500; i++ ) { 
    Serial.println(val); 
    digitalWrite(speakerPin, HIGH);
    delayMicroseconds(val);
    digitalWrite(speakerPin, LOW);
    delayMicroseconds(val);
  }
}
 
Processing code:
import processing.serial.*;
 
String portname = "COM3"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
int waarde;
 
  
void setup() {
  size(500,500);
  background(255);
  smooth();  
  port = new Serial(this, portname, 9600);  
}
 
void draw() {
  // Use those variables to draw an ellipse
  background(255);
  stroke(0);
  fill(100,150,200,200);  // (Remember, the fourth argument for a color is transparency.
  ellipse(250,250,waarde/8,waarde/8);  
}
 
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {   
    waarde = int(buf);
    println("val="+waarde);
    buf = "";
  }
}
 
Piezo_0.JPG
0
Your rating: None