I have a Problem with my Code. I try to do a Multithread with LED fading and Servo-control at the same time.
The fading has no problem as long as the servo is not attached. If the servo is attached the fading turns into a blinking and the servo is not controlled as it should be.
#include <Servo.h>
Servo servo1;
const int servoPin = A2;
const int ledPin1 = 9; // LED connected to digital pin 13
void setup() // run once, when the sketch starts
{
randomSeed (analogRead (0)); // randomize
pinMode(ledPin1, OUTPUT); // sets the digital pin as output
//servo1.attach(servoPin);
}
void loop() // run over and over again
{
LEDBlink();
servothread();
}
void LEDBlink(){
// fade in from min to max in increments of 5 points:
for (int fadeValue = 0 ; fadeValue <= 255; fadeValue += 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(50);
}
// fade out from max to min in increments of 5 points:
for (int fadeValue = 255 ; fadeValue >= 0; fadeValue -= 5) {
// sets the value (range from 0 to 255):
analogWrite(ledPin1, fadeValue);
// wait for 30 milliseconds to see the dimming effect
delay(50);
}
}
void servothread()
{
servo1.write(0);
servo1.write(180);
}