I am trying to write a script for project I am working on involving servos and LEDs. My goal is to have the servo start at 0 and slowly rotate to 60 degrees continuously back and forth. I would like the LED to fade with from lowest to brightest based on the position of the servo arm. To reiterate, when the servo is at 0 degrees, the LED is at 0; when the servo is at 60 degrees, the LED is 255. It should fade to a fro based on the servo's degree.
Similar cases involve servos fading with variable speeds and led push button circuits, but nothing like this.
It seems super simple to me, but I keep getting caught up. Here is my script:
int ledPin = 2; #include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
pinMode(2, OUTPUT);
}
void loop()
{
for(pos = 0; pos < 60; pos += 1) // goes from 0 degrees to 60 degrees in steps of 1 degree
{myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
for(pos = 60; pos>=1; pos-=1) // goes from 60 degrees to 0 degrees
{myservo.write(pos); // tell servo to go to position in variable 'pos'
for(int fadeValue = 255 ; fadeValue >= 0; fadeValue -=5) // sets the value (range from 0 to 255):
{analogWrite(ledPin, fadeValue); } // wait for 30 milliseconds to see the dimming effect
delay(50); // waits 50ms for the servo to reach the position
}
}
I'm not sure i follow... I am still fairly new to this and I built this code with two existing examples.I feel like this should be a lot easier than I've coded it.
If we read you correctly, all you want is the brightness of the LED to indicate the position of the servo? So all you need to do is multiply the position by 4 (ok, 255/60 = 4.25 strictly) and set the LED PWM every time the servo moves. (fadeValue= 255 * pos / 60)
for(pos = 0; pos < 60; pos += 1) // goes from 0 degrees to 60 degrees in steps of 1 degree
{myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(50); // waits 50ms for the servo to reach the position
}
for(pos = 60; pos>=1; pos-=1) // goes from 60 degrees to 0 degrees
{myservo.write(pos); // tell servo to go to position in variable 'pos
delay(50); // waits 50ms for the servo to reach the position
}
}
Would I need to repeat those LED conditions for both the 0-60 and 60-0 servo movements or will it work being inside of just one of the for loops?
void loop()
{
for(pos = 0; pos < 60; pos += 1) // goes from 0 degrees to 60 degrees in steps of 1 degree
{myservo.write(pos); // tell servo to go to position in variable 'pos'
int fadeValue
(fadeValue= 255 * pos / 60);
analogWrite(ledPin, fadeValue);
delay(50); // waits 50ms for the servo to reach the position
}
for(pos = 60; pos>=1; pos-=1) // goes from 60 degrees to 0 degrees
{myservo.write(pos); // tell servo to go to position in variable 'pos
delay(50); // waits 50ms for the servo to reach the position
}
}
The LED comes shuts off at 0 and then comes on very dull for the duration of the sweep.. no fading.
I understand why we multiply 255 by the position and divide by 60 (total range of movement), this allows us to get the full range of brightness of the LED.
I also understand putting the LED fade statements inside of the for loops of the servo, this allows the fadeAmount see the actual position of the servo while it is moving.
After reviewing PaulS comments and JimboZA,
I have arrived at this loop:
void loop()
{
for(pos = 0; pos < 60; pos += 1) // goes from 0 degrees to 60 degrees in steps of 1 degree
{myservo.write(pos); // tell servo to go to position in variable 'pos'
int fadeValue = 255 * pos / 60;
analogWrite(ledPin, fadeValue);
delay(50);} // waits 50ms for the servo to reach the position
for(pos = 60; pos>=1; pos-=1) // goes from 60 degrees to 0 degrees
{myservo.write(pos); // tell servo to go to position in variable 'pos
int fadeValue = 255 * pos / 60;
analogWrite(ledPin, fadeValue);
delay(50);} // waits 50ms for the servo to reach the position
}
To me, this loop makes sense (still learning );however the LED does not fade.