Processing Arduino

You need to upload standardfirmata with Arduino software to your Arduino board.
Then you need to use Processing software.


/* Practice 1. Testing ports */
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
void setup() {
size(200, 200);
noLoop(); // force not to loop
println(Arduino.list()); // print in the console COM ports list
}
void draw() {
}


/*Practice 2. Turn on a LED when the button is pressed*/

import processing.serial.*; //Import the libraries
import cc.arduino.*;
Arduino arduino; // Create the object Arduino
int ledPin = 13; // Assign the PIN number to LED
void setup() { //Port and I/O setting size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600); // Set up port as [1]
arduino.pinMode(ledPin, Arduino.OUTPUT); // PIN13 is set up as output arduino.digitalWrite(ledPin, Arduino.HIGH); //Turn on LED
}
void draw() { //Draw an interaction screen
if (mousePressed == true) { //ask whether the button is pressed or not arduino.digitalWrite(13,Arduino.LOW); // If mouse is pressed
} else {
arduino.digitalWrite(13,Arduino.HIGH); // If mouse button is not pressed switch on LED
}
}


/* Practice 3. Click mouse -> LED on/ off */
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 13;
void setup() {
size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.digitalWrite(ledPin, Arduino.HIGH);
}
void draw() {}
void mousePressed() { // Procedure for pressed button
arduino.digitalWrite(ledPin, Arduino.LOW);
}
void mouseReleased() { //Procedure for released button arduino.digitalWrite(ledPin, Arduino.HIGH);
}


/* Practice 4. LED on/off with boolean */
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 13;
boolean isLedOn = false;
void setup() {
size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.digitalWrite(ledPin, Arduino.HIGH);
}
void draw() {}
void mousePressed() { //Procedure to test mouse state
if(isLedOn) {
arduino.digitalWrite(ledPin, Arduino.HIGH);
} else {
arduino.digitalWrite(ledPin, Arduino.LOW);
}
isLedOn = !isLedOn;
}



/* Practice 5.* Mouse click and LED blinking*/
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int ledPin = 13;
boolean blinkLed = false; //Variable to find out if LES is blinking
void setup() {
size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.digitalWrite(ledPin, Arduino.LOW);
}
void draw() {
if(blinkLed) {
arduino.digitalWrite(ledPin, Arduino.LOW);
delay(50);
arduino.digitalWrite(ledPin, Arduino.HIGH);
delay(50);
} else {
arduino.digitalWrite(ledPin, Arduino.LOW);
}
}
void mousePressed() { // Detect if mouse is pressed
blinkLed = !blinkLed;
}



/* Practice 6. LED fading*/

import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
int pwm=0; int ledPin=9;
boolean rising=true;
void setup() {
size(200, 200);
arduino = new Arduino(this, Arduino.list()[1], 57600); arduino.pinMode(ledPin, Arduino.OUTPUT); arduino.analogWrite(ledPin, pwm);
}
void draw() {
arduino.analogWrite(ledPin, pwm); del LED println(pwm); if(rising) { pwm+=2;
if(pwm>=255) {
rising=false;
}
}else {
pwm-=2; if(pwm<=0) {
rising=true;
}
}
}


/* Practice 7. Mouse controls LED intensity*/
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
void setup() {
size(512, 512); arduino = new Arduino(this, Arduino.list()[1], 57600);
}
void draw() {
background(constrain(mouseX / 2, 0, 255));
//value of mouse X position sent to analog output 11
arduino.analogWrite(11, constrain(mouseX / 2, 0, 255));
//value of mouse Y position sent to analog output 9
arduino.analogWrite(9, constrain(255 - mouseY / 2, 0, 255));
}

Using Serial instead of firmata:

http://mertarduinotutorial.blogspot.com/2017/01/arduino-processing-tutorial-01-serial.html

Video and source code https://youtu.be/7iMMUO0J3h4

https://bcourses.berkeley.edu/courses/1464145/assignments/7819012

Computer vision, tts and arduino https://github.com/drfperez/scientix

Un bloc a XTECBlocs