一.arduino
1.校准 :当模拟量输入时,在初始化过程中确定模拟量的范围。检测到一个最大值,一个最小值。
int sensorMax=0;int sensorMin = 1023;
while (millis() < 5000) {
int sensorValue = analogRead(sensorPin);
if (sensorValue >sensorMax) {
sensorMax = sensorValue;}
if (sensorValue < sensorMin) {
sensorMin = sensorValue;}}
2.限定变量范围:constrain(x, 0, 255);
3.平滑取输入(10个值平均值,新值与前9个值)
int readings[numReadings];
void loop() {
total= total - readings[readIndex];
readings[readIndex] = analogRead(inputPin);
total= total + readings[readIndex];
readIndex = readIndex + 1;
if (readIndex >= numReadings)
readIndex = 0;
average = total / numReadings;}