Hello,
I am adding electronics to my scratch-build models. I have an Arduino Every with a micro servo attached. I used sweep code to verify the hardware and connections work. The servo runs landing gear and with basic code they move too fast from 25 degrees to 100 and back. This code is my attempt at non-blocking code to just test the landing gear and see if I can get them to slow down. Then I'll add it to more code later with remote control as void () landing gear; That's a whole other ball of wax...
I set the servo to 25 degrees and start the following code and it moves quickly to 100 degrees and stops. I know I had it working once, but maybe that was different code. I have a thick skull when it comes to logic, so I was hoping someone could point me in the right direction. Thank you in advance. Here's the code I hope I added it correctly to the post:
#include <Servo.h>
//some global variables available anywhere in the program
unsigned long currentMillis;
int interval = 50;
unsigned long previousMillis;
int current = 25;
int target = 100;
Servo myservo; // create servo object to control a servo
// twelve servo objects can be created on most boards
void update()
{
if (millis() - previousMillis > interval)
{
previousMillis = millis();
if (target < current)
{
current--;
myservo.write(current);
}
else if (target > current)
{
current++;
myservo.write(current);
}
}
}
void setup() {
myservo.attach(9); // attaches the servo on pin 9 to the servo object
}
void loop() {
update;
}
Hey gcjr,
I'm sure the code works great but you completely lost me! I don't know what struct is just for starters. Is Ctl ctl [ ] an array? I'll spend more time looking at it.
Servo is 100 degrees for gear down, 25 for gear up. That's not the problem. Like I said, using the regular sweep code, and obviously putting in 100 and 25, works perfectly. It's just too fast.
xfpd,
I'm just trying to figure out how to slow the servo down. One step at a time. After I wrap my head around that I can use ir remote button and re-write. This is just a test code for slowing down servo.
Hey everyone,
I'm an absent-minded dunderhead. I found the code that works I based on someone else's idea I had adapted for my test. I'm coming back to this after a couple months off and looked at my flawed first attempt. In case a newbie like me has the same problem, here's the final code that works.
#include <Servo.h>
Servo myservo;
int position = 25;
unsigned long ts = millis();
unsigned long interval = 50;
boolean forward = false;
void setup()
{
myservo.attach(9);
}
void loop()
{
if (millis() - ts >= interval)
{
ts += interval; //setup timestamp for next time
if (forward)
{
myservo.write(-- position); //progress servo
if (position == 25) //test for reverse
forward = false;
}
else
{
myservo.write(++ position); //progress servo
if (position ==100) //test for reverse
forward = true;
}
}
}
Sounds like things are under control; in the future it’s always best to show us a schematic and images of your project so we can be brought up to speed.
instead of separate arrays for each thing, a struct defines a group of variables and you can have an array of that struct. all the values in an array of a struct can be initialized, one struct on each line