Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added Processing examples

  • Loading branch information...
commit 955b18a8cc1f26e91747b3eb7cd3d9c1bc20008f 1 parent 54fdf5f
@pictux pictux authored
View
67 examples/pro_serialSimpleRead/pro_serialSimpleRead.pde
@@ -0,0 +1,67 @@
+/**
+ * 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.
+ */
+
+
+import processing.serial.*;
+
+Serial myPort; // Create object from Serial class
+char val; // Data received from the serial port
+
+void setup()
+{
+ size(200, 200);
+ // 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);
+}
+
+void draw()
+{
+ if ( myPort.available() > 0) { // If data is available,
+ val = (char)myPort.read(); // read it and store it in val
+ }
+ println(val);
+ background(255); // Set background to white
+ if (val == '0') { // If the serial value is 0,
+ fill(0); // set fill to black
+ }
+ else { // If the serial value is not 0,
+ fill(204); // set fill to light gray
+ }
+
+ rect(50, 50, 100, 100);
+
+}
+
+
+
+/*
+
+// Wiring / Arduino Code
+// Code for sensing a switch status and writing the value to the serial port.
+
+int switchPin = 4; // Switch connected to pin 4
+
+void setup() {
+ pinMode(switchPin, INPUT); // Set pin 0 as an input
+ Serial.begin(9600); // Start serial communication at 9600 bps
+}
+
+void loop() {
+ if (digitalRead(switchPin) == HIGH) { // If switch is ON,
+ Serial.print(1); // send 1 to Processing
+ } else { // If the switch is not ON,
+ Serial.print(0); // send 0 to Processing
+ }
+ delay(100); // Wait 100 milliseconds
+}
+
+*/
View
58 examples/pro_serialSimpleReadAnalog/pro_serialSimpleReadAnalog.pde
@@ -0,0 +1,58 @@
+import processing.serial.*;
+
+Serial myPort; // Create object from Serial class
+float val; // Data received from the serial port
+
+void setup()
+{
+ size(200, 200);
+ // 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);
+ myPort.bufferUntil('\n');
+}
+
+void draw()
+{
+ background(255); // Set background to white
+ fill(val); // set fill to black
+ rect(50, 50, 100, 100);
+}
+
+void serialEvent(Serial p)
+{
+ val = float(myPort.readStringUntil('\n'));
+}
+
+
+/*
+
+// Wiring / Arduino Code
+// Code for sensing a potentiometer and writing the value to the serial port.
+
+const int analogInPin = A0;
+
+int sensorValue = 0;
+int outputValue = 0;
+
+void setup() {
+ Serial.begin(9600);
+}
+
+void loop() {
+ sensorValue = analogRead(analogInPin);
+ outputValue = map(sensorValue, 0, 1023, 0, 255);
+
+ // print the results to the serial monitor:
+ Serial.println(outputValue);
+
+ // wait 2 milliseconds before the next loop
+ // for the analog-to-digital converter to settle
+ // after the last reading:
+ delay(2);
+}
+
+*/
View
86 examples/pro_serialSimpleWrite/pro_serialSimpleWrite.pde
@@ -0,0 +1,86 @@
+/**
+ * Simple Write.
+ *
+ * Check if the mouse is over a rectangle and writes the status to the serial port.
+ * This example works with the Wiring / Arduino program that follows below.
+ */
+
+
+import processing.serial.*;
+
+Serial myPort; // Create object from Serial class
+int val; // Data received from the serial port
+
+void setup()
+{
+ size(200, 200);
+ // 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];
+ String portName = "/dev/ttyUSB1";
+ myPort = new Serial(this, portName, 9600);
+ myPort.bufferUntil('\n');
+}
+
+void draw() {
+ background(255);
+ if (mouseOverRect() == true) { // If mouse is over square,
+ fill(204); // change color and
+ myPort.write(int(1)); // send an 1 to indicate mouse is over square
+ }
+ else { // If mouse is not over square,
+ fill(0); // change color and
+ myPort.write(0); // send an 0 otherwise
+ }
+ rect(50, 50, 100, 100); // Draw a square
+}
+
+boolean mouseOverRect() { // Test if mouse is over square
+ return ((mouseX >= 50) && (mouseX <= 150) && (mouseY >= 50) && (mouseY <= 150));
+}
+
+void serialEvent(Serial p) {
+ String inS = myPort.readString();
+ print(inS);
+}
+/*
+ // Wiring/Arduino code:
+ // Read data from the serial and turn ON or OFF a light depending on the value
+
+int ledPin = 9;
+
+void setup()
+{
+ Serial.begin(9600);
+ pinMode(ledPin, OUTPUT);
+}
+
+void loop()
+{
+ //Have the arduino wait to receive input
+ while (Serial.available() == 0);
+
+ //Read the input
+ int val = Serial.read();
+
+ Serial.println(val);
+ if (val == 1)
+ {
+ Serial.println("Led is On");
+ digitalWrite(ledPin, HIGH);
+ }
+ else if (val == 0)
+ {
+ Serial.println("Led is Off");
+ digitalWrite(ledPin, LOW);
+ }
+ else
+ {
+ Serial.println("Invalid!");
+ }
+ Serial.flush();
+}
+
+ */
View
62 examples/pro_serialSimpleWritePWM/pro_serialSimpleWritePWM.pde
@@ -0,0 +1,62 @@
+/**
+ * Simple Write.
+ *
+ * Check if the mouse is over a rectangle and writes the status to the serial port.
+ * This example works with the Wiring / Arduino program that follows below.
+ */
+
+
+import processing.serial.*;
+
+Serial myPort; // Create object from Serial class
+int val; // Data received from the serial port
+
+void setup()
+{
+ size(600, 200);
+ // 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];
+ String portName = "/dev/ttyUSB1";
+ myPort = new Serial(this, portName, 9600);
+ myPort.bufferUntil('\n');
+}
+
+void draw() {
+ val = constrain(mouseX / 2, 0, 255);
+ background(val);
+ myPort.write(val);
+}
+
+void serialEvent(Serial p) {
+ String inS = myPort.readString();
+ print(inS);
+}
+/*
+ // Wiring/Arduino code:
+ // Read data from the serial and PWM a light depending on the value
+
+int ledPin = 9;
+
+void setup()
+{
+ Serial.begin(9600);
+ pinMode(ledPin, OUTPUT);
+}
+
+void loop()
+{
+ //Have the arduino wait to receive input
+ while (Serial.available() == 0);
+
+ //Read the input
+ int val = Serial.read();
+
+ Serial.println(val);
+ analogWrite(ledPin, val);
+ Serial.flush();
+}
+
+ */
Please sign in to comment.
Something went wrong with that request. Please try again.