Go Down

Topic: controlling led brightness w/ pot (Read 11095 times) previous topic - next topic

lyrical10

I thought the code below would let me control an led's brightness with a potentiometer, but a guess I was wrong because it doesn't work.  Could someone help me with this simple task?

int knobvalue;

void setup()
{
  pinMode(11, OUTPUT);
}

void loop()
{
  knobvalue = analogRead(0);
  knobvalue = map(knobvalue, 0, 1023, 0, 255);
  analogWrite(11, knobvalue);
}

rlwoodjr

How are you hooking up the  potentiometer and led?

lyrical10

Nevermind.  Stupid mistake.  I got it working.

int knobvalue;
int potpin = 0;

void setup()
{
  pinMode(11, OUTPUT);
}

void loop()
{
  knobvalue = analogRead(potpin);
  knobvalue = map(knobvalue, 0, 1023, 0, 255);
  analogWrite(11, knobvalue);
}

PaulS

Take a look at the source code for the map function, sometime. It is a relatively inefficient means of dividing by 4.

Code: [Select]
analogWrite(11, analogRead(potPin)/4);
would accomplish the same thing that all your code in loop is doing, and do it faster.
The art of getting good answers lies in asking good questions.

Go Up