Coding timer help me

can anyone help me to change this coding, so my coding is for run dc motor for every 16 hours....so anyone can help me to change the coding i want the motor run for every 5 minute

#define motorpin1 2
#define motorpin2 3

unsigned long previousTime = 0;
int Counter = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
}

void loop() {

  unsigned long currentTime = millis();

  if (currentTime - previousTime >= 10000) {

    Counter++;
    previousTime = currentTime;
  }

  if (Counter >= 5760) {
    Counter = 0;                                       // delay for 16 hours
    digitalWrite(motorpin1, HIGH);
    digitalWrite(motorpin2, LOW);

    delay(60000);         //On motor for 1 minute

    digitalWrite(motorpin1, LOW);
    digitalWrite(motorpin2, LOW);
  }


}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the </> icon above the compose window) to make it easier to read and copy for examination

Counter is incremented by 1 every 10 seconds by this part of the code

if (currentTime - previousTime >= 10000) {
  Counter++;
  previousTime = currentTime;
}

then when the Counter reaches 5760 you trigger some action in this code and reset the Counter

if(Counter >= 5760){
  Counter = 0;
  ... // some actions here

➜ so your action happens every 10s x 5760 = 57600 seconds which is 16 hours as you have 3600 seconds in one hour.

Now, how hard can it be to change de maths to be 5 minutes instead of 16 hours...

give it a try and post your solution with code tags (and fix also the first post please).

PS: side note, as the code is not doing anything else, just using delay would work...

conceptually you would just do:

loop:
  delay for 5 minutes
  trigger the action
  delay for the duration of the action
  stop the action
1 Like

consider
just posted this code for someone else who wanted to manage turning multiple lamps on/off

// demonstrate multiple lamp timer

enum { StOff, StOn };

struct Light {
    byte    pin;
    byte    onHour;
    byte    onMins;
    byte    offHour;
    byte    offMins;
    const char *desc;
    byte    on;
};

Light light [] = {
    { 10,  1, 30,  12,  0, "lamp0" },
    { 11,  3,  0,   9, 30, "lamp1" },
    { 12, 13,  0,  14, 30, "lamp2" },
    { 11, 11,  0,  14, 30, "lamp1" },
};
#define N_Light     (sizeof(light)/sizeof(Light))

enum { LedOff = HIGH, LedOn = LOW };

unsigned hour;
unsigned mins;

char s [80];

// -----------------------------------------------------------------------------
void loop ()
{
    unsigned long msec = millis ();

    mins = msec / 40;       // accelerate time for testing, 1hr = 2400 msec
    hour = mins / 60;
    hour = 24 < hour ? 1 : hour;
    mins = mins % 60;


    Light *l = light;
    for (unsigned n = 0; n < N_Light; n++, l++)  {
        if (l->on)  {
                    // compare mins range because of accelerated time
            if (hour == l->offHour && (mins - l->offMins) < 5)  {
                digitalWrite (l->pin, LedOff);
                l->on = false;

                sprintf (s, "  %2d:%02d  %3s  %s",
                    hour, mins, l->on ? "on" : "off", l->desc );
                Serial.println (s);
            }
        }
        else {
            if (hour == l->onHour && (mins - l->onMins) < 5)  {
                digitalWrite (l->pin, LedOn);
                l->on = true;

                sprintf (s, "  %2d:%02d  %3s  %s",
                    hour, mins, l->on ? "on" : "off", l->desc );
                Serial.println (s);
            }
        }
    }
}

// -----------------------------------------------------------------------------
void
setup (void)
{
    Serial.begin (115200);
    
    for (unsigned n = 0; n < N_Light; n++)  {
        digitalWrite (light [n].pin, LedOff);
        pinMode      (light [n].pin, OUTPUT);
    }
}
#define motorpin1 2
#define motorpin2 3

unsigned long previousTime = 0;
int Counter = 0;

void setup() {
  // put your setup code here, to run once:
  pinMode(motorpin1, OUTPUT);
  pinMode(motorpin2, OUTPUT);
}

void loop() {

  unsigned long currentTime = millis();

  if (currentTime - previousTime >= 10000) {

    Counter++;
    previousTime = currentTime;
  }

  if (Counter >= 5760) {
    Counter = 0;
    digitalWrite(motorpin1, HIGH);
    digitalWrite(motorpin2, LOW);

    delay(60000);         //On motor for 1 minute

    digitalWrite(motorpin1, LOW);
    digitalWrite(motorpin2, LOW);
  }


}

code tags like this right

code tags are right but you did not update the code to match the 5 minutes :slight_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.