One thing that immediately jumps out at me is you are trying to power the servo from a digital pin. That is very very wrong.
The servo must be powered from the +5V pin (or +3.3V pin if it's a 3.3V servo) and only the "pulse" pin is connected to an Arduino's PWM capable IO pin.
Also, I don't know how the "Threads" library works, but it looks to me like it's not real threads, but a "round robin" execution of function with time delays. If one of your functions never returns (like your servo one) then the whole system will lock up. You need to make your servo function non-blocking like your LED functions. Give it a 15ms time setting and only move one step of the servo each time it gets called.
Ok, according to comments and research I can confirm that in fact the Thread library is really badly named. It doesn't run threads, it just runs different functions at different times. Those functions MUST be "single shot" - i.e., they MUST complete before anything else can happen. There is absolutely no concurrency whatsoever. It is entirely up to you to ensure that all your "threads" are fairly simple repetitive operations which are to be run at specific points in time.
Basically the operation is thus:
- Add a function and its time to a list.
- Work through the list to find a function whose time has expired
- Call that function
- Continue through the table
- Go back to the beginning
If you have two functions both with the same time then one function will be called first and execute to completion, then the other function will be called afterwards, to execute to completion. Only when a function returns is the system able to move on to the next entry in the function list.
You must not use delay() in your "thread" functions for anything more than maybe 1-2ms of delay in total. You MUST return from your thread with enough time for your next timed event to execute, otherwise it will be delayed, or never run at all.
So your servo thread would look something like this:
- Add my current "direction" to the servo value
- If the servo value < 0 or > 180 then invert the direction and limit the value to 0-180.
- Write the value to the servo
That is then triggered every 15ms by the thread library.
So as real code it may look like:
void runServo() {
static int direction = 1; // 1 = up, -1 is down
static int value = 0;
value += direction;
if ((value <= 0) || (value >= 180)) {
valuedirection = 0 - direction; // Change direction from + to - or - to +
}
value = max(min(180, value), 0); // Clamp it to between 0 and 180
servo.write(value);
}