In: Computer Science
Try to control 3 LEDs by Potentiometer. Take 3 LEDs and connect it up the same way we use to do and connect the potentiometer to one of the analog pins. Observe the values coming out from it. Program the code such a way, when it runs, first, LED 1 should turn ON, then after a certain amount of value range, LED 1 should be OFF and LED 2 should be ON. Again, the same way, LED 2 should be OFF and LED 3 should be ON. The value ranges from 0-1023. It is up to you how these values should be divided into 3 parts. It would be nice if you would use Nested loops because there are more than two conditions.
using Arudrino and Sparkfun kit
const int potpin=A0;// potentiometer pin to connection const int led1=3;//led1,led2,led3 connections const int led2=4; const int led3=5;// int val=0;//variable to store pot values void setup() { // put your setup code here, to run once: pinMode(led1,OUTPUT); pinMode(led2,OUTPUT); pinMode(led3,OUTPUT); pinMode(potpin,INPUT); } void loop() { val=analogRead(potpin); //pot values divided as 0 to 341=led1 on,342 to 682=led2 on,683 to 1023=led3 on if(val>=0 && val<=341) { digitalWrite(led1,HIGH);//led1 is on digitalWrite(led2,LOW);//led2 is off digitalWrite(led3,LOW);//led3 is off } else if(val>=342 && val<=682) { digitalWrite(led2,HIGH);//led2 is on digitalWrite(led1,LOW);//led1 is off digitalWrite(led3,LOW);//led3 is off } if(val>=683 && val<=1023) { digitalWrite(led3,HIGH);//led3 is on digitalWrite(led1,LOW);//led1 is off digitalWrite(led2,LOW);//led2 is off } }