Step 4Code
N.B. The led controller PCB turns the LEDs on when low, not high.
=============================================
/*
- Intelligent Solar Powered Garden Light version 0.02 zzpza<at>truenames.co<dot>uk
- Original code example by Clay Shirky <clay.shirky<at>nyu<dot>edu>
int ledPin = 3; // LED, connected to digital pin 3
int ledVal = 0; // value to send to pin
int wait = 20; // 20ms (.02 second) delay; shorten for faster fades
int solarPin = 0; // analog pin solar panel is connected to
int solarVal; // value read from solarpanel
int PIRPin = 1; // analog pin PIR is connected to
int PIRval; // value read from PIR
int ledState = 1; // status of LED
int counter = 0; // timer for LED
void setup()
{
Serial.begin(9600);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, HIGH); // turn leds off
ledState=0;
delay(500);
}
void fadeDownLed()
{
digitalWrite(ledPin, LOW);
Serial.println("fadeDownLed");
for (int i=0; i <= 255; i++)
{
analogWrite(ledPin, i);
// Serial.print("fadeDownLed ");
// Serial.println(i);
delay(20);
}
digitalWrite(ledPin, HIGH);
ledState=0;
counter=0;
}
void fadeUpLed()
{
digitalWrite(ledPin, HIGH);
Serial.println("fadeUpLed");
for (int i=255; i >= 0; i--)
{
analogWrite(ledPin, i);
// Serial.print("fadeUpLed ");
// Serial.println(i);
delay(20);
}
digitalWrite(ledPin, LOW);
ledState=1;
}
void turnOffLed()
{
digitalWrite(ledPin, LOW);
ledState=1;
}
void turnOnLed()
{
digitalWrite(ledPin, HIGH);
ledState=0;
}
void readSolarPanel()
{
solarVal = analogRead(solarPin);
Serial.print("Solar: ");
Serial.println(solarVal);
}
void readPIR()
{
PIRval = analogRead(PIRPin);
Serial.print("PIR: ");
Serial.println(PIRval);
}
void loop()
{
readSolarPanel();
readPIR();
if (solarVal < 100 && ledState==0 && PIRval < 5)
{
fadeUpLed();
}
if (solarVal > 120 && ledState==1 && PIRval > 5)
{
fadeDownLed();
}
if (counter > 5000) // how long LEDs should be on for
{
fadeDownLed();
}
if (ledState == 1)
{
Serial.println(counter);
counter++;
}
}
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|








































