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.
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.
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);
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 ?