简体   繁体   中英

Real Time Bar Charts in arduino

I have written a test program to see that my pressure sensor is working correctly and it does.

int redpin = 10;
int greenpin = 9;
int bluepin = 8;
int presurepin = 0;

//Program variables
int time = 100;
int presure = 0;
int thresholddown = 19;
int thresholdup = 52;
int color = 0;
int red   = 0;
int green = 0;
int blue  = 0;
int relesepresure = 1;

void setup() {
  pinMode(redpin, OUTPUT);
  pinMode(greenpin, OUTPUT);
  pinMode(bluepin, OUTPUT);
  Serial.begin(9600);
}

void loop() {
  // Read presure
  presure = analogRead(presurepin);

  // For bug finding purpose
  delay(time);
  Serial.print(presure);
  Serial.print("   ");
  Serial.println(color);

  // High presure = 0 low = 300-500
  // If high presure change color and wait until presure is low to send out the color
  if (presure < thresholddown && relesepresure == 1){
    if (color == 0){
          red   = 0;
          green = 0;
          blue  = 0;
          color = color + 1;
    }
    else if (color == 1) {
          red   = 1;
          green = 0;
          blue  = 0;
          color = color + 1;
    }
    else if (color == 2) {
          red   = 0;
          green = 1;
          blue  = 0;
          color = color + 1;
    }
    else if (color == 3) {
          red   = 0;
          green = 0;
          blue  = 1;
          color = color + 1;
    }
    else if (color == 4) {
          // Yellow
          red   = 1;
          green = 1;
          blue  = 0;
          color = 1;

    }
    // Turn of light while tile is presured
    digitalWrite(redpin,   0);   // Write current values to LED pins
    digitalWrite(greenpin, 0);
    digitalWrite(bluepin,  0);
    relesepresure = 0;
  }
  else if (presure > thresholdup && relesepresure == 0){
      //Send color to tile
      digitalWrite(redpin,   red);
      digitalWrite(greenpin, green);
      digitalWrite(bluepin,  blue);
      relesepresure = 1;
  }
}

So in this code above, what I want to do is to store all the times the functions write either red, green, blue, yellow, etc. and display it on a computer screen in real time bar graphs. Something like Flot Examples but with bars.

So naturally I need to do somehing like this:

else if (color == 3) {

      //Color3++;
      //Update the bar graph with the new values (Color1,0),(Color2,1), (Color3,2), (Color4,3) where the numbers inside the paragraph are the x,y values of the graph.

      red   = 0;
      green = 0;
      blue  = 1;
      color = color + 1;
}

How can this be achieved since Arduino is a very limited language? I was thinking about writing these variable values to a simple .json file and read them from there with jQuery, but I don't know how to do that either. Is there a smarter solution?

I'm using an Arduino Mega .

First, you are going to have something going between JQuery and the Arduino Mega. Some of your options are:

  • Access the arudino through a usb cable and use some sort of server based technology to access it, such as php. There is an example of how to do this here:

http://wanderr.com/jay/controlling-arduino-via-serial-usb-php/2010/12/28/

  • Use an ethernet or wifi shield and provide the data back in the format of your choice. This would add about $30-60 to your cost but make the arduino part of this project self contained and not require a computer for it to run.

  • if you want it wireless you could look into bluetooth and zigbee recievers. you would still need a server and something like php with this option.

  • for flash, you typically use something called a serial socket server. There is more inforamation about this on the Aruduino Playground . It is possible you could also access the same type of server through jquery directly. This would be similar to the first option and would require a direct usb connection to the computer.

If you wanted to try something different, I would recommend looking at processing and firmata as there are lots of examples of how take data from an arduino and create some sort of visual based on that data.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM