Controlling an LED and Servo with Potentiometer

Hello, I am very very new to Arduino. I was wondering how I could modify this code so that I can control the brightness of an LED as well as the servo with a potentiometer. When servo is counter clockwise, LED is dim or off. When servo is clockwise, LED is bright.

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott http://people.interaction-ivrea.it/m.rinott

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int potpin = 0; // analog pin used to connect the potentiometer
int val; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
}

Well, thats a pretty easy one actually. So what you will want to do is take the value from the potentiometer and put it into another "map" function, mapping it from 0 to 255. This will give you a value that you can then plug into the AnalogWrite function which will output a pwm onto one of the pwm pins. A value of 0 means the led is off, a value of 255 means its on, and anything in between will vary from dim to bright. Check out this, http://arduino.cc/en/Reference/AnalogWrite, page. It has lots of good stuff to get you started.

Does this look correct? Because it doesn't seem to be working. Thanks for your help.

// Controlling a servo position using a potentiometer (variable resistor)
// by Michal Rinott http://people.interaction-ivrea.it/m.rinott

#include <Servo.h>

Servo myservo; // create servo object to control a servo

int LED = 8;
int potpin = 0; // analog pin used to connect the potentiometer
int val=0; // variable to read the value from the analog pin

void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}

void loop()
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 0, 179); // scale it to use it with the servo (value between 0 and 180)
myservo.write(val); // sets the servo position according to the scaled value
delay(15); // waits for the servo to get there
val = map(val, 0, 1023, 0, 255);
analogWrite(LED, val);

}

I would use 2 map functions, one for the servo and the other for the LED. Don't use the same variable for the output of the maps.

void loop() {
   val = analogRead(potpin);
  int LED = map(val, 0, 1024, 0, 255);
  int SERVO = map(val, 0, 1024, 0, 179);
  myservo.write(SERVO);
  analogWrite(ledpin,LED);
  delay(20);
}

Try this

Bottom is some test code that prints the servo values to the serial monitor for testing and has a dead band provision to make trouble shooting easier.

//zoomkat dual pot/servo test 12-29-12
//view output using the serial monitor

#include <Servo.h> 
Servo myservo1;
Servo myservo2;

int potpin1 = 0;  //my pot pin
int potpin2 = 1;

int newval1, oldval1;
int newval2, oldval2;

void setup() 
{
  Serial.begin(9600);  
  myservo1.attach(2);  
  myservo2.attach(3);
  Serial.println("testing dual pot servo");  
}

void loop() 
{ 
  newval1 = analogRead(potpin1);           
  newval1 = map(newval1, 0, 1023, 0, 179); 
  if (newval1 < (oldval1-2) || newval1 > (oldval1+2)){  
    myservo1.write(newval1);
    Serial.print("1- ");
    Serial.println(newval1);
    oldval1=newval1;
  }

  newval2 = analogRead(potpin2);
  newval2 = map(newval2, 0, 1023, 0, 179);
  if (newval2 < (oldval2-2) || newval2 > (oldval2+2)){  
    myservo2.write(newval2);
    Serial.print("2- ");    
    Serial.println(newval2);
    oldval2=newval2;
  }
  delay(50);
}

ookid:
Well, thats a pretty easy one actually. So what you will want to do is take the value from the potentiometer and put it into another "map" function, mapping it from 0 to 255. This will give you a value that you can then plug into the AnalogWrite function which will output a pwm onto one of the pwm pins. A value of 0 means the led is off, a value of 255 means its on, and anything in between will vary from dim to bright. Check out this, http://arduino.cc/en/Reference/AnalogWrite, page. It has lots of good stuff to get you started.

but your solution to use servo library at all , so what is the usage of servo library if used AnalogWrite ?

Wow - two years and nine months.

What's your question?