processing - more erasmus+ pau, 2016 february

8
Processing is an open source programming language and environment for people who want to create images, animations, and interactions. Processing can interact with Arduino using serial communication. Processing (www.processing.org) is complementary to Arduino, because the languages and IDEs are so similar. Processing programs are called sketches http://www.processing.org and download the appropriate version for your OS of your choice. PROCESSING & ARDUINO PAU 2016

Upload: decibeldanilo

Post on 16-Apr-2017

106 views

Category:

Software


1 download

TRANSCRIPT

Page 1: Processing - MORE Erasmus+ PAU, 2016 February

Processing is an open source programming language and environment for people who want to create images, animations, and interactions.Processing can interact with Arduino using serial communication.

Processing (www.processing.org) is complementary to Arduino, because the languages and IDEs are so similar.

Processing programs are called sketches

http://www.processing.org and download the appropriate version for your OS of your choice.

PROCESSING & ARDUINO PAU 2016

Page 2: Processing - MORE Erasmus+ PAU, 2016 February

Arduino IDE was based on the Processing IDEPROCESSING & ARDUINO PAU 2016

Page 3: Processing - MORE Erasmus+ PAU, 2016 February

Structure of a Processing Sketch: example

int timer;

void setup() {// The next function sets the size of the sketchsize(800,600);/*This is a multiline comment, anything written between the two multiline commmentdelimiters will be ignored by the compiler.*/}void draw() {background(255);ellipse(timer, height/2, 30, 30);timer = timer + 1;}

setup() Function

draw() Functionwill run as a loop until the program is terminated

by the user.

PROCESSING & ARDUINO PAU 2016

Page 4: Processing - MORE Erasmus+ PAU, 2016 February

Processing IDEWe are going to draw a circle on screen that will move to the right of the screen as time passes.Background() function i s necessary to clear the frame with a color at every iteration. If we comment it out (temporarily remove it by adding // to the beginning of the line), we will see that the circles are drawn on top of the previous ones.

Press the Run buttonPROCESSING & ARDUINO

PAU 2016

Page 5: Processing - MORE Erasmus+ PAU, 2016 February

The following simple sketch in processing reads the mouse position and sends data (‘1’ or ‘0’) to the arduino board by serial port. The yellow LED connected on pin 13 is on or off depending of the position of the mouse in the rectangle.

Page 6: Processing - MORE Erasmus+ PAU, 2016 February

void setup() {Serial.begin(9600);pinMode(13, OUTPUT);}void loop(){if (Serial.available()>0) {byte input=Serial.read();if(input == '1'){digitalWrite(13, HIGH);}else{digitalWrite(13, LOW);}}}

Scketch for Arduino on_off_LED_da_mouse_ino

import processing.serial.*;Serial myPort;void setup(){size(255, 255);//String portName = Serial.list()[0];myPort = new Serial(this, "com4", 9600);}void draw() {noStroke();rect(0,0,width/2,height);if (mouseX>width/2) {myPort.write('1');}else { myPort.write('0');}}

Scketch for processing on_off_LED_da_mouse

PROCESSING & ARDUINO PAU 2016

Page 7: Processing - MORE Erasmus+ PAU, 2016 February

ARDUINO_PWM in processing_Example_libraries_Arduino(firmata)_arduino_pwmDemonstrates the control of analog output (PWM) pins of an Arduino boardrunning the StandardFirmata firmware. Moving the mouse horizontally acrossthe sketch changes the output on pins 9 (value is proportional to the mouseX coordinate) and 11 (inversely porportional to mouse X).

ARDUINO_OUTPUT in processing_Example_libraries_Arduino(firmata)_arduino_outputDemonstrates the control of digital pins of an Arduino board running theStandardFirmata firmware. Clicking the squares toggles the corresponding

digital pin of the Arduino.

Su arduino occorre caricare Standardfirmata da esempi_firmata_standardfirmataUsing the Arduino software, upload the StandardFirmata example (located in Examples > Firmata > StandardFirmata) to your Arduino board.

PROCESSING & ARDUINO PAU 2016

Page 8: Processing - MORE Erasmus+ PAU, 2016 February

1. we import the Serial library and declare a global Serial object2. Arduino and Processing communicate at the same rate by ”com 4”3. In our draw() loop, we send whatever we want over the serial port by using the “write method” from the Processing Serial library.4. For this sketch, we will send a ‘1’ or a ‘ 0’ whenever we click our mouse in the Processing window5. nostroke() disabilita il contorno6. size() create a window7. List all the available serial port8. background(0) put up a black background9. if (myPort.available() > 0) Read the serial port.10. String PortName…. /* On some computers, Arduino will usually be connected to the first serial port. If not, change the 0 to the correct one (examine the output of the previous line of code to figure out which one to use). */11. rect(0,0, width/2,heigth) 0,0 coordinates, width of the size and heigth of the size12. Ellipse (x,y, larghezza, altezza) x a partire da sinistra ed y a partire dall’alto13.strokeWeigth(20) spessore del contorno a 20 pixel