Quantcast
Channel: Arduino Forum - Latest topics
Viewing all articles
Browse latest Browse all 15514

Inconsistent Delay function in loop?

$
0
0

Hello Everyone!
I am trying to create an autofeeder (nema) motor that dispense every 4 hours. However, the results in very inconsistent, i.e the autofeeder will dispense 8 hours or 9 hours while sometimes it is dispensing every 4 hours.

Refer video as attached (https://youtu.be/Q8V4OghVcqQ) timelapse of dispensing for 24 hours with a timer.

The A0 pin is connected to a potentiometer, what we want to adjust the amount of feed dispenses each time with the potentiometer, while the dispensing interval remains constant (every 4 hours). So far the accuracy of the dispensing is good, just the timing is a bit inconsistent.

int driverPUL = 7;    // PUL- pin
int driverDIR = 6;    // DIR- pin
int spd = A0;     // Potentiometer
boolean setdir = LOW; 
int pd =9371; //needs to be calculated from the automation box.xls file 
int minutes = (10); // how many minutes you need to dispense the feed, maybe can conside to map the analog function to the time.
unsigned long loops = (minutes * 3200);
unsigned long stopfeed = (14400000); //stop the feeder for 4 hours minutes (4*60*60*1000)

void setup() { //system will start and initialize to dispense x2 feed, with 10 sec interval. before going into the main feed 
  pinMode (driverPUL, OUTPUT);
  pinMode (driverDIR, OUTPUT);
  minutes = map((analogRead(spd)),0,1023,0,10);
  loops = (minutes * 3200);
  for (int i=0; i<loops; i++) {
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
  }
  delay(10000); //stop for 10 seconds and repeat
    for (int i=0; i<loops; i++) {
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
  }
  delay(10000); //stop for 10 seconds and repeat
}

void loop() {
  // put your main code here, to run repeatedly:
  minutes = map((analogRead(spd)),0,1023,0,10);
  loops = (minutes * 3200);
  for (int i=0; i<loops; i++) {
    digitalWrite(driverDIR,setdir);
    digitalWrite(driverPUL,HIGH);
    delayMicroseconds(pd);
    digitalWrite(driverPUL,LOW);
    delayMicroseconds(pd);
  }
  delay(stopfeed);
}

Is there any issues with the code? or I am better to stick with an DS1302 with a clock?

7 posts - 5 participants

Read full topic


Viewing all articles
Browse latest Browse all 15514

Trending Articles