A Smart Night Lamp for Kids
วงจร
Code :
//ตั้งค่าตัวแปร
int photoSensorPin = A0;
int redLedPin = 3;
int greenLedPin = 5;
int blueLedPin = 6;
int redLedVal = 0;
int greenLedVal = 0;
int blueLedVal = 0;
boolean isLedOn = false;
int brightness = 1023;
int darkThreshold = 50;
int changeColorTime = 900000;
long lastUpdate=0;
void setup()
{
randomSeed(analogRead(1));
}
void loop()
{
getBrightness(); //รับค่าBrightness
if (brightness<darkThreshold){ if (!isLedOn) { LedOn(); isLedOn = true; lastUpdate = millis(); } else if (millis()>lastUpdate+changeColorTime)
{
LedOn();
isLedOn = true;
lastUpdate = millis();
}
}
else {
LedOff();
isLedOn = false;
}
delay(1000);
}
int getBrightness()
{
brightness = analogRead(photoSensorPin);
return brightness;
}
void LedOn()
{
redLedVal = random(1,128);
greenLedVal = random(1,128);
blueLedVal = random(1,128);
analogWrite(redLedPin,redLedVal);
analogWrite(greenLedPin,greenLedVal);
analogWrite(blueLedPin,blueLedVal);
}
void LedOff(){
analogWrite(redLedPin,0);
analogWrite(greenLedPin,0);
analogWrite(blueLedPin,0);
}
credit by : http://runtimeprojects.com/2016/05/a-smart-night-lamp-for-kids/
ความคิดเห็น
แสดงความคิดเห็น