gpt4 book ai didi

c++ - 递增if语句arduino

转载 作者:行者123 更新时间:2023-11-28 06:41:29 26 4
gpt4 key购买 nike

我有一盏灯连接到一个 x2 光敏电阻器,该光敏电阻器连接到我的 Arduino 我将它们设置为一个限制,当它们大于限制时它就会执行它的命令。

我想做的是让传感器执行其操作,如果说它的值在一个步骤中变化了 5,而不是等待它达到极限,从而使其更加敏感。

建议和一点帮助将不胜感激。

int sensorPin1 = A5;
int sensorPin2 = A4;
int ledPin2 = 7;
int ledPin1 = 8;
int moto = 12;

void setup() {
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(moto, OUTPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(moto, LOW);
}

int limit = 450;

void loop() {
// read the value from the sensor:
int sensorValue1 = analogRead(sensorPin1);
int sensorValue2 = analogRead(sensorPin2);
int sensorValue1a = analogRead(sensorPin1 - 2);
int sensorValue2a = analogRead(sensorPin2 - 2);

if (sensorValue1 > limit) {
digitalWrite(moto, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
}

delay(0);

if (sensorValue2 > limit) {
digitalWrite(moto, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
}

Serial.println(sensorValue1);
// delay(100);
}

最佳答案

int sensorPin1 = A5;
int sensorPin2 = A4;
int ledPin2 = 7;
int ledPin1 = 8;
int moto = 12;
int sensorValue1last = 0;
int sensorValue2last = 0;

void setup() {
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(moto, OUTPUT);
pinMode(sensorPin1, INPUT);
pinMode(sensorPin2, INPUT);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, HIGH);
digitalWrite(moto, LOW);
}

int limit = 450;

void loop() {

// read the value from the sensor:
int sensorValue1 = analogRead(sensorPin1);
int sensorValue2 = analogRead(sensorPin2);
int sensorValue1a = analogRead(sensorPin1 - 2);
int sensorValue2a = analogRead(sensorPin2 - 2);
int diff1 = sensorValue1-sensorValue1last;
int diff2 = sensorValue2-sensorValue2last;

if ( sensorValue1 > limit || diff1 > 5 ) {
digitalWrite(moto, LOW);
digitalWrite(ledPin1, LOW);
digitalWrite(ledPin2, HIGH);
sensorValue1last = sensorValue1;
}

delay(0);

if ( sensorValue2 > limit || diff2 > 5 ) {
digitalWrite(moto, HIGH);
digitalWrite(ledPin1, HIGH);
digitalWrite(ledPin2, LOW);
sensorValue2last = sensorValue2;
}

Serial.println(sensorValue1);
// delay(100);

}

关于c++ - 递增if语句arduino,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25877510/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com