Yes, here you go. This code is not nearly ready, the functions are not finished yet, they need to be completed, verified and simplified. Which I can not do, because I'm locked out.
//Milliseconds in a day.
#define dayInMillis 86400000
//Input pins for the buttons.
const int startFeedPin = 2;
const int openHatchPin = 3;
const int closeHatchPin = 4;
//PWM outputs and other variables for the motors.
const int motor_5_PWM = 5;
const int motor_4_PWM = 6;
const int motor_3_PWM = 9;
const int motor_2_PWM = 10;
const int motor_1_PWM = 11;
const int motorArray[5] = {motor_1_PWM, motor_2_PWM, motor_3_PWM, motor_4_PWM, motor_5_PWM};
int isMoved[5] = {0, 0, 0, 0, 0}; // Indicates if a certain motor has been rotated or not.
//Approximatelly 140ms delay means 90° of rotation.
const int rotate = 100;
//Motor enable.
const int _motorEN = 12;
//Variables for measuring time.
unsigned long currentTime = 0;
unsigned long previousTime = 0;
//Booleans for the feeding and hatch closing / opening algorithm.
bool feeding = false;
bool opening = false;
bool closing = false;
//Functions
void feedingSequence(bool *feed_p, int *isMoved_p, unsigned long *prev_t, unsigned long *curr_t); //If feeding button is pressed, starts the feeding sequence.
void hatchOpening(); //Opens up all the hatches if the button is pressed.
void hatchClosing(); //Closes all the hatches if the button is pressed.
void buttonCheck(bool *feed_p, bool *open_p, bool *close_p, unsigned long *prev_t); // Reading the button signals.
//void test(int *elem);
void setup() {
Serial.begin(9600);
//Pinmode setup.
pinMode(startFeedPin, INPUT);
pinMode(openHatchPin, INPUT);
pinMode(closeHatchPin, INPUT);
pinMode(_motorEN, OUTPUT);
pinMode(motor_5_PWM, OUTPUT);
pinMode(motor_4_PWM, OUTPUT);
pinMode(motor_3_PWM, OUTPUT);
pinMode(motor_2_PWM, OUTPUT);
pinMode(motor_1_PWM, OUTPUT);
pinMode(A5, OUTPUT); // A5: indicator LED. This pin has low active logic.
//Default output setup.
digitalWrite(A5, HIGH); //LED off.
digitalWrite(_motorEN, HIGH); //Motors disabled.
analogWrite(motor_1_PWM, 0); //Motor 1 PWM = 0.
analogWrite(motor_2_PWM, 0); //Motor 2 PWM = 0.
analogWrite(motor_3_PWM, 0); //Motor 3 PWM = 0.
analogWrite(motor_4_PWM, 0); //Motor 4 PWM = 0.
analogWrite(motor_5_PWM, 0); //Motor 5 PWM = 0.
}
void loop() {
//test(&isMoved[0]);
//Serial.println(isMoved[0]);
buttonCheck(&feeding, &opening, &closing, &previousTime); //Checking the buttons.
if (feeding == true){feedingSequence(&feeding, &isMoved[0], &previousTime, ¤tTime);} //Obvious.
//if (opening == true){hatchOpening();} //Obvious.
//if (closing == true){hatchClosing();} //Obvious.
}
//void test(int *elem){*elem += 1; return;}
//Checking if the buttons were pressed.
void buttonCheck(bool *feed_p, bool *open_p, bool *close_p, unsigned long *prev_t){
//Checking if the feeding button was pressed.
int startFeed = digitalRead(startFeedPin);
if(startFeed == 1){*feed_p = true; *open_p = false; *close_p = false; *prev_t = millis(); digitalWrite(A5, LOW);}
//Checking if the open hatch button was pressed.
int openHatch = digitalRead(openHatchPin);
if(openHatch == 1){*feed_p = false; *open_p = true; *close_p = false; *prev_t = 0; digitalWrite(A5, HIGH);}
//Checking if the close hatch button was pressed.
int closeHatch = digitalRead(closeHatchPin);
if(closeHatch == 1){*feed_p = false; *open_p = false; *close_p = true; *prev_t = 0; digitalWrite(A5, HIGH);}
return;
}
//The feeding sequence.
void feedingSequence(bool *feed_p, int *isMoved_p, unsigned long *prev_t, unsigned long *curr_t){
//*curr_t = millis();
unsigned long currt = millis();
unsigned long diff = currt - (*prev_t);
if(diff >= 3000){
digitalWrite(_motorEN, LOW);
analogWrite(motorArray[0], 127);
/*delay(150);
analogWrite(motorArray[0], 0);
digitalWrite(_motorEN, HIGH);
digitalWrite(A5, HIGH);
feed_p = false;/
}
/*//"Checking the current time".
*curr_t = millis();
//Checking if the time spent is >= 1 day.
if ((*curr_t - *prev_t) >= 1000){
for (int i = 0; i <= 4; i++){
// If the current motor hasn't moved yet, move it.
int currentArrayElement = *(isMoved_p + i);
if (currentArrayElement == 0){
digitalWrite(_motorEN, LOW); //Motor enable.
analogWrite(motorArray[i], 127); //Start rotating.
delay(165); // Waiting.
analogWrite(motorArray[i], 0); //Stop rotating.
digitalWrite(_motorEN, HIGH); //Motor disable.
currentArrayElement = 1; //Setting the motor as "moved".
*prev_t = millis(); //Increasing the "previous time" value.
//Checking if we have reached the end of the array. If yes, reset the values.
if(i == 4){
for(int k = 0; k <= 4; k++){int cleanArray = *(isMoved_p + k); cleanArray = 0;} //Restoring array value to default.
digitalWrite(A5, HIGH); //Switch off LED.
*feed_p = false; //Setting feeding boolean
return;
}
return;
}
}
} */
return;
}
//Opening all hatches.
void hatchOpening(){
feeding = false; //If there was feeding, stop the current sequence.
digitalWrite(_motorEN, LOW); //Motor enable.
//Going through the array.
for (int i = 0; i <= 4; i++){
//If a motor hasn't been moved, move it.
if (isMoved[i] == 0){
analogWrite(motorArray[i], 100); //Start rotating.
delay(165); // Waiting.
analogWrite(motorArray[i], 0); //Stop rotating.
isMoved[i] = 1; //Setting the motor as "moved".
}
}
digitalWrite(_motorEN, HIGH); //Motor disable.
opening = false;
return;
}
//Closing all hatches.
void hatchClosing(){
feeding = false; //If there was feeding, stop the current sequence.
digitalWrite(_motorEN, LOW); //Motor enable.
//Going through the array.
for (int i = 0; i <= 4; i++){
//If a motor has been moved, move it again so the hatch will be closed.
if (isMoved[i] == 1){
analogWrite(motorArray[i], 127); //Start rotating.
delay(165); // Waiting.
analogWrite(motorArray[i], 0); //Stop rotating.
isMoved[i] = 0; //Setting the motor as "not moved". Or default state. Or whatevs.
}
}
digitalWrite(_motorEN, HIGH); //Motor disable.
closing = false;
return;
}