loading

This instructable will show you how to build an adjustable lamp that adjust how many leds that will be lit depending on how close or far away you choose to hold your hand above the sensor.

Remove your hand when you´ve decided how many leds you want to be lit. The leds will remain lit untill you eather move your hand above the sensor and change the range, or if you turn the ON/OFF switch OFF. Then as you might guess the leds will be off and also the sensor.

Step 1: Parts You Will Need

You will need a HC-SR04 sensor, 8 ledpins, 8 220 ohms resistance, a on/off switch or button and last but not least some wires.

Step 2: Building the Ciruit

Build the lamp according to the pictures.

Then intergrade the components into a simple plexiglas module or something nice visually.

Step 3: The Code

#define

trigPin 12

#define echoPin 13

#define led 8

#define led2 7

#define led3 6

#define led4 5 // defining all the leds

int state = 1;

boolean onOff = false;

void setup() {

Serial.begin (9600);

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

pinMode(led, OUTPUT);

pinMode(led2, OUTPUT);

pinMode(led3, OUTPUT);

pinMode(led4, OUTPUT);

pinMode(9,INPUT_PULLUP); // defining the leds as outputs

}

int threshold = 1000;

void loop() {

if (digitalRead(9) == HIGH){ //if the switch is on

onOff = true; // give the variable onOff the value true

Serial.println("HIGH"); //check if is it working in SerialPrint

}

else if (digitalRead(9) == LOW){ // if the switch is off

onOff = false; // give the variable onOff the value false

Serial.println("LOW"); //check if is it working in SerialPrint

}

if (onOff == true){ // if onOff is true, the leds will be effected by the motionsensor

long duration, distance;

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH);

distance = (duration/2) / 29.1;

Serial.println(distance); // if distance is closer than 10 cm, all the leds will shine

if (distance < 10) {

digitalWrite(led,HIGH);

digitalWrite(led2,HIGH);

digitalWrite(led3,HIGH);

digitalWrite(led4,HIGH);

}

else if(distance < 15) { // if distance is closer than 15 cm, all the leds will shine

digitalWrite(led,HIGH);

digitalWrite(led2,HIGH);

digitalWrite(led3,HIGH);

digitalWrite(led4,LOW);

}

else if(distance < 20) { // if distance is closer than 20 cm, all the leds will shine

digitalWrite(led,HIGH);

digitalWrite(led2,HIGH);

digitalWrite(led3,LOW);

digitalWrite(led4,LOW);

}

else if(distance < 30) { // if distance is closer than 30 cm, all the leds will shine

digitalWrite(led,HIGH);

digitalWrite(led2,LOW);

digitalWrite(led3,LOW);

digitalWrite(led4,LOW);

}

}

else if (onOff == false){ // if onOff is false, the LEDS are turned LOW

digitalWrite(led,LOW);

digitalWrite(led2,LOW);

digitalWrite(led3,LOW);

digitalWrite(led4,LOW);

}

}

About This Instructable

413views

4favorites

License:

More by mongorobin:Adjustable lamp   
Add instructable to: