Using RTC and buttonstate for stopping motor at specific button clicks

Hello community,

i build a feeding machine for my chickens. Now the problem is i wanted to turn
on the motor at a specific time like 9pm, the motor has a button on the motorshaft so when the motor turns it needs to stop at 2 rotations.

I managed to code it that the motor turns on at a specific time but not stop at 2 button clicks.

Thanks in advance, i hope you guys can help me :slight_smile:

Here my code

  Download latest Blynk library here:

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials:
    Sketch generator: 
    Blynk community:  
    Follow us:        

  Blynk library is licensed under MIT license
  This example code is in public domain.

  This example runs directly on ESP8266 chip.

  Note: This requires ESP8266 support package:

  Please be sure to select the right ESP8266 module
  in the Tools -> Board menu!

  Change WiFi ssid, pass, and Blynk auth token to run :)
  Feel free to apply it to any other example. It's simple!

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <RTClib.h>
#include <Wire.h>

RTC_DS1307 rtc;
char buf1[20];

const int OnHour = 15; //SET TIME TO ON RELAY (24 HOUR FORMAT)
const int OnMin = 12;
const int OffHour = 15; //SET TIME TO OFF RELAY
const int OffMin = 12;

const int  buttonPin = 0;    // the pin that the pushbutton is attached to
const int relay = 16;       // the pin that the LED is attached to

// Variables will change:
int buttonPushCounter = 0;   // counter for the number of button presses
int buttonState = 0;         // current state of the button
int lastButtonState = 0; 

#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "lol";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "lol";
char pass[] = "lol";

void setup()
  // Debug console
  Blynk.begin(auth, ssid, pass);
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  pinMode(buttonPin, INPUT);

void loop(){
  DateTime now =;
  sprintf(buf1, "%02d:%02d:%02d %02d/%02d/%02d",  now.hour(), now.minute(), now.second() ,, now.month(), now.year());  
  Serial.print(F("Date/Time: "));

    if (buttonState != lastButtonState) {
    // if the state has changed, increment the counter
    if (buttonState == HIGH) {
      // if the current state is HIGH then the button went from off to on:
      Serial.print("number of button pushes: ");
    } else {
      // if the current state is LOW then the button went from on to off:
    // Delay a little bit to avoid bouncing
  // save the current state as the last state, for next time through the loop
  lastButtonState = buttonState;

  // turns on the LED every four button pushes by checking the modulo of the
  // button push counter. the modulo function gives you the remainder of the
  // division of two numbers:
  if (now.hour() == OnHour && now.minute() == OnMin && buttonPushCounter % 2 == 0) {
    digitalWrite(relay, HIGH);
  } else if(now.hour() == OffHour && now.minute() == OffMin) {
    digitalWrite(relay, LOW);


the motor has a button on the motorshaft

Does that button slip into a buttonhole when the shaft turns?

Or, is there actually a switch on the shaft?

void loop(){



    if (buttonState != lastButtonState) {

You seem to have failed to read the pin that the button is sewn onto. You also failed to explain how the button is sewn on.

Hello PaulS,

so i put a tiny metal sheet on the shaft so when the motor rotates it turns to another plate where it touch each other. and that act like a button.

i do not have so much knowledge about programming but i want to finnish that so fast a possible.