Last lesson of the FabAcademy electronics for the external learning activity at TU/e. This time maybe one of the most magical and surprising ones, it is still a big surprise for me what is possible to achieve with very few lines of code in a software like Processing, the one I will be using during this assignment.
The goal is to make some sort of animation that interfaces with an input from a physical device. In my case, I will be using the hello echo board I made in week 2.
Getting started
The first step was to make sure that the serial communication between the hello.echo board and the computer was working correctly. I remembered the last exercise during week 3 involved sending information about wether the led was on and off ("Led on" or "Led off"). However, I wanted to keep it simple and I used an even earlier sketch for the hello.echo board with which the led only switched on when the button was pressed. I add a couple of lines to send the value "1" to the serial monitor when this happened and added a couple of delays to make sure the serial didn't read too many numbers for every press, just one. This is the modified simple sketch.
Starting with a cute example that took the mouse click as an input to change the color of a cross that tracked the mouse movement (MousePress example), I set out to modify it and include the push of the switch embedded in the hello.echo board as input. I looked at the Processing code in the documentation of other students and found some interesting things:
- import processing.serial.*;
- Serial myPort;
[...]
myPort = new Serial(this, "/dev/cu.usbserial-FTHC3YU2", 9600);
This lines of code are very important for they set the communication between the serial port through the FTDI cable (address and baud rate need to be specified) and Processing, so the program can take the date coming from the serial as an input.
- myPort.bufferUntil('\n');This line tells Processing to wait and not do anything until it gets the data it is waiting for.
- void loop() is in Processing void draw(), but still is a loop.
Example 1: mouse cross change color
Starting with the basic MousePress example, I modified it to get the press from the switch of the hello.echo board instead. The cross that follows the mouse movement switches from black to white when the switch button is pressed. You can find the code here.
Example 2: Mouse Color Squares Pattern
Building up on the same example, a series of rectangles are drawn based on the position of the mouse, superposing each other for every loop of the program. When the switch button is pressed, an new background is drawn and therefore the animation is reseted. You can find the Processing code here.
Example 3: Switch Opposite Squares
Starting with the Mouse1D example, I substituted the x coordinates of the mouse by clicks on the switch button as inputs for the squares to change size and fill color. You can find the code here.
Workfiles
Añadir un comentario