声明arduino增加

时间:2014-09-16 20:05:35

标签: c++ c arduino arduino-uno

我有一个灯连接到连接到我的Arduino的x2光电阻器,当它们比它执行命令的限制更大时,它们有限制。

我想做的是让传感器执行其操作时更敏感,如果说它的值在一步中改变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);
}

1 个答案:

答案 0 :(得分:1)

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);

}