2012-12-26 1 views
0

Arduino를 사용하여 빛이 있는지 여부를 알려주고 LDR을 사용하고 있습니다. 내 코드는 간단하지만 "light light light light"라는 스팸 대신 "light"라고 말하면 좋을 것입니다. 그리고 light가 꺼지면 "light not"라고 말합니다. "readanalogvoltage"에서 편집 코드 :Arduino IDE : 루프 내부를 한 번 인쇄하십시오.

void setup() { 
    // initialize serial communication at 9600 bits per second: 
    Serial.begin(9600); 
} 

// the loop routine runs over and over again forever: 
void loop() { 
    // read the input on analog pin 0: 
    int sensorValue = analogRead(A0); 

    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): 
    float voltage = sensorValue * (5.0/1023.0); 

    // print out the value you read: 
    if (voltage > 1.5) 
    { 
     Serial.print("Light"); 
     Serial.println(); 
     delay(5000); 
    } 

    if (voltage < 1.5) 
    { 
     Serial.print("No Light"); 
     Serial.println(); 
     delay(50); 
    } 

} 
+0

글로벌 부울을 사용 하시겠습니까? –

답변

4

마지막 상태를 유지하는 변수를 유지 : 시험의

void setup() { 
    // initialize serial communication at 9600 bits per second: 
    Serial.begin(9600); 
} 

int wasLit = 0; 

// the loop routine runs over and over again forever: 
void loop() { 
    // read the input on analog pin 0: 
    int sensorValue = analogRead(A0); 

    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): 
    float voltage = sensorValue * (5.0/1023.0); 

    // print out the value you read: 
    if (!wasLit && voltage > 1.5) { 
     wasLit = 1; 
     Serial.print("Light"); 
     Serial.println(); 
     delay(5000); 
    } else if(wasLit && voltage <= 1.5) { 
     wasLit = 0; 
     Serial.print("No Light"); 
     Serial.println(); 
     delay(50); 
    } 
} 
+0

dang ... 나를 때려 라 :) – Daniel

+0

그래, 위의 코드를 사용하면 빛의 상태가 충족 될 것으로 예상되는 경우 스팸을 계속 "빛" "빛 없음"으로 유지합니다 (물론 지연에 순응합니다). 그러나 그 위에 손가락을 올려 놓으면 "빛" "끊임없이 빛"이 스패밍되는 것을 멈추고 그저 "빛이 없다"라고 말합니다. – Leetfail

+0

@ user1914484 : 죄송합니다. 그 wasLit은 틀렸다. 고정. – Ryan

0

이런 종류의 히스테리시스의 사용에 도움이됩니다. 특히 형광등이있는 경우 약간의 깜박 거림이 있습니다. 센서 판독 값이 달라 지므로 깨끗한 방식으로 < 1.5에서> 1.5로 변경되지 않을 수 있습니다.

boolean bLast = false; 

const float vTrip = 1.5; 
// you find a good value for this by looking at the noise in readings 
// use a scratch program to just read in a loop 
// set this value to something much larger than any variation 
const float vHyst = 0.1; 

float getLight() { 
    int sensorValue = analogRead(A0); 
    // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): 
    float voltage = sensorValue * (5.0/1023.0); 
    return voltage; 
} 

void setup() { 
    // establish the initial state of the light 
    float v = getLight(); 
    bLast = (v < (vTrip-vHyst)); 
} 

void loop() { 
    float v = getLight(); 
    if(bLast) { 
     // light was on 
     // when looking for decreasing light, test the low limit 
     if(v < (vTrip-vHyst)) { 
      bLast = false; 
      Serial.print("Dark"); 
     } 
    } 
    else { 
     // light was off 
     // when looking for increasing light, test the high limit 
     if(v > (vTrip+vHyst)) { 
      bLast = true; 
      Serial.print("Light"); 
     } 
    } 
} 
관련 문제