Descrição
O sketch foi feito a partir de exemplos contidos na seção Examples do Processing. Aqui um sólido tridimensional é gerado na tela do computador. O sketch do Arduino propriamente dito recebe as leituras de um acelerômetro e as envia para a porta serial (usb). O Processing "intercepta" esses dados seriais e movimenta o sólido nos três eixos.
Script do Processing:
/**
* Serial Call-Response
* by Tom Igoe.
* Modified by Solucionática on Oct,23th * 2013.
*
* Sends a byte out the serial port, and reads 3 bytes in.
* Sets foregound color, xpos, and ypos of a circle onstage
* using the values returned from the serial port.
* Thanks to Daniel Shiffman and Greg Shakar for the improvements.
*
* Note: This sketch assumes that the device on the other end of the serial
* port is going to send a single byte of value 65 (ASCII A) on startup.
* The sketch waits for that byte, then sends an ASCII A whenever
* it wants more data.
*/
import processing.serial.*;
Serial myPort; // The serial port
int[] serialInArray = new int[3]; // Where we'll put what we receive
int serialCount = 0; // A count of how many bytes we receive
int xpos, ypos,zpos; // Starting position of the ball
boolean firstContact = false;
int inByte;
// Whether we've heard from the microcontroller
void setup() {
//size(256, 256); // Stage size
//noStroke(); // No border on the next thing drawn
frameRate(4);
size(640, 360, P3D);
stroke(204,50,0);
//for(int i=0; i<num; i++) {
// colors[i] = color(255 * (i+1)/num);
// Set the starting position of the ball (middle of the stage)
xpos = width/2;
ypos = height/2;
zpos = height/2;
// Print a list of the serial ports, for debugging purposes:
println(Serial.list());
// 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, 9600);
//lights();
}
void draw() {
background(0,0,26);
//fill(fgcolor);
// Draw the shape
translate (320,180,0);
rotateX(map(xpos,0,255,0,2*PI));
rotateY(map(ypos,0,255,0,2*PI));
rotateZ(map(zpos,0,255,0,2*PI));
box(100);
//ellipse(xpos, ypos, zpos, 20); //efeito interessante
delay(20);
}
void serialEvent(Serial myPort) {
int inByte = myPort.readChar();
// if this is the first byte received, and it's an A,
// clear the serial buffer and note that you've
// had first contact from the microcontroller.
// Otherwise, add the incoming byte to the array:
if (firstContact == false) {
if (inByte == 'A') {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
}
}
else {
// Add the latest byte from the serial port to array:
serialInArray[serialCount] = inByte;
serialCount++;
// If we have 3 bytes:
if (serialCount > 2 ) {
xpos = serialInArray[0];
ypos =serialInArray[1];
zpos =serialInArray[2];
// print the values (for debugging purposes only):
println(xpos + "\t" + ypos + "\t" + zpos);
// Send a capital A to request new sensor readings:
myPort.write('A');
// Reset serialCount:
serialCount = 0;
}
}
}
Material :
1. Arduino Atmega 2560
2. Acelerômetro de três eixos MMA 7361L da Iteadstudio.
3. Prot-o-board
4. Paciência e imaginação.
Sketch (lado do Arduino):
/*
ADXL3xx
Reads an Analog Devices ADXL3xx accelerometer and communicates the
acceleration to the computer. The pins used are designed to be easily
compatible with the breakout boards from Sparkfun, available from:
http://www.sparkfun.com/commerce/categories.php?c=80
http://www.arduino.cc/en/Tutorial/ADXL3xx
The circuit:
analog 0: accelerometer self test
analog 1: z-axis
analog 2: y-axis
analog 3: x-axis
analog 4: ground
analog 5: vcc
created 2 Jul 2008
by David A. Mellis
modified 30 Aug 2011
by Tom Igoe
This example code is in the public domain.
*/
// these constants describe the pins. They won't change:
const int groundpin = 18; // analog input pin 4 -- ground
const int powerpin = 19; // analog input pin 5 -- voltage
int sensorx = A3; // x-axis of the accelerometer
int sensory = A2; // y-axis
int sensorz = A1; // z-axis (only on 3-axis models)
int x; // x-axis of the accelerometer
int y; // y-axis
int z;
void setup()
{
// initialize the serial communications:
Serial.begin(9600);
// Provide ground and power by using the analog inputs as normal
// digital pins. This makes it possible to directly connect the
// breakout board to the Arduino. If you use the normal 5V and
// GND pins on the Arduino, you can remove these lines.
pinMode(groundpin, OUTPUT);
pinMode(powerpin, OUTPUT);
digitalWrite(groundpin, LOW);
digitalWrite(powerpin, HIGH);
establishContact();
}
void loop()
{
x=constrain(analogRead(sensorx),0,400);
y=constrain(analogRead(sensory),0,400);
z=constrain(analogRead(sensorz),0,400);
int xx=map(x,0,400,0,255);
int yy=map(y,0,400,0,255);
int zz=map(z,0,400,0,255);
Serial.write(xx);
Serial.write(yy);
Serial.write(zz);
/*
// print the sensor values:
Serial.print(analogRead(xpin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(ypin));
// print a tab between values:
Serial.print("\t");
Serial.print(analogRead(zpin));
Serial.println();
// delay before next reading:
*/
delay(100);
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.print('A'); // send a capital A
delay(300);
}
}