Lab - Sensing

Description

A lot of people are tapping a table when they are listening to music. They usually try to convert rhythm to something tangible such as the movement of one's fingers or the sound of tapping actions. Using this action, this program visualizes the tapping actions into a music note. When a user taps the force sensor, it brings up a music note on the screen. After a certain number of music notes appears, it removes all the music notes.

Code

 

import processing.serial.*;
 
PImage img;
 
// Change this to the portname your Arduino board
String portname = "/dev/cu.usbmodemfa131"; // or "COM5"
Serial port;
String buf="";
int cr = 13;  // ASCII return   == 13
int lf = 10;  // ASCII linefeed == 10
int num_rhy = 0;
int delay_rhy = 0;
 
void setup() {
  size(640,400);
  img = loadImage("test.jpg");
  frameRate(60);
  smooth();
  background(255);
  noStroke();
  port = new Serial(this, portname, 9600); 
}
 
void draw() {
}
void keyPressed() {
  if(key == ' ') {
    background(40,40,40);  // erase screen
  }
  else {
    int x = int(random(0,width));
    int y = int(random(0,height));
    drawball(x,y, 50);
  }
}
// draw balls
void drawball(int x, int y, int r) {
 
}
// called whenever serial data arrives
void serialEvent(Serial p) {
  int c = port.read();
  if (c != lf && c != cr) {
    buf += char(c);
  }
  if (c == lf) {
    int val = int(buf);
    println("val="+val); 
    if(val > 300 && delay_rhy <= 0){
      int x = int(random(0,width));
      int y = int(random(0,height));
      image(img, x, y, 50, 50);
      num_rhy += 1;
      delay_rhy = 20; //20 miliseconds delay
    }
 
    if(num_rhy > 30){
      background(255);   // erase screen      
      num_rhy = 0;      
    }
 
    if(delay_rhy > 0){
       delay_rhy--; 
    }
 
    buf = "";
  }
}
 

Photos

 
A screenshot and photo of the mechanical part are attached.
Screenshot of the Programming Part
Photo of the Mechanical Part
0
Your rating: None