Friday, 12 June 2015

Potentiometer with LEDs and "if" commands


In this beginners project I will show you two key parts, one being a component the potentiometer and the other is a software command called "if".


*What is a potentiometer*

A potentiometer or "pot" as it is commonly know is a voltage divider (WIKI LINK) but rather than two preset resistors as you will find used in any old text book, this one you can change the resistance and measure the voltage out.

Potentiometers are used all over the place for all sorts of things, if you have volume controls in-line on headphone then that is possibly going to be one.




*The project*

 You will need:

  1. Arduino Uno / Nano
  2. Breadboard (half+ will do fine)
  3. LED; 1x green, 1x yellow and 1x red
  4. 10k potentiometer
  5. Jumper wires

 The Diagram:


The Code:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int PotPin = A5;                     //This is where you put in the Potentiometer middle pin
int val;                            //The Arduino will use this to store the read value
int ledGreen = 7;                   //Setting up the LEDs 
int ledRed = 11;
int ledYellow = 3;

void setup()
{
  pinMode(ledGreen, OUTPUT);        //Setting the outputs for the LED
  pinMode(ledRed, OUTPUT);
  pinMode(ledYellow, OUTPUT);
}

void loop()
{  
  {val = analogRead(PotPin);        //"Val" equals that is read from the PotPin
  val=map(val,0,677,0,8);           //See note below on mapping
  delay(1000);}                     //Set a delay of one second
  
  if (val > 6)                      //If "val" is equal or greater than 6 run code below
  {digitalWrite(ledGreen, HIGH);
  digitalWrite(ledRed, LOW);
  digitalWrite(ledYellow, LOW);}
  
  else if (val > 3)                 //If "val" is equal or greater then 3 run code below
  {digitalWrite(ledRed, HIGH);
  digitalWrite(ledGreen, LOW);
  digitalWrite(ledYellow, LOW);}
  
  else if (val < 3)                 //If "val" is less that 3 run the code below
  {digitalWrite(ledYellow, HIGH);
  digitalWrite(ledGreen, LOW);
  digitalWrite(ledRed, LOW);}
  
  else                              //If it is anything else turn it all off
  {digitalWrite(ledRed, LOW);
  digitalWrite(ledGreen, LOW);
  digitalWrite(ledYellow, LOW);}
}
Code layout from a website called hilite.me

Lets look a bit more in detail on the "map" part of the code as its important to mention what is going on.

Mapping;

val=map(val,0,667,0,8);
What the line above dose it to remaps the figures that the pot outputs and is read by the Arduino and sets it to a range that is more useful, by all means just use the raw output from the pot and work out the maths but it makes it a lot more manageable to use.

"Val" in out case is the value that the pot outputs we want to make "val" a number between 0 and 8 but currently its a range between 0 and 667.
First up in the bracket is "val" this is what your mapping, secondly is the raw minimum "0" input followed by "667" the maximum input, second to last is the "new" minimum you want "0" again and finally the maximum range "8" which we want to me the cap.


*Results* 

With the dial turned all the way up we get the green LED lit up;


 With the dial turned to the other end we get the yellow LED on;


 And floating on the middle the red LED;




And a photo of the whole thing!


No comments:

Post a Comment