Clock using Servos and Millis()

Hello community,

I am trying to do a clock seconds clock project which must be created with the servo and LEDs. So, as we know, the servo cannot turn beyond what is indicated in its specifications. For this reason, I need to use a loop to go to the maximum position of the servo (180°) and then returning to the origin (0°). The total cycle time must be configurable from a single variable and the travel speed of the servo must be determined. The maximum time for my clock is 60 seconds and there must be a row of 10 LEDs in which each LED equals 6 seconds. When the clock starts, the equivalent amount in units of time that each LED represents should light up and go off in sequence as time passes. If the configured time is less than 6 seconds then the LEDs do not light up. Finally, the use of the delay () function should be completely replaced by correctly implementing the millis() function to provide the same timing effect.


This is the code I currently have, but when I run it two things happen:

  1. The first LED turns on at 6 seconds (that’s great), but the others start to turn on every 3
    seconds. Each one of the 10 LEDs are supposed to turn on every 6 seconds the time reaches 60
    seconds.

  2. My servo goes to 180 perfectly, but when I comes to going back to the origin, it does not
    go back completely.

Any help/suggestions/optimization/ideas for the code are extremelly appreciated!

#include <Servo.h>

unsigned long startMillis = 0;
unsigned long currentMillis;
volatile unsigned long periodo = 0;
volatile unsigned long velocidad = 6000;
int ledPins = {2, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // Puertos de los LEDs
const int seis = 6000; // Variable para aumentar 6 segundos
const int minima = 0;
const int maxima = 60000;
int position = 0; // Posición inicial del servo

Servo MyServo;

int i = 0; // Indice de las LEDs

void setup() {

Serial.begin(115200);

startMillis = millis();

MyServo.attach(2, 1000, 2000);

pinMode(ledPins[0], OUTPUT); // ledPins[0] = 2
pinMode(ledPins[1], OUTPUT); // ledPins[1] = 4
pinMode(ledPins[2], OUTPUT); // ledPins[2] = 5
pinMode(ledPins[3], OUTPUT); // ledPins[3] = 6
pinMode(ledPins[4], OUTPUT); // ledPins[4] = 7
pinMode(ledPins[5], OUTPUT); // ledPins[5] = 8
pinMode(ledPins[6], OUTPUT); // ledPins[6] = 9
pinMode(ledPins[7], OUTPUT); // ledPins[7] = 10
pinMode(ledPins[8], OUTPUT); // ledPins[8] = 11
pinMode(ledPins[9], OUTPUT); // ledPins[9] = 12
pinMode(ledPins[10], OUTPUT); // ledPins[10] = 13

}

void loop() {

currentMillis = millis();
periodo = velocidad/2;

if (currentMillis - startMillis >= periodo) {

digitalWrite(ledPins_, !digitalRead(ledPins*));_
_
startMillis = currentMillis; _
_
i++;*_

* if(i == 11) { *

* MyServo.write(position);*
* position = position - 20;*
* i = 0;*
* }*

* if(position <= 0) {*

* position = 180;*

* }*
* }*
}
void velocidadMenos() {
* velocidad = velocidad + seis;*
* if (velocidad > maxima) {*
* velocidad = minima;*
* }*
}
void velocidadMas() {
* velocidad = velocidad + seis;*
* if (velocidad > maxima) {*

* velocidad = minima;*

* }*
}

And now with code tags, please

Hi Newbee,
here is a link with tips how to speed up getting good answers

as additional hint: add serial-output to your code to see what values the variables really have

best regards Stefan

const int maxima = 60000;

OOPS! Too much for an int (-32768 to 32767), use long (+ / - 2 billion).

const long maxima = 60000;

Read the guidelines. Edit your post to include code tags. Preferably use auto format first to tidy your code up. Add some serial print to debug. Make it easy for others to help you.

You have TWO functions that do exactly the same thing and you don't call either one of them. You can simplify your code by removing them both.

void velocidadMenos()
{
  velocidad = velocidad + seis;
  if (velocidad > maxima)
  {
    velocidad = minima;
  }
}


void velocidadMas()
{
  velocidad = velocidad + seis;
  if (velocidad > maxima)
  {


    velocidad = minima;


  }
}