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:
    https://github.com/blynkkk/blynk-library/releases/latest

  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: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  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:
    https://github.com/esp8266/Arduino

  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
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  rtc.begin();
  pinMode(relay, OUTPUT);
  digitalWrite(relay, HIGH);
  pinMode(buttonPin, INPUT);
}

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

    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:
      buttonPushCounter++;
      Serial.println("on");
      Serial.print("number of button pushes: ");
      Serial.println(buttonPushCounter);
    } else {
      // if the current state is LOW then the button went from on to off:
      Serial.println("off");
    }
    // Delay a little bit to avoid bouncing
    delay(50);
  }
  // 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(){

  delay(1000);

Why?

    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.

thanks