Hey everyone, so I am completely new to Arduino, and I'm having some problems. So my goal is to make my servo motor rotate 12 degrees in 1 hour. This is my full code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
delay(36000ul) ;
write(12)
}
It says that I haven't defined write. How do I do that, and what else should I add onto my code? Thank you so much!!!!
You are missing a semicolon after write() and that might be confusing the complier. (Error messages aren't always that helpful.)
Your code delays (doing nothing) and then rotates to 12 degrees. I don't think that's what you want.
You should also set the starting position before any delay.
I think you want to rotate 1 degree at a time (1degree every 5 minutes), I don't think you can use fractional degrees but I'm not sure.) And you need to increment the angle. If you set it to 12 degrees twice it will just stay at 12 degrees, not move 12 degrees more.
For code development, shorten the delay so you don't have to wait so long to see if it's working. Maybe 12 degrees per minute or something like that.
So then how do you make it start at say 25 degrees, and then move 12 degrees more every hour? Like how would you code that? I'm sorry for asking so many questions.. I feel kinda bad now
Note: Since the servo can only go to 180, this will stop after about 12 hours.
Note: You will get smoother action if you move one degree every five minutes instead of 12 degrees every hour.
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 25; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(pos);
}
void loop()
{
delay(60ul * 60ul * 1000ul); // Wait one hour
pos += 12;
myservo.write(pos);
}
Hey, so I am trying to make a solar panel rotate a certain number of degrees every hour using a servo motor and arduino. Can you guys help me troubleshoot some problems and make sure my code is good?
This is my code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 0; // variable to store the servo position
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
delay(2000) ;
myservo.write(12);
}
void loop() {
delay() ;
myservo.write();
}
void loop() {
delay() ;
myservo.write();
}
Also, for the myservo.write(12), it ends up being more than 12 degrees, and it stops after like 2 turns, and also the turns are in opposite directions, I want them to be in the same direction.
Thank you so much!
I edited the last part so it wasn't void, also I want these to move in the same direction
loop(2) {
delay(2000) ;
myservo.write(12);
}
loop() {
delay() ;
myservo.write();
}
loop() {
delay() ;
myservo.write();
}
If someone answers, could you please give me the code and also explain it so I have both?? This is my first time using arduino, so I am struggling a lot right now, I hope to get better at it soon!!!!
Cross-posting is against the Arduino forum rules. The reason is that duplicate posts can waste the time of the people trying to help. Someone might spend a lot of time investigating and writing a detailed answer on one topic, without knowing that someone else already did the same in the other topic.
Repeated cross-posting can result in a suspension from the forum.
In the future, please only create one topic for each distinct subject matter. This is basic forum etiquette, as explained in the "How to get the best out of this forum" guide. It contains a lot of other useful information. Please read it.
Hey everyone, what is the code for resetting my servo motor back to it's original starting point? So it's finished its rotation all the way, and now it needs to just rotate all the way back to the start so it can redo the whole thing over again.
Here is my code:
#include <Servo.h>
Servo myservo; // create servo object to control a servo
int pos = 25; // variable to store the servo position
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(pos);
}
The problem is that the code you use just keeps on going, with the servo eventually hitting the end stop. That will damage your servo so limit it to the maximum value you want to use.
Then use
maxPos = // you put the value of the maximum position here
void loop() // the loop function takes in no argument
{
while(pos < maxPos){
delay(2000);
pos +=12;
myservo.write(pos);
}
// now send it back
pos = 25; // the place it started from
myservo.write(pos);
}