我已经编写了一个测试程序来查看我的压力传感器是否正常工作并且确实如此。
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;
}
}
所以在上面的这段代码中,我想做的是存储函数写入红色、绿色、蓝色、黄色等的所有时间,并将其以实时条形图的形式显示在计算机屏幕上。类似于Flot Examples但带有条形图的东西。
所以很自然我需要做这样的事情:
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;
}
由于 Arduino 是一种非常有限的语言,如何实现这一点?我正在考虑将这些变量值写入一个简单的 .json 文件并使用 jQuery 从那里读取它们,但我也不知道该怎么做。有更聪明的解决方案吗?
我正在使用Arduino Mega。