We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpElectronics,  Serial Library › Bar Graph on Button Press from Arduino
Page Index Toggle Pages: 1
Bar Graph on Button Press from Arduino (Read 2656 times)
Bar Graph on Button Press from Arduino
Dec 10th, 2009, 5:48pm
 
Please help! I'm trying to create a bar graph  that increments in Processing as a button is pressed from the arduino controller. How do I, 1) create the bar graph to increment?; 2)Show a tally on the bar chart of the total button presses? I have the arduino part resolved, just need help with graphics. The code for drawGraph() so far:

void drawGraph() {

 int lineHeight = sensorValue/2;
 
 if (buttonPress){
   
   
  stroke(0, 255,0);
 

 }
 else {
 stroke(255, 0,0);
   
     

 }

 rect(height, graphPosition, height - lineHeight, graphPosition);

 if (graphPosition >= width) {
   graphPosition = 0;
  background(0);
 
 }
 else {
   graphPosition++;
 }
}

Re: Bar Graph on Button Press from Arduino
Reply #1 - Dec 10th, 2009, 10:25pm
 
I am not really sure what you tried to do in your code.
Do you have a sensor, or just want it to change when a button is pressed?
So what you need is a create a variable that holds the value and count that up everytime akey is pressed.

this is how it could be done.

Code:
int value;
PFont font;

void setup(){
size(300,500);
font = createFont("Arial",22);
textAlign(CENTER);
textFont(font);
}

void draw(){
background(255);
fill(0 );
rect(30,height,240,-value);
text(value,width/2,height-value-20 );
}

void keyPressed(){
value+=5;
}
Re: Bar Graph on Button Press from Arduino
Reply #2 - Dec 15th, 2009, 3:08pm
 
Thanks for the response Cedric, your code has been helpful to create the graph. I'm still trying to increment the bar as I press a button from arduino, instead of keyPressed(). So far, if I press the button, the bar will raise at a given value and reset to 0 instead of incrementing upwards. Any thoughts? The revised code:


Code:

/* PROCESSING CODE */

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int value;      // Data received from the serial port
PFont font;


void setup()
{
 size(300, 500);

 // Open whatever port being used.
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
 font = createFont("Arial", 22);
 textAlign(CENTER);
 textFont(font);
}

void draw(){
 background(255);
 fill(0);
 rect(30,height,240,-value);
 text(value,width/2,height-value-20);
drawGraph();
}

void drawGraph(){
   if (myPort.available() > 0) {
   value = myPort.read();
 }
 if (value == 1){ //If the switch/button is ON
value+=5;  
 }
}




/*ARDUINO CODE */
// Code for sensing a switch status and writing the value to the serial port.

int switchPin = 4;                       // Switch connected to pin 4

void setup() {
 pinMode(switchPin, INPUT);             // Set pin 0 as an input
 Serial.begin(9600);                    // Start serial communication at 9600 bps
}

void loop() {
 if (digitalRead(switchPin) == HIGH) {  // If switch is ON,
   Serial.print(1, BYTE);               // send 1 to Processing
 } else {                               // If the switch is not ON,
   Serial.print(0, BYTE);               // send 0 to Processing
 }
 delay(100);                            // Wait 100 milliseconds
}




Re: Bar Graph on Button Press from Arduino
Reply #3 - Dec 15th, 2009, 4:10pm
 
Ok, I figured how to increment the bar graph as I press a button from Arduino that will save the button presses (value) from the previous value so I can take new input/readings.  The issue now is the bar graph itself, the bar graph needs to remain visible on the screen  while it continues to increment when the button is pressed. So far, every time I press the button, the bar graph drops/disappears, but remembers the last number of presses and its last position on the screen. The graph still needs to visually increment. Any ideas?  Revised code:

Code:
 

import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
PFont font;
int buttonValue = 0;
int prevButtonValue = 0;


void setup()
{
 size(300, 500);

 // Open whatever port is the one you're using.
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
 font = createFont("Arial", 22);
 textAlign(CENTER);
 textFont(font);
}

void draw(){
 background(255);
 fill(0);
 rect(30,height,240,-val);
 text(val,width/2,height-val-20);
drawGraph();
prevButtonValue = buttonValue; //save button value as the previous
                                 //  value to take new readings
}


void drawGraph(){
   if (myPort.available() > 0) {
   val = myPort.read();
 }
 if (val == 1){ //if button is pressed
val = buttonValue;
buttonValue++;
 }
}



Re: Bar Graph on Button Press from Arduino
Reply #4 - Dec 15th, 2009, 5:18pm
 
SOLVED IT!

Code:
 



import processing.serial.*;

Serial myPort;  // Create object from Serial class
int val;      // Data received from the serial port
PFont font;
int buttonValue = 0;
int prevButtonValue = 0;


void setup()
{
 size(300, 500);

 // Open whatever port is the one you're using.
 String portName = Serial.list()[0];
 myPort = new Serial(this, portName, 9600);
 font = createFont("Arial", 22);
 textAlign(CENTER);
 textFont(font);
// font =loadFont("Ziggurat-12.vlw");

}

void draw(){

 
 background(255);
 fill(0, 0, 255);
 rect(30,height,240,-val);
text(val,width/2,height-val-20);
drawGraph();
prevButtonValue = buttonValue;
}


void drawGraph(){
   if (myPort.available() > 0) {
   val = myPort.read();
 }
 if (val == 1){ //if button is pressed
val = buttonValue;
buttonValue++;
 } else {
   val = buttonValue; //stores last graph value!
}
}




Page Index Toggle Pages: 1