我试图用下面的代码将超声波传感器的输出映射到9个leds上,因为某些原因,当我上传leds所有的代码时,我甚至没有通过串行监视器得到一个读数。我曾经尝试过类似的代码,但没有leds,这是完美无缺的。
const int trigPin = 13;
const int echoPin = 12;
const int maxRange = 300;
const int minRange = 0;
const int delayTime = 300;
const int ledPins[] = {10, 9, 8, 7 ,6, 5, 4, 3, 2};
const int ledCount = 9;
void setup() {
Serial.begin (9600);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
for(int thisLed = 0; thisLed < ledCount; thisLed = thisLed++){
pinMode(ledPins[thisLed], OUTPUT);
}
}
void loop() {
long duration;
digitalWrite(trigPin, LOW); // this alinea triggers the sensor
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH); // stores lenght of returned pulse
long distance = duration/58.2;
if (distance > maxRange){
Serial.println("Out of range");
delay(delayTime);
}
else if (distance < minRange){
Serial.println("Out of range");
delay(delayTime);
}
else {
Serial.print(distance);
Serial.println(" cm");
delay(delayTime);
}
constrain(distance, minRange, maxRange);
int usedLed = map(distance, minRange, maxRange, 0, ledCount);
for(int thisLed = 0; thisLed < usedLed; thisLed++){
digitalWrite(ledPins[thisLed], HIGH);
}
}发布于 2016-02-21 00:01:09
基本上,在下面的代码中,在安装函数中运行一个无限循环:
for(int thisLed = 0; thisLed < ledCount; thisLed = thisLed++){
pinMode(ledPins[thisLed], OUTPUT);
}这段代码thisLed = thisLed++根据C99规范的第6.5§2条产生未定义的行为:
在前一个序列点和下一个序列点之间,对象最多应该通过表达式的计算修改其存储值一次。此外,应只读取先验值以确定要存储的值。
为了将thisLed = thisLed++替换为thisLed++
https://stackoverflow.com/questions/35530498
复制相似问题