빛을 벽에있는 서보의 입력으로 사용하는 프로젝트 프로젝트 질문에 오늘 인사드립니다. 빛이 방에 들어가고 그늘이 내려 가고 빛이 없으면 그늘이 물러날 것입니다. 조건 변경이없는 한 arduino 코드의 void 루프에서 두 번 조건을 한 번만 적용하고 싶습니다. 이 말을하면, 나는이 void loop가 계속 두 가지 상태를 유지하기를 원한다. 동일한 조건이 두 번 연속으로 충족되면 (즉, 800과 10000 사이의 센서 읽기, 5000과 6032 사이의 센서 읽기) 아무 것도 실행되지 않습니다. 하나의 조건이 충족 된 후 다른 조건이 충족되면 확인됩니다. 여기에 내 코드와 내가 사용해야 할 참조 명령이나 다음 행동 계획에 대한 도움이 있습니다. 먼저 귀하의 설정이 추가특정 조건에서 프로그램 중지
// Reports the frequency from the TSL230, higher number means brighter
// Part: http://www.sparkfun.com/products/8940
// Article: http://bildr.org/2011/08/tsl230r-arduino/
#include <Servo.h>
Servo myservo1;
int TSL230_Pin = 4; //TSL230 output
int TSL230_s0 = 3; //TSL230 sensitivity setting 1
int TSL230_s1 = 2; //TSL230 sensitivity setting 2
int TSL230_samples = 30; //higher = slower but more stable and accurate
void setup(){
Serial.begin(9600);
setupTSL230();
pinMode(5,OUTPUT);
}
void loop(){
float lightLevel = readTSL230(TSL230_samples);
Serial.println(lightLevel);
if(lightLevel>800 && lightLevel<1000)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1300);delay(1000);
myservo1.writeMicroseconds(1500);delay(5000000);
}
else if(lightLevel<800)
{
myservo1.attach(5);
myservo1.writeMicroseconds(1700);delay(5000);
myservo1.writeMicroseconds(1500);delay(5000000);
}
}
void setupTSL230(){
pinMode(TSL230_s0, OUTPUT);
pinMode(TSL230_s1, OUTPUT);
//configure sensitivity - Can set to
//S1 LOW | S0 HIGH: low
//S1 HIGH | S0 LOW: med
//S1 HIGH | S0 HIGH: high
digitalWrite(TSL230_s1, LOW);
digitalWrite(TSL230_s0, HIGH);
}
float readTSL230(int samples){
//sample light, return reading in frequency
//higher number means brighter
float start = micros();
int readings = 0;
while(readings < samples){
pulseIn(TSL230_Pin, HIGH);
readings ++;
}
float length = micros() - start;
float freq = (1000000/(length/samples)) * 10;
return freq;
}
위의 코드 (이 주석을 쓰고 있습니다). –