我已经编写了一个测试程序来查看我的压力传感器是否正常工作并且确实如此。
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 .
最佳答案
首先,您将在 JQuery 和 Arduino Mega 之间进行一些操作。您的一些选择是:
- 通过 USB 电缆访问 arudino,并使用某种基于服务器的技术来访问它,例如 php。这里有一个如何执行此操作的示例:
http://wanderr.com/jay/controlling-arduino-via-serial-usb-php/2010/12/28/
使用以太网或 wifi 屏蔽并以您选择的格式提供回数据。这会使您的成本增加大约 30-60 美元,但可以使该项目的 arduino 部分独立运行,不需要计算机即可运行。
如果您想要它是无线的,您可以查看蓝牙和 zigbee 接收器。您仍然需要一台服务器和带有此选项的 php 之类的东西。
对于闪存,您通常使用称为串行套接字服务器的东西。在 Aruduino Playground 上有更多关于此的信息.您也可以直接通过 jquery 访问相同类型的服务器。这与第一个选项类似,需要直接通过 USB 连接到计算机。
如果您想尝试不同的东西,我建议您查看 processing和 firmata因为有很多示例说明如何从 arduino 获取数据并基于该数据创建某种视觉效果。
关于jquery - arduino 中的实时条形图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12821254/