土壤湿度数据采集
目的本教程将教大家如何把土壤湿度传感器测到的数据以曲线图的方式在Processing中显示。 设备
输出与湿度的关系0~300 dry soil 干 300~700 humid soil 湿 700~950 in water 水
原理图程序SoilMoistureDataCollectionProcessing 调试步骤一:按着原理图搭建硬件环境,像这样:
本例需要两端的代码,Processing端和Microduino端 Microduino: //得到土壤湿度数据后输出到串口以便Processing显示 void loop ()
{
val=analogRead(A4);//val value read from potPin signal port
//Serial.print("Moisture Sensor Value:");
Serial.println(val);
delay(100);//delay
}
Processing: //得到第一个串口的数据,并定义如果有换行就缓存 // is always my Arduino, so I open Serial.list()[0].
// Open whatever port is the one you're using.
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n'); // Trigger a SerialEvent on new line
//把从Microduino接收到的数据以曲线图的形式显示在屏幕上,并标上标尺 void draw() {
background(255);
// Draw lines connecting all points
for (int i = 0; i < vals.length-1; i++) {
stroke(0);
strokeWeight(1);
line(i, vals[i], i+1, vals[i+1]);
}
// Slide everything down in the array
for (int i = 0; i < vals.length-1; i++) {
vals[i] = vals[i+1];
}
String val = myPort.readStringUntil('\n');
if (val != null) {
val = trim(val);
println(val);
soilHumidity=Float.parseFloat(val);
}
vals[vals.length-1] = 200-soilHumidity;
//Display scale
text ( "200-", 370, 10);
text ( "--", 370, 50);
text ( "100-", 370, 100);
text ( "--", 370, 150);
text ( "0-", 370, 200);
//show current num
text ( “val: ”+soilHumidity, 0, 10);
}
步骤三:下载代码并编译通过。 步骤四:运行后,把传感器插到土壤里,看看能不能显示出土壤湿度数据。 结果屏幕上会显示土壤湿度的曲线图数据,像这样:
视频 |