#include <Arduino.h>
int ledRpin = 6;
int ledGpin = 5;
int ledBpin = 3;
int irsensorpin = 8;
int lightsensorpin = A0;
int ledR = 99;
int ledG = 187;
int ledB = 33;
bool irread;
int lightread;
void setup(){
Serial.begin(9600);
Serial.println("小夜灯开始工作!");
pinMode(ledRpin, OUTPUT);
pinMode(ledGpin, OUTPUT);
pinMode(ledBpin, OUTPUT);
pinMode(irsensorpin, INPUT);
}
void loop(){
irread = digitalRead(irsensorpin);
lightread = 1023 - analogRead(lightsensorpin);
if(irread == HIGH && lightread <= 512){
lightOn();
}else{
lightOff();
}
Serial.println("========分割线========");
if(irread == HIGH){
Serial.println("有人!");
}else{
Serial.println("无人...");
}
Serial.print("当前光照强度为:");
Serial.println(lightread);
delay(1000);
}
void lightOn(){
analogWrite(ledRpin, ledR);
analogWrite(ledGpin, ledG);
analogWrite(ledBpin, ledB);
}
void lightOff(){
analogWrite(ledRpin, 0);
analogWrite(ledGpin, 0);
analogWrite(ledBpin, 0);
}