// play on Raspberry Pi 3B+ with OS Raspbian // using Arduino IDE 1.8.8 nightly 18.1.2019 // upload / Standard Firmata // using Processing IDE 3.5.2 // /File / Examples / Contributed Libraries / Arduino (Firmata) / arduino input + arduino output // for test: jumper arduino A0 <--> D5 import processing.serial.*; import cc.arduino.*; Arduino myUno; int AinPin0 = 0; // A0 int DoutPin5 = 5, doutval5 = Arduino.LOW; // D5 float w_AinRaw_0, w_volt_0; long stime, dtime=1000; // sampling scheduler int posx =20, posy = height-40, w=102, h = 15; // voltmeter int bposx=20, bposy=20, bw=60, bh=20; // button rectangle settings boolean benable=true, bstat=false; void setup() { size( 200, 200); println(Arduino.list()); myUno= new Arduino(this, Arduino.list()[1], 57600); myUno.pinMode(DoutPin5, Arduino.OUTPUT); println("use key [5] toggle dout"); } void get_data() { if ( millis() > stime + dtime ) { w_AinRaw_0 = myUno.analogRead(AinPin0); // A0 // 1023 == 5VDC w_volt_0 = w_AinRaw_0 * 5.0 / 1023.0; println("A0 raw: "+w_AinRaw_0+" volt: "+w_volt_0); stime = millis(); } } void draw() { background(200, 200, 0); if (benable) myButton("D 5"); get_data(); // read input ever second see: dtime volt_meter(); } void toggle_D5() { if ( doutval5 == Arduino.LOW ) doutval5=Arduino.HIGH; // toggle else doutval5=Arduino.LOW; myUno.digitalWrite(DoutPin5, doutval5); // write println("set dout channel "+DoutPin5+ " to "+doutval5); } //_________________________________________________________________ use keyboard OR mouse button void keyPressed() { if ( key == '5' ) toggle_D5(); } //_________________________________________________________________ BUTTON with text void myButton(String showtxt) { pushMatrix(); pushStyle(); stroke(0, 180, 200); strokeWeight(2); if (bstat) fill(0, 200, 0); else fill(0, 0, 200); rect(bposx, bposy, bw, bh); fill(0, 0, 0); text(showtxt, bposx+5, bposy+15); popStyle(); popMatrix(); } //_________________________________________________________________ mouse click left void mousePressed() { if (mouseButton == LEFT) { if ( overRect(bposx, bposy, bw, bh) && benable ) { bstat = ! bstat; toggle_D5(); } } } //_________________________________________________________________ mouse position over rectangle yes/no boolean overRect(int rx, int ry, int rwidth, int rheight) { if (mouseX >= rx && mouseX <= rx+rwidth && mouseY >= ry && mouseY <= ry+rheight) return true; else return false; } void volt_meter() { posy = height-40; stroke(0, 200, 0); fill(0, 0, 200); rect(posx, posy, w, h); int bar = int(map(w_volt_0, 0, 5.0, 0, 100)); for ( int i = 0; i<=bar; i++) line(posx+i+1, posy+1, posx+i+1, posy+h-1); text("_A0_", posx, posy-5); text(" "+nf(w_volt_0, 1, 2)+" V", posx+w+10, posy+10); }