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
28 Replies
AnonEngineering
AnonEngineering3mo ago
No description
PigPerOne
PigPerOneOP3mo ago
Alr gimme a sec
AnonEngineering
AnonEngineering3mo ago
set the Properties to Public
PigPerOne
PigPerOneOP3mo ago
It’s pending then it will be public I figured out the first problem after a while but I still cannot get the second
AnonEngineering
AnonEngineering3mo ago
iirc servos start up at 90 unless you tell them otherwise still need a link...
AnonEngineering
AnonEngineering3mo ago
No description
PigPerOne
PigPerOneOP3mo ago
#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); }
PigPerOne
PigPerOneOP3mo ago
No description
AnonEngineering
AnonEngineering3mo ago
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)
PigPerOne
PigPerOneOP3mo ago
Ok thank you However still don’t understand how to make the servo spin once all the lights turn on? In the void loop
AnonEngineering
AnonEngineering3mo ago
so when all three are lit spin to 180, then back to 0 once all 3 are off?
PigPerOne
PigPerOneOP3mo ago
Exactly In the middle of the loop where I have the if statement
AnonEngineering
AnonEngineering3mo ago
myservo.write(180); or myservo.write(0);, at the points you want it to move
PigPerOne
PigPerOneOP3mo ago
I mean I'll try it but so far that hasnt worked I plugged it in and it only worked one time around.
AnonEngineering
AnonEngineering3mo ago
you need to go to the project properties and make it public
PigPerOne
PigPerOneOP3mo ago
No description
AnonEngineering
AnonEngineering3mo ago
did you hit Save, then copy the new link?
No description
PigPerOne
PigPerOneOP3mo ago
i changed it to share link, try that.
AnonEngineering
AnonEngineering3mo ago
that worked, one minute like this? https://www.tinkercad.com/things/aVADcbb1XDs/editel
PigPerOne
PigPerOneOP3mo ago
that one is missing
PigPerOne
PigPerOneOP3mo ago
That workled
AnonEngineering
AnonEngineering3mo ago
you need 2 statements, one for one way, one to go back
PigPerOne
PigPerOneOP3mo ago
oh. yeah thats obvious. ok. im dumb thank you for your help lol
AnonEngineering
AnonEngineering3mo ago
np, have fun!
PigPerOne
PigPerOneOP3mo ago
thanks!
AnonEngineering
AnonEngineering3mo ago
for your consideration, using arrays can make the code more compact and less repetitious https://wokwi.com/projects/435037998886838273
PigPerOne
PigPerOneOP3mo ago
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

Did you find this page helpful?