Go Down

Topic: Arduino Mini delay error (Read 125 times) previous topic - next topic

louisbzh

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:
Code: [Select]
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


aarg

delay() and millis() do not belong in the same sketch together.
  ... with a transistor and a large sum of money to spend ...
Please don't PM me with technical questions. Post them in the forum.

Paul__B

#2
Jan 21, 2021, 04:19 am Last Edit: Jan 21, 2021, 04:19 am by Paul__B
"delay()" probably does not belong in any sketch beyond flashing pretty lights - and even then ...

TomGeorge

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.. :)
Everything runs on smoke, let the smoke out, it stops running....

Go Up