Fish Feeder TIMER CODE HELP:)

Hello all:) I am new here and new to programing so please know that when giving your answers.

I have a fish feeder case that I printed out and now I just need to get the motor going on a 24 hour loop. I already typed out the code to set the speed of the motor so I can adjust the amount of fish food but I still need the timer. I really have no idea how to do this so some guidance would be great! Below is what I have so far.

  1. Step Motor 28BYJ-48 5v DC along with a ULN2003 Driver Board.
  2. Arduino Uno
  3. 3D printed parts put together. (https://www.instagram.com/p/BviGD-aFfv2/?utm_source=ig_web_copy_link)
int bluePin = 2;
int pinkPin = 3;
int yellowPin = 4;
int orangePin = 5;

int currentStep = 0;

void setup() {
 // put your setup code here, to run once:

 pinMode(bluePin,OUTPUT);
 pinMode(pinkPin,OUTPUT);
 pinMode(yellowPin,OUTPUT);
 pinMode(orangePin,OUTPUT);

 digitalWrite(bluePin,LOW);
 digitalWrite(pinkPin,LOW);
 digitalWrite(yellowPin,LOW);
 digitalWrite(orangePin,LOW);
}

void loop() {
 // put your main code here, to run repeatedly:

 switch(currentStep){
   case 0:
     digitalWrite(bluePin,HIGH);
     digitalWrite(pinkPin,LOW);
     digitalWrite(yellowPin,LOW);
     digitalWrite(orangePin,LOW);
     break;
   case 1:
     digitalWrite(bluePin,LOW);
     digitalWrite(pinkPin,HIGH);
     digitalWrite(yellowPin,LOW);
     digitalWrite(orangePin,LOW);
     break;
   case 2:
     digitalWrite(bluePin,LOW);
     digitalWrite(pinkPin,LOW);
     digitalWrite(yellowPin,HIGH);
     digitalWrite(orangePin,LOW);
     break;
   case 3:
     digitalWrite(bluePin,LOW);
     digitalWrite(pinkPin,LOW);
     digitalWrite(yellowPin,LOW);
     digitalWrite(orangePin,HIGH);
     break;

 }
 
 currentStep = (++currentStep < 4) ? currentStep : 0;

 delay(200);

}

Moderator edit:
</mark> <mark>[code]</mark> <mark>

</mark> <mark>[/code]</mark> <mark>
tags added.

Other thread removed as requested.

Do you know what an RTC (real time clock) is?

Do a Google search on:
DS3231 ARDUINO

Thanks! Any help on coding what I need for this fish feeder? I just need a 24hour timer in the code:)

That code never stops moving your motor... You might be better off using a library. The CheapStepper was written for this motor.

As for a timer, see the example in the IDE "Blink without delay" for how to determine how much time has elapsed without blocking your code.

larryd:
Do you know what a RTC (real time clock) is?

Do a Google search on:
DS3231 ARDUINO

Yes I kind of do but is it needed just to set a 24 hour time? I basically need it to start the stepper motor once a day.

An RTC as mentioned would be the best way to go if you need repeatability in timing.
A DS3231 is about $2 to $3 on eBay.
I find they are accurate to about +-3 seconds per month.

If you do not need accurate timing then you can use the built in millis() function to create the timing needed.

There is an example in the IDE called BWD (blink without delay) that is used to create nonblocking timing.

Take a look at the example, we can help you with things you have problems with.

An example of BWD:

//Blink without Delay skeleton 
//4 examples demonstrated
//

//LED wiring options
//=============================================
//Depending which way your LEDs are wired, uncomment the next line.
//#define PlusEqualsON 

#ifdef PlusEqualsON
//wired so +5V turns LED ON
#define ledON  HIGH
#define ledOFF LOW
//=========================
#else
//wired so +5V turns LED OFF
#define ledON  LOW
#define ledOFF HIGH
//=========================
#endif

//switch wiring options
//=============================================
//Depending which way your switches are wired, uncomment the next line.
#define PushEqualsLOW 

#ifdef PushEqualsLOW 
//pushing the switch makes pin LOW
#define Pushed   LOW
#define Released HIGH
//=========================
#else
//pushing the switch makes pin HIGH
#define Pushed   HIGH
#define Released LOW
//=========================
#endif

//=============================================
unsigned long currentMillis;
unsigned long pin13Millis;
unsigned long pin12Millis;
unsigned long pin11Millis;
unsigned long SwitchMillis;

//if these are not changed in the sketch, they can be const
unsigned long debounceMillis = 100UL;     //100ms
unsigned long ledOnTime      = 5*1000UL;  //5 seconds

byte laststartSwitchState    = HIGH;
byte buttonState             = HIGH;
byte counter                 = 0;

//the following are enable/disable flags
//some of these might not be used in this sketch
boolean flag13 = true;
boolean flag12 = true;
boolean flag11 = true;
boolean flag10 = true;

const byte startSwitch = 2; //pushed = LOW
const byte testSwitch  = 3; //pushed = LOW

//**********************************************************************

void setup()
{
  Serial.begin(9600);
  
  digitalWrite(13,ledOFF);
  pinMode(13, OUTPUT); 

  digitalWrite(12,ledOFF);
  pinMode(12, OUTPUT);

  digitalWrite(11,ledOFF);
  pinMode(11, OUTPUT);
  
  digitalWrite(10,ledOFF);
  pinMode(10, OUTPUT);

  pinMode(startSwitch, INPUT_PULLUP); //pushed = LOW
  pinMode(testSwitch,  INPUT_PULLUP); //pushed = LOW

} //  >>>>>>>>>>>>>> E N D  O F  s e t u p ( ) <<<<<<<<<<<<<<<<<

void loop()
{
  //save the current time
  currentMillis = millis();

  //************************************* E x a m p l e  1
  //toggle pin 13 every 200mS
  //has 200ms or more gone by?
  if (currentMillis - pin13Millis >= 200UL)
  {
    //code here runs every 200ms
    //get ready for next iteration
    pin13Millis = pin13Millis + 200UL;
    //toggle pin 13
    digitalWrite(13,!digitalRead(13));
  }

  //************************************* E x a m p l e  2
  //at power up, pin 12 LED goes ON, after 3 seconds goes OFF and stays OFF
  //could be used as a powerup reset signal
  if (flag12 == true && currentMillis - pin12Millis <= 3000UL)
  {
    //code here runs for 3 seconds after power up, then stops
    digitalWrite(12,ledON);
  }
  else
  {
    digitalWrite(12,ledOFF);
    //disable further pin 12 control
    flag12 = false;
  }

  //************************************* E x a m p l e  3
  //if testSwitch is pushed and released
  //pin 11 LED goes ON for 5 seconds, then goes OFF 
  buttonState = digitalRead(testSwitch);
  
  //are we are allowed to check the switch and is it pressed?
  if(flag11 == true && buttonState == Pushed)
  {    
    //enable timing of LED on pin 11
    flag11 = false; //false --> timing is enabled
    //turn LED ON
    digitalWrite(11,ledON);
    //record the time LED turned ON
    pin11Millis = currentMillis;
  }
    
  //are we allowed and is it time to control pin 11
  if (flag11 == false && currentMillis - pin11Millis >= ledOnTime)
  {
    //if enabled, code here runs after ledOnTime ms goes by
    digitalWrite(11,ledOFF);
    //allow switch press detection again
    flag11 = true; //true --> switch monitoring is enabled
  }

  //************************************* E x a m p l e  4
  //is it time to check the switches?
  //in particular, pushing startSwitch will turn ON/OFF (toggle) an output pin 10
  //is it time to check the switches
  if (currentMillis - SwitchMillis >= debounceMillis)
  {
    //code here runs every debounceMillis ms
    //get ready for the next iteration
    SwitchMillis += debounceMillis; 
    //go and check the switches
    checkSwitches();    
  } 

  //*********************************
  //put other non-blocking stuff here
  //*********************************

} //  >>>>>>>>>>>>>> E N D  O F  l o o p ( ) <<<<<<<<<<<<<<<<<


//======================================================================
//                      F U N C T I O N S
//======================================================================


//****************** c h e c k S w i t c h e s ( ) *********************
//switches are checked every debounceValue milli seconds 
//no minimum switch press time is validated with this code (i.e. No glitch filter)
void checkSwitches()  
{
  //re-usable for all the switches  
  boolean thisState;    

  //************************************* E x a m p l e  Push ON push OFF (toggle)   
  //check if this switch has changed state
  thisState = digitalRead(startSwitch);
  if (thisState != laststartSwitchState)
  {  
    //update the switch state
    laststartSwitchState = thisState;  

    //this switch position has changed so do some stuff

    //"HIGH condition code"
    //switch went from LOW to HIGH
    if(thisState == HIGH)        
    {
      //Do some HIGH switch stuff here
    }

    //"LOW condition code"
    //switch went from HIGH to LOW
    else                          
    {
      //Do some LOW switch stuff here  
      digitalWrite(10, !digitalRead(10));
      //print number of pushes
      counter++;
      Serial.println(counter);
    }

  } //END of startSwitch code

  //*****************************************  
  //similar code for other switches goes here 
  //*****************************************  

} //END of checkSwitches()

//**********************************************************************

//======================================================================
//                      E N D  O F  C O D E
//======================================================================

homescienceideas:
Yes I kind of do but is it needed just to set a 24 hour time? I basically need it to start the stepper motor once a day.

Don't you need to stop it too?

Hello,

It looks like your program takes roughly 800mS to cycle through one rotation. You could deduct 800 mS from 24 hours worth of milliseconds, (86400000)-(800) = 86399200 mS.

I'm not sure how many times this would cycle before rollover would occur, seems one source says about 49 days on a 32u4, so maybe at the end of each cycle, implement something such as this (a reset method)

https://www.instructables.com/id/two-ways-to-reset-arduino-in-software/.

I imagine it would be best to digitalWrite your outputs LOW before delaying for a day.

If you can live with something weird happening on day 49 or thereabouts, maybe just add

digitalWrite(bluePin,LOW);
digitalWrite(pinkPin,LOW);
digitalWrite(yellowPin,LOW);
digitalWrite(orangePin,LOW);

delay(86399200);

at the end of your loop() function.

and the method used in that link requires that you reset your "reset reset pin" (it'll make sense when you read it) to HIGH at the very beginning of your program. They claim it works.

larryd:
Do you know what an RTC (real time clock) is?

Do a Google search on:
DS3231 ARDUINO

Thanks for the input:) I think this will give me more functionality so I am thinking I will use this. Know if anyone has used this to run the 28BYJ-48 stepper motor? I just want to get some code that I can read through.

You can use the time readings from the RTC to start control functions.

Your stepper can be controlled when the correct RTC time is read.

Lots of code on the web to control this motor.

Search the forum for 28BYJ-48 as there have been many threads on this motor.

Look at the examples that come with the RTC library for using this breakout board.
RTC library:

example:
if(now.hour() == 3 && now.second() == 0)
{
// step your motor
}

larryd:
You can use the time readings from the RTC to start control functions.

Your stepper can be controlled when the correct RTC time is read.

Lots of code on the web to control this motor.

Search the forum for 28BYJ-48 as there have been many threads on this motor.

Look at the examples that come with the RTC library for using this breakout board.
RTC library:
GitHub - adafruit/RTClib: A fork of Jeelab's fantastic RTC library

example:
if(now.hour() == 3 && now.second() == 0)
{
// step your motor
}

Okay, I got the DS3221:) I am hopping I can bring good news this works.