Hey all,
I am trying to make a dynamic bar chart from button presses that I detect from my Arduino. I have been looking on the internet to find any examples and I found this:
http://processing.org/discourse/yabb2/YaBB.pl?num=1260496119
After some tweaking for my own project it gives me a 'dynamic bar' but it starts running without me pressing any buttons.
What do I have to do to make the program react to my button presses? Consumption is the dynamic data to be put into a bar. The data will increase with every button press.
Hopefully some one can help me getting my program to work!
Thanks in advance!
Kim
Here is my code:
Arduino
- ...
- int buttonRec = analogRead(buttons);
- Serial.println(buttonRec);
- if(buttonRec >= 745 && buttonRec <= 790) {
- digitalWrite(ontbijtLED, HIGH);
- updateValue(1, 1);
- }
- if(buttonRec >= 640 && buttonRec <= 710) {
- analogWrite(lunchLED, 1023);
- updateValue(1, 2);
- }
- if(buttonRec >= 450 && buttonRec <= 550) {
- digitalWrite(dinerLED, HIGH);
- updateValue(1, 3);
- }
- if(buttonRec >= 0 && buttonRec <= 50) {
- digitalWrite(snackLED, HIGH);
- updateValue(1, 4);
- }
- if(buttonRec > 800) {
- digitalWrite(ontbijtLED, LOW);
- analogWrite(lunchLED, 0);
- digitalWrite(dinerLED, LOW);
- digitalWrite(snackLED, LOW);
- }
- ...
Processing
- import processing.serial.*;
- Serial myPort;
- float weight = 60.3; // kg
- float fatPercentage = 28; //%
- float activityLevel = 1.2;
- float leanMass = (weight * fatPercentage)/100;
- float BMR = 370 + (21.6 * leanMass);
- float personalEnergy = BMR * activityLevel;
- float consumption = 0;
- int data;
- void setup() {
- size(400, 400);
- myPort = new Serial(this, Serial.list()[0], 9600);
- }
- void draw() {
- background(0);
- fill(150);
- rect(80, height, 80, -consumption);
- drawGraph();
- }
- void drawGraph() {
- if (myPort.available() > 0) {
- data = myPort.read();
- }
- if (data >= 745 && data <= 790) {
- // ontbijt is detected
- consumption = consumption + ((25 * personalEnergy)/100);
- }
- if (data >=640 && data <= 710) {
- // lunch is detected
- consumption = consumption + ((30 * personalEnergy)/100);
- }
- if (data >= 450 && data <= 550) {
- // diner is detected
- consumption = consumption + ((30 * personalEnergy)/100);
- }
- if (data >= 0 && data <= 50); {
- // snack is detected
- consumption = consumption + ((5 * personalEnergy)/100);
- }
- if (data > 800) {
- // no button pressed, consumption stays the same
- consumption = consumption;
- }
- }
1