3 Simple Ways to
Share What You Make

With Instructables you can share what you make with the world — and tap into an ever-growing community of creative experts.

PhotosPhotos

Share one or more photos of a project, recipe, or whatever you've made, quickly and easily.

Step by StepStep-By-Step

Share your step-by-step photos with text instructions of what you made so others can do it too!

VideoVideo

Share your how-to video. You'll need your embed code from a video site such as YouTube.

Intelligent Solar Garden Light - Part 2

Step 4Code

(Code hacked from an RGB LED cross fade example by Clay Shirky <clay.shirky@nyu.edu>).
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 StepDownload PDFView All StepsNext Step »

Pro

Get More Out of Instructables

Already have an Account?

close

All Steps Viewing
View all steps of an Instructable on the same page when you're a Pro Member.

Upgrade to Pro today!
8
Followers
7
Author:zzpza(All Alone in the Night)
I enjoy cycling and photography, and especially photography whilst cycling. :)