Lado do Arduino
Sketch
// Serial Call and Response
// by Tom Igoe
// Language: Wiring/Arduino
// This program sends an ASCII A (byte of value 65) on startup
// and repeats that until it gets some data in.
// Then it waits for a byte in the serial port, and
// sends three sensor values whenever it gets a byte in.
// Thanks to Greg Shakar for the improvements
// Created 26 Sept. 2005
// Updated 18 April 2008
// Modified by Solucionática 24 July 2013.
// Be carefull with the commented lines below.
#include "Ultrasonic.h"
#include <LiquidCrystal.h>
#include <Servo.h>
int firstSensor = 0; // first analog sensor
int secondSensor = 0; // second analog sensor
int thirdSensor = 0; // digital sensor
int inByte = 0; // incoming serial byte
int pos=0;
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
Ultrasonic ultrasonic(10,9);
Servo myservo; // create servo object to control a servo
// a maximum of eight servo objects can be created
void setup()
{
lcd.begin(16, 2);
lcd.print("testing...");
delay(50);
// start serial port at 9600 bps:
myservo.attach(7); // attaches the servo on pin 9 to the servo object
Serial.begin(57600);
//myservo.write(90);
//establishContact(); // send a byte to establish contact //until Processing responds
}
void loop()
{
// lcd.clear();
//lcd.setCursor(0, 0);
// delay(200);
//lcd.print(ultrasonic.Ranging(CM));
//lcd.print("cm");
//lcd.clear();
//delay(30);
//while (ultrasonic.Ranging(CM)<100) {
//
myservo.write(pos);
delay(180);
//}
//delay(15);
//lcd.setCursor(0,0);
//lcd.print(ultrasonic.Ranging(CM));
//lcd.setCursor(0, 1);
// lcd.write("Alvo detectado!");
//delay(50);
//tone(8,850-ultrasonic.Ranging(CM),50);
//pos+=1;
//if (pos>180) {
// pos=-1;
//}
// }
//else if (ultrasonic.Ranging(CM)>51)
//{
// lcd.clear();
//lcd.setCursor(0,0);
//lcd.write("Caminho livre");
//delay(30);
// pos+=1;
// if (pos>180) {
// pos=-1;
// }
// delay(20);
Serial.write(ultrasonic.Ranging(CM));
//delay(50);
Serial.write(pos);
//Serial.write(";");
pos+=5;
delay(30);
if (pos>170){
pos=0;
}
}
//pos+=1;
// myservo.write(pos);
//lcd.clear();
//lcd.setCursor(0, 0);
// delay(30);
//lcd.print(ultrasonic.Ranging(CM));
// Serial.println(ultrasonic.Ranging(CM));
// Serial.write(";");
// Serial.println(pos);
// if (pos>180) {
// pos=-1;
// }
//}
Lado do Processing
Sketch
/**
* Simple Read
*
* Read data from the serial port and change the color of a rectangle
* when a switch connected to a Wiring or Arduino board is pressed and released.
* This example works with the Wiring / Arduino program that follows below.
* Modified by Solucionática on March 25th.
*/
import processing.serial.*;
import ddf.minim.*;
import ddf.minim.ugens.*;
import ddf.minim.signals.*;
Minim minim;
AudioOutput out;
float freq ;
SineWave sine = new SineWave(600,5,44100);
Serial myPort; // Create object from Serial class
int[] serialInArray = new int[2];
int serialCount = 0;
int dist,pos;//Dados recebidos da porta serial
float x,y; //vetor
void setup()
{
size(512, 522);
// I know that the first port in the serial list on my mac
// is always my FTDI adaptor, so I open Serial.list()[0].
// On Windows machines, this generally opens COM1.
// Open whatever port is the one you're using.
String portName = Serial.list()[0];
myPort = new Serial(this, portName, 57600);
minim = new Minim(this);
out = minim.getLineOut( Minim.MONO, 2048 );
sine.portamento(200);//a variação de uma frequência para outra é suave.
out.addSignal(sine);//gera a senóide na saída de áudio.
}
void draw() {
stroke(153);
background(47);
fill(122);
//ellipse(256,256,100,100);
stroke(pos);
ellipse(256,256,200,200);
fill(dist);
// Draw the shape
//
//pos=byte(posM)+byte(pos);
//dist=byte(distM)+byte(distm);
ellipse(x,512-y,10,10);
line(256,256,x,512-y);
text(dist,x,512-y);
text(pos,256,256);
}
void serialEvent(Serial myPort) {
// read a byte from the serial port:
int inByte = myPort.readChar();
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes or more uncomment the additional //lines. So take care!
if (serialCount > 1 ) {
dist = serialInArray[0];
//distm = serialInArray[1];
pos=serialInArray[1];
//posM=serialInArray[3];
//dir = serialInArray[2];
y=256+int(dist*sin(pos*PI/180));
if(cos(pos)>0){
//
x=256+int(dist*(cos(pos*PI/180)));
}
else if (cos(pos)<=0){
x=256+int(dist*(cos(pos*PI/180)));
}
float freq=map(dist,0,200,800,440);//mapeamento da frequência em função de dist (distância do alvo). Freq vai de 800 a 400Hz quando dist vai de 0 a 200 cm.
float pan=map(pos,0,175,-1,1);
sine.setPan(pan);
sine.setFreq(freq);
println(dist+"\t"+pos+"\t"+x+"\t"+y+"\t"+(cos(pos))+"\t"+(sin(pos)));
serialCount=0;//reseta a serial, sem esta linha o sketch não funciona!
}
}
void stop()
{
out.close();
minim.stop();
super.stop();
}