0

Good day, I am new to Arduino, but have made some nice projects work. I have a n issue with a Auto fish feeder. I have used a servo that I modified to run 360. I am having a issue when the Nano board starts. It runs the time I have set between feeds, and then it stops. If I set it to feed every 10 sec, it starts up and run for 10sec and then the LED flashes and it stops. From there it works perfect. But if I lose power and it comes back, its going to feed for 10sec and that will kill the fish.

Here is my sketch I am using now, please can someone look at it and tell me where I am going wrong. I just want it not to run when it starts up, or just for the to that it feeds, not continues like now for 10sec, or 12 hours !!!

//--Setting Vars


   //Fish Feeder Vars.
int pinOutMotor = 3; 
int pinOutLed = 5;
int pinInButton = 2;
int MotorRunDuration = 1000;
float AutoWaitDurationHours = 0.;
//--Program Vars
unsigned long currentMsSinceStart = 0;
unsigned long msValueAtLastRun = 0;
float HoursSinceLastRun = 0;
int buttonValue = 0;

void setup() {
  Serial.begin(9600);             //Turns on serial monitor.
  pinMode(pinOutMotor, OUTPUT);   //Is controlled by a 3906 npn transistor to switch the motor off and on.  When program started I need to add a line to turn off motor when program was started.
  pinMode(pinInButton, INPUT);    //Manual feed.
  pinMode(pinOutLed, OUTPUT);     //Shows motor is on when lite.
}

void loop() {
  //HoursSinceLastRun = AutoWaitDurationHours - 0.05; //this will make it run 3 minutes after turned on.

//Fish Feeder
  //get miliseconds since program start and calc.
  currentMsSinceStart = millis();
  HoursSinceLastRun = (currentMsSinceStart - msValueAtLastRun) / 10000; //10000 millis= 10 seconds.  To change the time to vary feeding times set for 12hrs type in 43200000 or 6hrs tyoe in 21600000.
  //check for button press
  buttonValue = digitalRead(pinInButton);
  if(buttonValue == HIGH){  //had to change state because of button from LOW to HIGH.
    //run motor for button press.
    RunMotor();
    Serial.println("Feed Fish Manually");  //serial print funtion completed by button

  } else if(HoursSinceLastRun > AutoWaitDurationHours){
    RunMotor();
    msValueAtLastRun = currentMsSinceStart;
    Serial.println("Feed Fish Auto");      //serial print funtion completed by auto wait duration hours
  }
  //slow the loop down so it isn't running at full CPU Utilization for nothing.
  delay(50); //delay 1/20th of a second.
}

void RunMotor(){
    analogWrite(pinOutMotor, 0);   // turn on motor
    analogWrite(pinOutLed, 80);        // turn on green led to show motor is on.
    delay(MotorRunDuration);   // wait for motor run duration to expire
    analogWrite(pinOutMotor, 255);    // turn off motor
    analogWrite(pinOutLed, 0);        // turn off green led to show motor is off.
}
  • I do not know what's wrong, as I copy and past from my sketch and mine is compiling 100% – Loeikie Nov 4 '17 at 15:37
  • I am using Arduino 1.8.5, if that helps – Loeikie Nov 4 '17 at 15:40
1

If I correctly understand, your problem is really with the restart, that will always feed the fish.

You can use the EEPROM to save the last time you feed the fish. You also need a RTC to get the real time.

Every time you feed the fish, you also write in the EEPROM the current hour:minutes (from the RTC). When you restart, you read your last feed from the EEPROM and uses it to initialize msValueAtLastRun. You have to change your time recording to hh:mm instead of millis from start.

You code looks OK, only missing debouncing the button.

You can also use a NodeMCU, and get your time via NTP.

  • Thanks for the answer, I will have a look to get a RTC. I will also have to investigate how to get it working and how to right to the EEPROM.I have never done this. But it sounds like a great project. Is there no other way to let it just do a "run" at start up and then have the delay between feeds. So it do not run 10 sec before it goes to the first feed. So feed immediately and then delay – Loeikie Nov 4 '17 at 16:44
  • Is there no other way to let it just do a "run" at start up and then have the delay between feeds. So it do not run 10 sec before it goes to the first feed. So feed immediately and then delay – Loeikie Nov 5 '17 at 12:55
  • @Loeikie Every time Arduino reset, it start from zero. No memory of what it was doing before. No way to know how long has been down. – user31481 Nov 5 '17 at 14:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.