Problems Controlling DC Motor with Pushbutton

My project is a Marine Aquarium Dosing Pump.
I have put together the code below to control two DC motors using an Arduino UNO, a DF Robot motor shield a 16x2 LCD an RCT1307 using I2C and two 6 volt dosing pumps.
The code works OK for now and I can control the motors by setting an appropriate time, duration and speed.
What I would like to do is incorporate a push button for each motor that turns the motor on/off in between times so that if necessary I can manually dose my tank.
I have struggled with this problem for a while and am totally flummoxed. Can some out there please assist.

// Arduino Doser using DFROBOT Motor Shield, 16x2 LCD with I2C and RTC1307

#include <Wire.h>
#include "RTClib.h"
#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,16,2);
 
RTC_DS1307 RTC;
 //Arduino PWM Speed Control?
int E1 = 6;   
int M1 = 7;
int E2 = 5;                         
int M2 = 4; 
 
void setup () {
    Wire.begin();
    RTC.begin();
    lcd.init(); // initialize the lcd
    lcd.backlight();
//RTC.adjust(DateTime(__DATE__, __TIME__)); //Include this line to set the Clock
  }
 
void loop () {
  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
    DateTime now = RTC.now();
lcd.clear();

lcd.setCursor(0, 0);
        lcd.print("Time ");
    lcd.print(now.hour(), DEC);
    lcd.print(':');
    lcd.print(now.minute(), DEC);
    lcd.print(':');
    lcd.print(now.second(), DEC);
    lcd.setCursor(0, 1);
        lcd.print("Date ");
    lcd.print(now.day(), DEC);
    lcd.print('/');
    lcd.print(now.month(), DEC);
    lcd.print('/');
    lcd.print(now.year(), DEC);
    delay(100);
 
//Motor loop
 
if(now.hour() == 15 && now.minute() == 39 && now.second()==00){ // Set the time you want the motor1 to kick in 
    lcd.clear();
    lcd.setCursor(0, 0);
        lcd.print("Motor 1 Running");
    digitalWrite(M1,HIGH);  
    analogWrite(E1, 175); //speed of motor
        delay(15000); //duration for motor to run 15000 = 15 secs
    analogWrite(E1, 0);
    digitalWrite(M1,LOW);
    lcd.clear();
    lcd.print("Motor 1 Stopped");
        delay(1000);}
    lcd.clear();  
    
if(now.hour() == 15 && now.minute() == 39 && now.second()==30){ // Set the time you want the motor2 to kick in
    lcd.setCursor(0, 1);
    lcd.print("Motor 2 Running");
    digitalWrite(M2,HIGH);  
    analogWrite(E2, 175); 
        delay(15000);
    analogWrite(E2, 0);
    digitalWrite(M2,LOW);
    lcd.clear(); 
    lcd.setCursor(0, 1);
    lcd.print("Motor 2 Stopped");
    delay(1000);
    lcd.clear(); 
} 
}

For the diehard programmers out there you may see many errors with my coding as I have basically pulled different routines from various places and combined them, so whilst it does what I intended it may not be written in the most effective way.
I am very new to this hobby.
Cheers
Gastel

I have struggled with this problem for a while and am totally flummoxed.

The first step is to admit that you have a problem. You've done that.
The second step is to admit to what the problem is. You haven't said what the problem is.

How is the switch wired?

  byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;

Why? You never use these.

The use of delay() is stupid. You know, to the second, what time it is. Turn the pump of at the right time. Turn the pump off at the first time.

You never read the state of a pin that a switch might be attached to, so you can't possibly expect to do anything with respect to the state of anything.

Dude (PaulS),
did you read the bit where I said that I had probably made numerous errors in my coding because I was new to this hobby??
You have come on extremely strong for no real reason and I resent it.
I have asked for some help not criticism.

To answer your question about how the switch is wired.
I DON'T HAVE A SWITCH.
THAT is my problem. I am unable to work out how to properly wire and encode the necessary script into my sketch to enable me to manually turn on and off the motors. I was looking to have a simple push on release off but am unable to work out how.

If you are able to assist then I would be grateful, if not then I would appreciate you keeping your aggressive attitude to yourself.
As this is my first post on this forum I sincerely hope that other members deal with requests for help from those with less knowledge a little more constructively.

Cheers
Gastel

You have come on extremely strong for no real reason and I resent it.

Fine with me.

I DON'T HAVE A SWITCH.

Then you can't possibly do what you want. Get over it and move on.

Now, if you HAD a switch, but didn't know how to connect it to the Arduino, that would be a different story.

I can see by reading some of your recent posts that you very seldom actually help anyone you just seem to pick shit out of what they do.

I am sure you fully understand what it is I am asking.
If not perhaps this will assist you in understanding my dilemma.
I have, I believe all of the necessary equipment to produce what I want. What I don't have is the knowledge to enable me to write the code that, when a switch is wired up and pressed will turn on one of the motors. Do you have any practical suggestions on;
A. how to wire a switch properly, and
B. what would be the right code to read when the switch is depressed to start the motor..

A. how to wire a switch properly

Connect one leg to ground. Connect the other leg to a digital pin (7, for example). Then, set the mode of the pin to INPUT, and turn on the internal pullup resistor:

byte switchPin = 7;

void setup()
{
   pinMode(switchPin, INPUT_PULLUP);
}

what would be the right code to read when the switch is depressed

if(digitalRead(switchPin) == LOW)
   // the switch is currently pressed

Now, notice that there is a huge difference between the switch now being pressed and the switch just having become pressed. Depending on what you want to do, the difference may be critical.

Start by looking at the blink without delay example in the IDE.
You'll need to fully understand the fundamental concept it embodies, because you'll need to rewrite your sketch using that concept.

Thanks guys,
I am starting to get the picture.
Once the switch is depressed, what code would would I use to start say motor 1 ?
Would it be something like

if(digitalRead(switchPin) == LOW){
digitalWrite(M1,HIGH);  
    analogWrite(E1, 175);
}

I am only guessing here so don't be too hard please.

I am only guessing here

Why? You have the hardware, don't you? Try that, and see what happens.

Jesus, man you are hard work.

I am not in a position to wire it up and test it right at this point in time. Tomorrow I will give it a try but for now could you throw me a bone and perhaps let me know if what I have suggested looks like it may be feasible so I can go to bed in a bit thinking that at least I am on the right track ??

You're on a track, but whether or not it is the track, you will find out when you try it.

OK so I have wired in a push button as suggested by PaulS and have inserted the following:

if(digitalRead(switchPin) == LOW)
   //the switch is currently pressed
    digitalWrite(M1,HIGH);  
   analogWrite(E1, 175);

This gets the motor running as I wished. Thanks
I now need to get the motor to stop.
Should I use another IF statement to see if switchPin is HIGH or would I use and else statement?
Any help ??

Gastel

if(digitalRead(switchPin) == LOW)
   //the switch is currently pressed
    digitalWrite(M1,HIGH);  
   analogWrite(E1, 175);

Your indentation suggests to me that you think the “analogWrite” should be part of the conditional, but the compiler thinks otherwise.
Either sort out the indentation, or use braces

if(digitalRead(switchPin) == LOW) {
   //the switch is currently pressed
    digitalWrite(M1,HIGH);  
   analogWrite(E1, 175);
}

Should I use another IF statement to see if switchPin is HIGH or would I use and else statement?

You are asking us what your requirements are? OK. I need for the application to stop the motor when the rotary encoder has been rotated 32 times.

I am asking how i stop the motor after I have started it by pushing the button.
Preferably by pushing the button again or if necessary pushing another button.
I already have the motor turning on and of at specific times. I want this as a manual override.

Let me paraphrase my earlier reply:
Either get used to pressing the switch for a long time, or completely restructure your code, based on the principles in the blink without delay example.

A simple way to do it would be to copy the pattern you used in the scheduled dosing and use delay. You'll need to pick a suitable number of milliseconds and it may mean that you need to press the button several times to get the dose you need. Also, you'll need to wait until dosing is done before the button will be responsive again.

All of which means that, as already suggested, the blink without delay method would serve you better, but delay will get your system working without the need for a complete overhaul of your code.

Thank you wildbill and AWOL.
The manual override is not something that I must have in order to use the doser but it would help when I need to load up the plastic tubing from the liquid bottles I am dosing with.
At the moment I am using a push on push off button and am successfully turning on a motor with a single push using the methods suggested. What I can't seem to accomplish is turning off the motor once it is running.
I will try it with a delay.
Conversely is there another simple way or as you suggest wildbill would it require a complete overhaul?

Thanks for you assistance it is appreciated.

What I can't seem to accomplish is turning off the motor once it is running.
I will try it with a delay.

It is precisely because you have a delay that you can't turn it off.
During delay() you're not looking at the switch.

What I can't seem to accomplish is turning off the motor once it is running.

You could use an else statement. If the switch is pressed, turn the motor on. If not, turn it off.

Of course, in the else block you'll need to determine if the motor was turned on for a manual dose or an automatic dose, and only turn if off if it was turned on manually.