February 24, 2013

Siren light, or controlling 6 LEDs with a potentiometer

Introduction

Yesterday I've got a variable resistance (aka potentiometer) from an old car radio, and today morning my brother (age 5) asked me to build a robot with him. So we built a controllable siren:

As you can see there is much more wire than necessary. Well, that was my brother's part of the project :D

Purpose of the project

As seen above, if you turn the potentiometer the lights will "run" slower or faster.

What you need:

6 LEDs, if possible reds and blues
a potentiometer
an Ardunio microcontroller and a breadboard with wires

First step: wiring

Connect the 6 LEDs to the ground and to the pin 2,4,6,8,10,12.
Connect the middle pin of the potmeter to the pin A2 (analog input) and the other 2 to 5V and GND. (More info about potmeter in this tutorial: http://www.arduino.cc/en/Tutorial/Potentiometer)

Second step: programming

Here is the code:

int val = 0;       // variable to store the value coming from the sensor

void setup() {
  pinMode(2,OUTPUT);
  pinMode(4,OUTPUT);
  pinMode(6,OUTPUT); 
  pinMode(8,OUTPUT);
  pinMode(10,OUTPUT);
  pinMode(12,OUTPUT);
  Serial.begin(9600);
}

void loop() {
  val = analogRead(2)+1000;    // read the value from the sensor, and add 1000, because if it's too small, we can't see anything
  Serial.println(val);        //write it on serial monitor

for(int i=1; i < 7; i++)
  {
   digitalWrite(i*2,HIGH);
   delay(val/15);
   digitalWrite(i*2,LOW);  
  }
for(int i=6; i > 0; i--)
  {
   digitalWrite(i*2,HIGH);
   delay(val/15);
   digitalWrite(i*2,LOW);  
  }
}

Best regards:
Mark

No comments:

Post a Comment