Code Version 1
int LEDpin = 9;
int PRpin = A0;
int PRval, LEDval;
void setup(){
pinMode(LEDpin, OUTPUT);
pinMode(PRpin, INPUT);
Serial.begin(9600);
}
void loop(){
readPR();
writeLED();
}
void readPR(){
PRval = analogRead(PRpin);
Serial.print("PRval = ");
Serial.print(PRval);
}
void writeLED(){
LEDval = map(PRval, 0, 450, 0, 255);
analogWrite(LEDpin, LEDval);
Serial.print(", LEDval = ");
Serial.println(LEDval);
}
Code Version 2
int LEDpin = 9;int PRpin = A0;
int PRval, LEDval, num;
void setup(){
pinMode(LEDpin, OUTPUT);
pinMode(PRpin, INPUT);
Serial.begin(9600);
}
void loop(){
readPR();
Serial.print("PRval = ");
Serial.print(PRval);
if(PRval < 10) num = 1;
else if(PRval > 300) num = 3;
else num = 2;
switch(num){
case 1: // PR is in shadow
Serial.println("shadow");
digitalWrite(LEDpin, LOW);
break;
case 2: // PR is in ambient light
Serial.println("ambient");
writeLED();
break;
case 3: // PR is in bright light
Serial.println("bright");
digitalWrite(LEDpin, LOW);
break;
}
delay(20);
}
void readPR(){
PRval = analogRead(PRpin);
}
void writeLED(){
LEDval = map(PRval, 0, 450, 255, 0);
analogWrite(LEDpin, LEDval);
}
No comments:
Post a Comment