Reads in a string of characters from a serial port until it gets a linefeed (ASCII 10). Then splits the string into sections separated by commas. Then converts the sections to ints, and prints them out. */
import processing.serial.*; // import the Processing serial library
int linefeed = 10; // Linefeed in ASCII Serial myPort; // The serial port int r = 0; int [] sensors;
//frameRate(10); // List all the available serial ports println(Serial.list());
// I know that the first port in the serial list on my mac // is always my Arduino module, so I open Serial.list()[0]. // Change the 0 to the appropriate number of the serial port // that your microcontroller is attached to. myPort = new Serial(this, Serial.list()[2], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10): myPort.bufferUntil(linefeed);
}
void draw() { // twiddle your thumbs background(0); fill(226);
ellipse(width/2, height/2, r, r);
}
// serialEvent method is run automatically by the Processing applet // whenever the buffer reaches the byte value set in the bufferUntil() // method in the setup():
void serialEvent(Serial myPort) { // read the serial buffer: String myString = myPort.readStringUntil(linefeed);
// if you got any bytes other than the linefeed: if (myString != null) {
myString = trim(myString);
// split the string at the commas // and convert the sections into integers: sensors = int(split(myString, ','));
// print out the values you got: for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) { print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t"); } // add a linefeed after all the sensor values are printed: println(); r = (int)sensors[1]; }
Reads two analog inputs and two digital inputs and outputs their values. Connections: analog sensors on analog input pins 0 and 1 switches on digital I/O pins 2 and 3 */
int leftSensor = 0; // analog input for the left arm int rightSensor = 1; // analog input for tht right arm int resetButton = 2; // digital input for the reset button int serveButton = 3; // digital input for the serve button
int leftValue = 0; // reading from the left arm int rightValue = 0; // reading from the right arm int reset = 0; // reading from the reset button int serve = 0; // reading from the serve button
void setup() { // configure the serial connection: Serial.begin(9600); // configure the digital inputs: pinMode(resetButton, INPUT); pinMode(serveButton, INPUT); }
void loop() { // read the analog sensors: leftValue = analogRead(leftSensor); rightValue = analogRead(rightSensor);
// read the digital sensors: reset = digitalRead(resetButton); serve = digitalRead(serveButton);
// print the results: Serial.print(leftValue, DEC); Serial.print(","); Serial.print(rightValue, DEC); Serial.print(","); Serial.print(reset, DEC); Serial.print(","); // print the last sensor value with a println() so that // each set of four readings prints on a line by itself: Serial.println(serve, DEC); }
3 則留言:
/*
Serial String Reader
Language: Processing
Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
*/
import processing.serial.*; // import the Processing serial library
int linefeed = 10; // Linefeed in ASCII
Serial myPort; // The serial port
int r = 0;
int [] sensors;
void setup() {
size(400, 400);
smooth();
background(0);
noStroke();
//frameRate(10);
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[2], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil(linefeed);
}
void draw() {
// twiddle your thumbs
background(0);
fill(226);
ellipse(width/2, height/2, r, r);
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil(linefeed);
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// split the string at the commas
// and convert the sections into integers:
sensors = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) {
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
r = (int)sensors[1];
}
}
Sensor reader
/*
Sensor Reader
Language: Wiring/Arduino
Reads two analog inputs and two digital inputs
and outputs their values.
Connections:
analog sensors on analog input pins 0 and 1
switches on digital I/O pins 2 and 3
*/
int leftSensor = 0; // analog input for the left arm
int rightSensor = 1; // analog input for tht right arm
int resetButton = 2; // digital input for the reset button
int serveButton = 3; // digital input for the serve button
int leftValue = 0; // reading from the left arm
int rightValue = 0; // reading from the right arm
int reset = 0; // reading from the reset button
int serve = 0; // reading from the serve button
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital inputs:
pinMode(resetButton, INPUT);
pinMode(serveButton, INPUT);
}
void loop() {
// read the analog sensors:
leftValue = analogRead(leftSensor);
rightValue = analogRead(rightSensor);
// read the digital sensors:
reset = digitalRead(resetButton);
serve = digitalRead(serveButton);
// print the results:
Serial.print(leftValue, DEC);
Serial.print(",");
Serial.print(rightValue, DEC);
Serial.print(",");
Serial.print(reset, DEC);
Serial.print(",");
// print the last sensor value with a println() so that
// each set of four readings prints on a line by itself:
Serial.println(serve, DEC);
}
單純讓電阻值輸入
int potPin = 0; // 宣告類比腳位輸入的PIN腳
int val = 0; // 宣告變數Val = 0
void setup() {
Serial.begin(9600); //設定SerialPort的速度9600 = 9600bit/s
}
void loop() {
val = analogRead(potPin); // 用analogRead()這個函式讀取特定腳位的數值到變數val
Serial.print(val); //印出數值在Serial.Monitor
delay(150); //延遲讀入的時間為150ms
}
讓電阻值輸入,影響LED燈
int potPin = 0; // 宣告類比腳位輸入的PIN腳
int val = 0; // 宣告變數Val = 0
int ledPin = 9;
void setup() {
Serial.begin(9600); //設定SerialPort的速度9600 = 9600bit/s
}
void loop() {
val = analogRead(potPin); // 用analogRead()這個函式讀取特定腳位的數值到變數val
Serial.println(val); //印出數值在Serial.Monitor
delay(10); //延遲讀入的時間為10ms
analogWrite(ledPin, val/4);
}
張貼留言