Interfaces and Application Programming
Processing
Aim: To change a brush size using a phototransistor board.
Running Processing on linux:
First I tried to scale the ellipse size using the mouse wheel. I found a mousewheel function which has to be imported into processing. Then I combined a mousewheel example and combined it with another example sketch for scale and using ellipseMode(CENTER). Notes: MOVED background(255) from void (draw) to void setup() so that the background is not redrawn every time the code repeats.
Processing with Serial
I had trouble directing it to the correct serial port. I knew I was using dev/tty/USB1 - by using dmesg on linux. However even using the correct index from the list of printed ports available did not work and the serial Processing library wasnt very helpful. I asked Ferdi my instructor and we made it work like this: "dev/ttyUSB1"
The phototransistor board outputs two values. Each value is made up of 2 bytes containing 8 bits
In order to access these two values I had to edit the code:
while (myPort.available() > 0) { // If data is available
byte1 = byte2;
byte2 = byte3;
byte3 = byte4;
byte4 = on_low;
on_low = on_high;
on_high = off_low;
off_low = off_high;
off_high = myPort.read();
Then to compile the right value from the two bytes we do the x 256 trick which moves the value to the left 8 bits. And calculate the difference value:
if ((byte1 == 1) & (byte2 == 2) & (byte3 == 3) & (byte4 == 4)){
on_value = ((256*on_high) + on_low);
off_value = ((256*off_high) + off_low);
diff_value = on_value - off_value;
The I printed out the values:
println("THE ON VALUE IS " + on_value);
println("THE OFF VALUE IS " + off_value);
println("THE DIFFERENCE VALUE IS " + diff_value);
Once I could see the difference values being output, I wanted to adjust the range of the output values to the size of the brush. There is a handy map() function in processing which does this for you:
In the end I opted for the line() function and mapped the phototransistor's difference value to a suitable range to change the strokeWeight.
Below my fellow classmate Theo, enjoys my invention- my code can be downloaded here.