Физический пиксель
Пример использования платы Arduino для связи с компьютером, а конкретнее, чтобы получать данные из компьютера. В этом случае, платы Arduino включают светодиод, когда они получают 'H' (High), и выключают светодиод, когда они получают, соответственно, 'L' (Low).
Данные можно послать с ардуиновского последовательного монитора, или с другой программы, наподобие Processing (см. пример кода ниже), Flash (посредством serial-net proxy), PD или же Max/MSP.
Цепь
Светодиод на 13-ом пине
Код:
/*
Physical Pixel
The circuit:
* Светодиод идёт от цифрового 13-го пина к земле
created 2006
by David A. Mellis
modified 14 Apr 2009
by Tom Igoe and Scott Fitzgerald
*/
const int ledPin = 13; // the pin that the LED is attached to
int incomingByte; // a variable to read incoming serial data into
void setup() {
// initialize serial communication:
Serial.begin(9600);
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
}
void loop() {
// see if there's incoming serial data:
if (Serial.available() > 0) {
// read the oldest byte in the serial buffer:
incomingByte = Serial.read();
// if it's a capital H (ASCII 72), turn on the LED:
if (incomingByte == 'H') {
digitalWrite(ledPin, HIGH);
}
// if it's an L (ASCII 76) turn off the LED:
if (incomingByte == 'L') {
digitalWrite(ledPin, LOW);
}
}
}
Код для Processing
// mouseover serial
//В примере показано, как посылать данные на ардуиновскую
//плату ввода/вывода, для того, чтобы включать свет, если
//мышка находиться над квадратом и выключать, если,
//соответственно, мышка не находится над квадратом.
// created 2003-4
// based on examples by Casey Reas and Hernando Barragan
// modified 18 Jan 2009
// by Tom Igoe
import processing.serial.*;
float boxX;
float boxY;
int boxSize = 20;
boolean mouseOverBox = false;
Serial port;
void setup() {
size(200, 200);
boxX = width/2.0;
boxY = height/2.0;
rectMode(RADIUS);
// List all the available serial ports in the output pane.
// You will need to choose the port that the Arduino board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
// Open the port that the Arduino board is connected to (in this case #0)
// Make sure to open the port at the same speed Arduino is using (9600bps)
port = new Serial(this, Serial.list()[0], 9600);
}
void draw()
{
background(0);
// Test if the cursor is over the box
if (mouseX > boxX-boxSize && mouseX < boxX+boxSize &&
mouseY > boxY-boxSize && mouseY < boxY+boxSize) {
mouseOverBox = true;
// draw a line around the box and change its color:
stroke(255);
fill(153);
// send an 'H' to indicate mouse is over square:
port.write('H');
}
else {
// return the box to it's inactive state:
stroke(153);
fill(153);
// send an 'L' to turn the LED off:
port.write('L');
mouseOverBox = false;
}
// Draw the box
rect(boxX, boxY, boxSize, boxSize);
}
Выход:
Когда Вы наведёте мышь на центральный квадрат, светодиод на 13-ом пине должен включиться и выключиться. Пример для Processing выглядит следующим образом:
Max patch
Патч для Max/MSP показан на изображении ниже. Текст находится . Скопируйте и вставьте этот текст в новое окно патча.
комментарии(0)
Комментировать