Controlling LED-s with Potentiometer

Hello again.
I would like to control LED-s when i turn the potentiometer(if that's a right way to say it) LED's will be turning HIGH and opposite(something like that).I would really appreciate it if someone could write me a code to do this..And if someone will write the code please write it with no arrays or "for" commands or something like that.Thank you:)

P.S.- I would use 8 LED's.:slight_smile:

Unfortunately, no one here is going to right the code for you. You could pay someone to do it, but that would require you to post in the "Gigs and Collaborations" forum.

It seems that you were receive some value help in your other thread. I can tell you that you are not going to learn programming in a day, week or even a month.

But, what you are asking is fairly simple. A quick Google search turned up a nice tutorial on the subject.

Look it over and try to modify it for your LEDs. If you have any problems, ask your question here.

Not too sure on what you are asking for, from what I see you want something like a position display where as you turn the potentiometer the respective LED turns on? Something like this should work:

int led1 = 2; //led 1 is connected to pin #2 (avoid pins 0 and 1 as they are used for serial communication)
int led2 = 3; //led2 is connected to pin #3
int led3 = 4; //etc
int led4 = 5;

int pot = A0; // potentiometer is connected to analog pin 0
int value;

void setup(){
  pinMode(led1, OUTPUT); //assiging the led as an output
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(pot, INPUT); //assigning the potentiometer as an input
}

void loop(){
  value = analogRead(pot); /*assigning the variable "value" with the value of the potentiometer (0-1024) which is respective to (0-5v)
   since we have 4 leds and we want to break the potentiometer into 4 areas for each led, simply divide 1024/4 = 256 
   therefore every 256 increment will activate another led (256, 512, 768, 1024)  */
  if(value >= 0 && value < 256){
    digitalWrite(led1, HIGH);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  else if(value >= 256 && value < 512){
    digitalWrite(led1, LOW);
    digitalWrite(led2, HIGH);
    digitalWrite(led3, LOW);
    digitalWrite(led4, LOW);
  }
  else if(value >= 512 && value < 768){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, HIGH);
    digitalWrite(led4, LOW);
  }
  else if(value >= 768 && value < 1024){
    digitalWrite(led1, LOW);
    digitalWrite(led2, LOW);
    digitalWrite(led3, LOW);
    digitalWrite(led4, HIGH);
  }
}

This should work, I havent tested it. Expand on the code to your 8 leds and adapt it if you wanted to do something else with the leds. Good luck.

I can tell you that you are not going to learn programming in a day, week or even a month.

or at all if someone writes the code for you.

I asked for the code to explore it to learn how to do it so i can modify it and play with it.Thank you guys for your help:)

I myself was looking to do the same thing. Took me a couple of minutes but after combining a couple of existing codes, adding some things and removing some things here is what I have got. It controls an LED from "off" all the way up to "high" by adjusting the potentiometer.

int potPin = 2;    // select the input pin for the potentiometer
int led = 12;   // select the pin for the LED
int val = 0;       // variable to store the value coming from the sensor
int brightness = 0;
void setup() {
  pinMode(led, OUTPUT);  // declare the led as an OUTPUT
}

void loop() {
  val = analogRead(potPin);    // read value from potentiometer
  analogWrite (led, brightness);
  brightness = val;

You may need to map the values to match analogWrite

No mapping needed:

val = analogRead(potPin); // read 10-bit value from potentiometer
analogWrite (led, (val>>2) brightness); // write 8-bit value to PWM pin.
brightness = val;

Yeah I am brand new to these devices and this programming format. After posting I realized much of the code could be done away with, and made simpler. That's how we learn though.