/*
Arduino | coding-help
Thread:Servo Help
PigPerOne June 28, 2025 — 3:14 PM
I have a honestly pretty dumb question but for whatever reason I cannot
figure out how to make this servo work correctly. I have a couple problems.
The first being that the servo always rotates 90 degrees no matter what
when the sim is first started. The second being that when I do put a write
function in the void loop it doesn’t work once nor loop with the lights.
Here is the tinkercad, help would be greatly appreciated.
https://www.tinkercad.com/things/bm19ASxNey4-dazzling-leelo/editel?returnTo=https%3A%2F%2Fwww.tinkercad.com%2Fdashboard%2Fdesigns%2Fcircuits
*/
#include <Servo.h>
Servo myservo;
const int PIN_RED1 = 11;
const int PIN_GREEN1 = 10;
const int PIN_BLUE1 = 9;
const int PIN_RED2 = 8;
const int PIN_GREEN2 = 7;
const int PIN_BLUE2 = 6;
const int PIN_RED3 = 4;
const int PIN_GREEN3 = 3;
const int PIN_BLUE3 = 2;
int red;
int green;
int blue;
void setColor(int R, int G, int B) {
analogWrite(PIN_RED1, R);
analogWrite(PIN_GREEN1, G);
analogWrite(PIN_BLUE1, B);
analogWrite(PIN_RED2, R);
analogWrite(PIN_GREEN2, G);
analogWrite(PIN_BLUE2, B);
analogWrite(PIN_RED3, R);
analogWrite(PIN_GREEN3, G);
analogWrite(PIN_BLUE3, B);
}
void setColor(int ledNum, int R, int G, int B) {
switch (ledNum) {
case 1:
analogWrite(PIN_RED1, R);
analogWrite(PIN_GREEN1, G);
analogWrite(PIN_BLUE1, B);
break;
case 2:
analogWrite(PIN_RED2, R);
analogWrite(PIN_GREEN2, G);
analogWrite(PIN_BLUE2, B);
break;
case 3:
analogWrite(PIN_RED3, R);
analogWrite(PIN_GREEN3, G);
analogWrite(PIN_BLUE3, B);
break;
}
}
void setup()
{
myservo.attach(5);
pinMode(PIN_RED1, OUTPUT);
pinMode(PIN_GREEN1, OUTPUT);
pinMode(PIN_BLUE1, OUTPUT);
pinMode(PIN_RED2, OUTPUT);
pinMode(PIN_GREEN2, OUTPUT);
pinMode(PIN_BLUE2, OUTPUT);
pinMode(PIN_RED3, OUTPUT);
pinMode(PIN_GREEN3, OUTPUT);
pinMode(PIN_BLUE3, OUTPUT);
}
void loop()
{
setColor(1, 255, 0, 0);
delay(500);
setColor(2, 0, 255, 0);
delay(500);
setColor(3, 0, 0, 255);
delay(500);
bool led1On = true;
bool led2On = true;
bool led3On = true;
if (led3On && led2On && led1On) {
led1On = false;
led2On = false;
led3On = false;
}
delay(500);
setColor(1, 0, 0, 0);
delay(500);
setColor(2, 0, 0, 0);
delay(500);
setColor(3, 0, 0, 0);
delay(500);
}