Lighting up a toolcabin using a PIR


I'm starting to build a lightsystem in a toolcabin using a PIR to save power because it will be powered by battery. The idea is that when i put my hand in the cabin it will light up and will turn off just after i am done with it. I'm using: An Arduino Micro "copy" A 12V LED strip 10X Size C Battery A HC-SR501 PIR Sensor A 2N3904 Transistor A charger and A DC 2.5-32V LED Panel Voltage Meter 3-Digital for showing voltage...

//Connect PIR sensor to pin 9
//Connect NPN transistor to pin 10
//Connect the white wire of the voltmeter to pin 7

int val = 0;//sets value variable to 0

void setup() 
pinMode (10,OUTPUT); //pin 10 is the led pin
pinMode (9, INPUT); //pin 9 reads the high signal from the PIR
pinMode (7, OUTPUT); //pin 7 shows voltage

void loop () {
val = digitalRead(9); //Pin 9 is initiallly 0
digitalWrite(10,val); //Pin ten is equal to whatever pin 9 is
digitalWrite (7,val); //Showing the voltage of the cabin when the light is on
if (val == 1); //writes ten and seven high if PIR is high

digitalWrite(10,LOW); //led turns off
digitalWrite(7,LOW); //and the voltmeter
//Per Langkjær Febuary 22, 2014
//I have taken the code of Alex Rasekhi August 11,2011
//and modified it

I actually want to fade in the LED-string fast when it turns on and a little slower when it turns off. Do some off you have an idea to solve this?? and do you have some more ideas for it??

Thank you for all the help and ideas you can give me.

if (val == 1); //writes ten and seven high if PIR is high

If val is 1, do nothing (;). Otherwise, do nothing. Not too useful, I think.

No you are right, have to look at that too. But I'm still not sure how to code the fade in, fade out.

I know this might be a little basic but: can i "just" put in som extra code if i find something i like??

I know this might be a little basic but: can i “just” put in som extra code if i find something i like??

Answer is a “qualified maybe”… Assuming the piece you found implemented the exact logic you need, you would still more than likely have to change variable names and pin assignment. But more likely is that code you find won’t implement the exact logic you need, although it might give you a starting point.

Such a good answer and polite too :) thank you JimboZA. So code "A" with VoidSetup And VoidLoop Cannot directly be combined with code "B"? Have to look at the code in a couple of hours, but coud i take the code from the Fade toturial and use that?? Also, this should be in the Programming Forum, right?

Any given sketch can only have one setup function and one loop function, so your A & B example is correct. There have been a number of explanations on the forums in the past (by Crossroads and Grumpy_Mike among others IIRC) showing techniques you can use to combine them.

The fade example would be a good place to start, as you suggest.

Got to look into that, thank you WildBill. Seeing forwatd to my 2 hours 30 minutes bus ride home were I can sit and program :)

you may have figured it out by now ?

if you pwm the LED you can set the brightness.

since you start with it off, or 0, you need to increase the value to get to full brightness.

define what pwm pin you want to use, call it ledPin

int ledPin = 11; // pin11 is a PWM pin int ledLevel = 0; // starts with level a 0

void setup() { pinMode(ledPin, OUTPUT); } { ledLevel = ledLevel + 1; //increments the level by 1 each time. alter either steps or delay delay(10); // the higher the number, the longer it will take to reach full brightness if (ledLevel > 255) ledLevel = 255; // ledlevel has no stop, just rises. since PWM is 255 for max, you need to set max } analogWrite(ledPin, ledLevel); // controls the pin to the current level. will max in a few seconds either way on or off

you have to assign a pwm pin number to ledPin you have to figure out how/when to tell this to increase or decrease based on your switch(s) you could decrement ledLevel by 1 and increase by 5 to create different ramp speeds without changing delay

Hi Dave-in-NJ

I will have a look at it right away, just have my laptop now, not the boad so I can't test it. But I'll put it i the code at test it tomorrow. Really good, have to play a little with it, have to dare to play with it :-) Want it to fade in fast and out a little slower with a delay from last motion read.

Any ideas?? The delay at the last motion read should be around 10 seconds...

Stubled upon this tread: You think that this is better to work with?? Seems like it have at lot of debug features and pretty "smart" :-) It could easly be configured to my setup I think...

Modyfied: Started working with the other code now, much more fun to have a good base and work from there. (For me, with my small skills) I have to have something working so I can se how i should look. Will close this down for now and maybe open another tread in Programming if I can't get this to work. Thank you all for helping me and making me understand a little more...