Hello ! I have started Arduino a year ago and currently working on a basic robot project : When a pushbutton is pressed it triggers flashing LEDs and the robot arm waves up and down for a set period of time - The idea is for the arm to start and finish at the same position until the pushbutton is pressed again. At the moment I am ensuring this by matching the total duration of the action (led flashing and arm waving) with : degrees x Time to move 1 degree.
My code is working although I encounter 2 problem,
1.sometimes the arm does not start and finish at the same positon (it generally rush to one angle and then stabilize to regular speed)
Is this due to the surge of current incoming after the button is pressed ? Should I put a capacitor if so where and which capacitance ? (I will have 12 servos in parallel at some point shariong 1 data signal)
2/ aftter an extended period of incativity, the servo does not react (the LEDs do)
Thanks!!!
Gif here : Imgur: The magic of the Internet
Code here ;
#include<Servo.h>
Servo myservo;
const int buttonPin = 8;
const int ledPin = LED_BUILTIN;int pos = 0; // variable to store the servo position
int direction = 0; // variable to stor servo directionunsigned long previousMillis = 0;
int ledState = LOW;
bool isPressed = false;
const long actionDuration = 6080; // duration of led and servo function
const long onDuration = 1000; // led ON Duration
const long offDuration = 1000; // led OFF Duration#definerunEvery(t) for (static typeof(t) _lasttime; (typeof(t))((typeof(t))millis() - _lasttime) > (t); _lasttime += (t))
void setup()
{
myservo.attach(9); // attaches the servo on pin 9 to the servo object
myservo.write(pos);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}void loop()
{
if (digitalRead(buttonPin) == HIGH && isPressed == false)
{ //button is pressed AND this is the first digitalRead() that the button is pressed
Serial.println("button is pressed");
isPressed == true;
unsigned long startTime = millis();
Serial.println("Action starts");
while ((millis() - startTime) < actionDuration)
{ // trigger action
servo();
led();
}
isPressed == false;
Serial.println("Action over");
}
}void servo()
{
runEvery(10) //run every 10 milliseconds
{
if (pos < 60 && direction == 0)
{
pos = pos + 1;
}
if (pos > 0 && direction == 1)
{
pos = pos - 1;
}
}if (pos == 60)
{
direction = 1;
}
if (pos == 0)
{
direction = 0;
}
myservo.write(pos);
Serial.print("servo position is :");
Serial.println(pos);
}void led()
{if (ledState == HIGH)
{
if ((millis() - previousMillis) >= onDuration)
{
ledState = LOW; // change the state of LED
digitalWrite(ledPin, ledState); // set initial state
previousMillis = millis(); // remember Current millis() time
}
}
else
{
if ((millis() - previousMillis) >= offDuration)
{
ledState = HIGH; // change the state of LED
digitalWrite(ledPin, ledState); // set initial state
previousMillis = millis(); // remember Current millis() time
}
}
}