智能硬件开发:从基础搭建到创意应用
1. 示例行为与代码实现
在代码中设置简单行为以执行操作是很有趣的。以检测穿特定鞋子的人是否静止站立为例,当人静止站立时,双脚平放在地面,鞋子的脚趾和脚跟传感器都会受到持续压力。基于此,可以使用简单的条件语句来测试站立行为。
以下是测试左脚站立的代码示例:
// change this value to suit your weight/pressure needs, as explained above
var pressureThresh = 800;
// this tests if a sensor is currently being pressed hard enough
function isPressed(val) {
if (val > pressureThresh) {
return true;
} else {
return false;
}
}
// this tests if both sensors in the left foot are being pressed simultaneously
function isStanding() {
if (isPressed(leftToe.value) && isPressed(leftHeel.value)) {
return true;
} else {
return false;
}
}
// main loop, every 25 ms
this.loop(25, function() {
if (isStan