使用多个传感器/ if else语句时导致Arduino IDE闪烁

时间:2019-01-15 20:42:49

标签: arduino arduino-ide arduino-esp8266

如果满足多个条件,我正在尝试使我的LED闪烁。 下面的代码在if else语句中没有teller1部分的情况下可以正常工作,但是当我在if else语句中添加elapsedmillis计数器时,即使所有条件都满足,led也不会闪烁。

有帮助吗?

这是我的代码:

#define trigPin D7
#define echoPin D8
#include <elapsedMillis.h>
elapsedMillis teller1;
elapsedMillis teller2;

const int RED = D5; //het ‘rode’ pootje zit in D5
const int GREEN = D4; //etc. 
const int BLUE = D3;

void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(D6, OUTPUT);
pinMode(RED, OUTPUT);
pinMode(GREEN, OUTPUT);
pinMode(BLUE, OUTPUT);

}

void loop() {

  if (teller1 > 500) {

  int sensorValue = analogRead(A0);
  Serial.println(sensorValue);

  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;

    Serial.print(distance);
    Serial.println(" cm");

  if ((sensorValue > 20) && (distance > 20) && (teller1 < 250)) {

  analogWrite(RED, 255);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0); 
  } else if ((sensorValue > 20) && (distance > 20) && (teller1 > 250)) {
  analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0); 
  }

  else if (sensorValue <20) {
  analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0);
  }


  teller1 = 0;
  } 

  delay(50);

}

编辑:我想我已经修复了它,但是我不确定这是否会写得很好或执行此操作的正确方法:

#define trigPin D7          //trigger pin voor de echo sensor definieren op D7
#define echoPin D8          //echo pin voor de echo sensor definieren op D8
#include <elapsedMillis.h>  //elapsedMillis library aanroepen
elapsedMillis teller1;      //teller1 aanmaken
elapsedMillis teller2;      //teller 2 aanmaken

const int RED = D5; //het ‘rode’ pootje zit in D5
const int GREEN = D4; //etc. 
const int BLUE = D3;

void setup() {
Serial.begin(115200);
pinMode(trigPin, OUTPUT);   //confirugeer D7 als output
pinMode(echoPin, INPUT);    //confirugeer D8 als input
pinMode(D6, OUTPUT);        //confirugeer D6 als output
pinMode(RED, OUTPUT);       //confirugeer D5 als output
pinMode(GREEN, OUTPUT);     //confirugeer D4 als output
pinMode(BLUE, OUTPUT);      //confirugeer D3 als output

}

void loop() {

  if (teller1 > 500) {      //als de timer boven 500ms komt

  int sensorValue = analogRead(A0); //sla de sensorwaarde op
  Serial.println(sensorValue);      //druk de sensorwaarde af in de serial monitor

  long duration, distance;
  digitalWrite(trigPin, LOW); 
  delayMicroseconds(2); 
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration/2) / 29.1;   //formule om afstand te berekenen

    Serial.print(distance);
    Serial.println(" cm");

  if ((sensorValue > 20) && (distance < 20) && (teller1 < 750)) {

  analogWrite(RED, 255);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0); 
  digitalWrite(D6, HIGH);
  } 

  else if (sensorValue <20) {
  analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0);
  }


  teller1 = 0;
  } else if (teller1 < 250) {
   analogWrite(RED, 0);
  analogWrite(GREEN, 0);
  analogWrite(BLUE, 0); 
  digitalWrite(D6, LOW);
  }

  delay(50);
  Serial.print("teller");
Serial.println(teller1);
}

1 个答案:

答案 0 :(得分:0)

您的void loop()if (teller1 > 500) {开头。
然后,在检查传感器值时说

if ((sensorValue > 20) && (distance > 20) && (teller1 < 250)) {
  analogWrite(RED, 255);

teller1不能同时大于500和<250。

评论后编辑:

void loop(){
    if (teller1 > 500) {
    //read sensor values
    teller1 = 0;
    }

    if (sensorValue > 20 && distance > 20) {
    //power led
    teller2 = 0;
    }
    if (sensorValue <20 || teller2 > 250){
    //unpower led
    }
}