Servo Help
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
Tinkercad
Login - Tinkercad
28 Replies

Alr gimme a sec
set the Properties to Public
It’s pending then it will be public
I figured out the first problem after a while but I still cannot get the second
iirc servos start up at 90 unless you tell them otherwise
still need a link...

#include <Servo.h>
Servo myservo;
const int PIN_RED1 = 9;
const int PIN_GREEN1 = 10;
const int PIN_BLUE1 = 11;
const int PIN_RED2 = 6;
const int PIN_GREEN2 = 7;
const int PIN_BLUE2 = 8;
const int PIN_RED3 = 2;
const int PIN_GREEN3 = 3;
const int PIN_BLUE3 = 4;
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); }
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); }

do you have those RGB LEDs set to common cathode?
this is the code you sent https://wokwi.com/projects/435037612951079937
(I changed the LED pin numbers to make wiring easier)
Ok thank you
However still don’t understand how to make the servo spin once all the lights turn on?
In the void loop
so when all three are lit spin to 180, then back to 0 once all 3 are off?
Exactly
In the middle of the loop where I have the if statement
myservo.write(180);
or myservo.write(0);
, at the points you want it to moveI mean I'll try it but so far that hasnt worked
I plugged it in and it only worked one time around.
you need to go to the project properties and make it public

did you hit Save, then copy the new link?

i changed it to share link, try that.
that worked, one minute
like this? https://www.tinkercad.com/things/aVADcbb1XDs/editel
that one is missing
That workled
you need 2 statements, one for one way, one to go back
oh.
yeah thats obvious. ok.
im dumb thank you for your help lol
np, have fun!
thanks!
for your consideration, using arrays can make the code more compact and less repetitious
https://wokwi.com/projects/435037998886838273
Hm, that is interesting. I’ve had some experience with Java arrays but not much with c++ so I kinda tend to avoid them if I can but that is a good point. Thank you I will try to incorporate them into future projects