Im working on project that uses a relay module,arduino atmega328 and a 2 motor 12vdc. based on coding i give, the first code is the motor 1 will run based on the input switch. and the second code the motor will run based on the delay time. what im doing is, im going to combine these two coding into one sketch hoping these two coding will run together. but im confused on how to combine.

`int motorPin = 10; // pin that turns on the motor
int blinkPin = 9; // pin that turns on the LED
int watertime = 5; // how long to water in seconds
int waittime = 60; // how long to wait between watering, in minutes

void setup()
  pinMode(motorPin, OUTPUT); 
  pinMode(blinkPin, OUTPUT); // set pin 13 to an output so we can use it to turn on the LED

void loop()
  digitalWrite(motorPin, HIGH); // turn on the motor
  digitalWrite(blinkPin, HIGH); // turn on the LED
  delay(watertime*3000);        // multiply by 1000 to translate seconds to milliseconds

  digitalWrite(motorPin, LOW);  // turn off the motor
  digitalWrite(blinkPin, LOW);  // turn off the LED
  delay(waittime*6000);        // multiply by 60000 to translate minutes to milliseconds

this is the coding to run the motor by using delay time

const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

void loop() {
  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonState == LOW) {
    // turn LED on:
    digitalWrite(ledPin, LOW);
  } else {
    // turn LED off:
    digitalWrite(ledPin, HIGH);

this is the code which the motor will run based on the switch

for now, i dont have idea on how to combine these two coding.

the idea of my project is when switch is LOW(pin 3) the Motor 1(pin 13) will HIGH until the switch is HIGH, the motor 1 will be LOW and stop. At the same time, there will be time delay for 10 second and the Motor 2 (pin 10)will active soon after switch is LOW and delaying time for 10 seconds and the Motor 2 will be active with time for 25 second and stop and the next active for another 60 minutes

  • 2
    You won't have any idea until you learn to understand what those two individual codes are doing on each line. Once you learn that then combining them becomes trivial. So the thing to do is to go learn what all those lines of code mean and do.
    – Delta_G
    Jun 6, 2018 at 2:58
  • If you want someone here to be able to help you then you might want to consider telling what the combined code should do.
    – Delta_G
    Jun 6, 2018 at 2:59
  • @Delta_G okay i just edited what the code should do Jun 6, 2018 at 3:22
  • Can you edit it again and be specific about what you want it to do? How many seconds is "several seconds". Should I write a routine to come up with a random number for that? Or did you have some number in mind? Can you see how frustrating it can be to try to help someone who doesn't even know what they want?
    – Delta_G
    Jun 6, 2018 at 3:31
  • 1
    This question has been asked a number of times. Please see an old answer: arduino.stackexchange.com/questions/17277/… Jun 6, 2018 at 10:05

1 Answer 1


The simplest way of combining two programs is this:

  1. Copy the content of the two programs into a single new .ino (program) file
  2. Rename the first setup function setup_one
  3. Rename the first loop function loop_one
  4. Rename the other setup and loop functions setup_two and loop_two
  5. Add the following code to the bottom of that file.


void setup()
void loop ()

This is a very simplistic approach and it won't work for all cases. It will work in this case, but you will find it will do program one then program two. It will work because your constants don't clash (at the top of the program) you aren't using the same pin in the different programs, etc.

You need to learn what the code does though to really merge the programs, you will see better ways of doing it. The best way to learn is to right down, in your native language, what each line of code does, so in the case of program one:

turn on the motor
turn on the LED
wait for 'watertime' seconds
turn off the motor
turn off the LED
wait for 'waittime' minutes

For program 2:

  read the state of the pushbutton value:
  If the button is in state low
    Turn off the LED -- Not sure  this might be on
    Turn on the LED -- Not sure this might be off

Hopefully you should recognise those descriptions (they are the comments) and when you read through the descriptions you will realise why the code you put together using the above method "doesn't work". It does, you just need to wait for 'waittime' minutes before you get a few microseconds to press the button.

If you want the motor to respond to the button then rearrange the natural language description to do what you want and then replace the English with the code and you should have a program that is close to working. (Hopefully)

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