i looking integrate arduino controls , processing interface. in arduino code, there 3 buttons attached pin a1, a0 , d0 (all digitalread).
int x;// assigned a0 input int y;// assigned a1 input int z; //assigned d0 input int votes[3]={0,0,0}; void setup() { // initialize serial communication serial.begin(9600); // wait while littlebits arduino module not connected serial port // should not take long @ while(!serial); pinmode(0, input); pinmode(a0, input); pinmode(a1, input); } void loop() { // first need read values buttons x = digitalread(a0); // read , assign value of analog input a0 variable hillary y = digitalread(a1); // read , assign value of analog input a0 variable trump z = digitalread(0); // read , assign value of digital input 0 variable @faradaysciencecat // next need send these values processing // processing expecting particular format looks "123,456 " // format is: number | ',' | number | ' ' // ',' used tell end of first sensor value // ' ' used determine end of second value , packet whole if(digitalread(a0) == high) { votes[0]=votes[0]+1; } else if(digitalread(a1) == high) { votes[1]=votes[1]+1; } else if(digitalread(0) == high) { votes[2]=votes[2]+1; } // next need send these values processing // processing expecting particular format looks "123,456 " serial.println(votes[0]); serial.print(","); serial.println(votes[1]); serial.print(","); serial.println(votes[2]); // after send values need wait moment serial port delay(200); }
when button pressed being sent processing display bar graph, going every push. if button 1 pressed, first bar graph increase bit, etc.
import processing.serial.*; // serial port variables serial myport; string buff = ""; string buff1 = ""; string buff2 = ""; string buff3 = ""; int index = 0; int newline = 10; // store last 256 values sensors can draw them. int[] valuesx = new int[256]; int[] valuesy = new int[256]; int[] valuesz = new int[256]; // setup() processing sketch intitialized. happens first thing // sketch run , executes once. void setup() { // set size of window size(512, 512); // turn on anti-aliasing, makes things smoother smooth(); println(serial.list()); // use determine serial port arduino myport = new serial(this, serial.list()[0], 9600); myport.bufferuntil('\n'); } // draw() happens every frame of sketch. calculations made. // when draw() finished executing, executes again, on , over. void draw() { // set background littlebits purple background(87, 36, 124); // set stroke weight(thickness of line) 5 pixels strokeweight(5); (int = 0; < 255; i++) { stroke(247, i); // draw line (x1, y1, x2, y2) line(85, valuesx[i]+200, 85, valuesx[i + 1]+200); line(245, valuesy[i]+200, 245, valuesy[i + 1]+200); line(425, valuesz[i]+200, 425, valuesz[i + 1]+200); } // check serial port incoming data while (myport.available () > 0) { // if there data waiting... // execute serialevent() function. below see how serialevent works. serialevent(myport.read()); } } // serialevent controls how incoming serial data littlebits arduino module handled void serialevent(int serial) { if (serial != newline) { // store characters on line. buff += char(serial); } else { // end of each line marked 2 characters, carriage // return , newline. we're here because we've gotten newline, // still need strip off carriage return. buff = buff.substring(0, buff.length()-1); index = buff.indexof(","); buff1 = buff.substring(0, index); buff2 = buff.substring(index+1, buff.length()); buff3 = buff.substring(index+2, buff.length()); // parse string integer. divide 4 because // analog inputs go 0 1023 while colors in processing // go 0 255. int x = integer.parseint(buff1)/2; int y = integer.parseint(buff2)/2; int z = integer.parseint(buff3)/2; // clear value of "buff" buff = ""; // shift on existing values make room new one. (int = 0; < 255; i++) { valuesx[i] = valuesx[i + 1]; valuesy[i] = valuesy[i + 1]; valuesz[i] = valuesz[i + 1]; } // add received value array. valuesx[255] = x; valuesy[255] = y; valuesz[255] = z; } }
unfortunately, when press button, nothing displayed in processing.
you read serial port once, in setup()
function.
looking @ last question, looks forgot serialevent()
function.
Comments
Post a Comment