Arduino Mini delay error

Hi,

First post here, I hope this is understandable.

I have a project with an arduino mini pro 328p 5V. The goal is to activate a motor for a short period of time just once when powered, several times a day. It is powered by a battery charger (1200mA, 5V) mounted on a manual timer.

I followed what was recommended to drive a motor with a relay (added a transistor and diode) to reduce strain on the arduino. There is a schematic attached or you can visit my tinkercad circuit.

Here is my code:

int pinMotor=10;//Motor pin
int timeTotalDay=20;//Time total activated for one day in second
int nbActivation=10;//Number of activation per day
unsigned long timeStart;
unsigned long currentTime;
const long delayActivated=timeTotalDay/nbActivation*1000;//Delay for one activation

void setup(){
  pinMode(pinMoteur,OUTPUT);
  pinMode(13,OUTPUT);
  pinMode(11,OUTPUT);
  timeStart=millis();
}

void loop(){
  currentTime=millis();
  if(currentTime-timeStart<delayFeed){
    digitalWrite(pinMoteur,HIGH);
    digitalWrite(13,HIGH);
  }else{
    digitalWrite(pinMoteur,LOW);
    digitalWrite(13,LOW);
    digitalWrite(11,HIGH);
    delay(1000);
  }
  delay(500);
}

Most of the time it works perfectly but sometimes (1 activation out of 15, randomly) the motor continues to run even if the time limit is reached and does not stop at all.

I was using a delay in the Setup() at first but since the problem was present I changed to this solution but it doesn't seem to change anything. Do you know what I did wrong?

Thanks in advance for your advices,
Louis

delay() and millis() do not belong in the same sketch together.

"delay()" probably does not belong in any sketch beyond flashing pretty lights - and even then ...

Hi,
Welcome to the forum.

Can you please post a circuit diagram, the Fritzy doesn't show component numbers, pinouts and how you have connected your relay.

Thanks.. Tom.. :slight_smile:

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