i have a two scetch working the first one is set time with rtc matrix keypad to controll servo motor and the second one is HC-05 bluetooth module to controll servo using android app i want to combine those scetch can somebody help me i post my codes below

my first code was for setting time (components : lcd display , matrixkeypad 4*4, RTC realtime module, servo motor)

#include <DS3231.h>
#include <Servo.h>
#include <LiquidCrystal.h>
#include <Keypad.h>
#include <SoftwareSerial.h>
const byte ROWS = 4; // Four rows
const byte COLS = 4; // Three 

// Define the Keymap

char keys[ROWS][COLS] = {






// Connect keypad ROW0, ROW1, ROW2 and ROW3 to these Arduino pins.

byte rowPins[ROWS] = { 0, 2, 3, 4 };

// Connect keypad COL0, COL1 and COL2 to these Arduino pins.

byte colPins[COLS] = { 5, 6, 7 };

//  Create the Keypad
  Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

DS3231  rtc(A4, A5);
Servo servo_test;      //initialize a servo object for the connected servo  
LiquidCrystal lcd(A0, A1, A2, 8, 12, 13); // Creates an LC object. Parameters: (rs, enable, d4, d5, d6, d7)

 //int angle = 0;    

// int potentio = A0;      // initialize the A0analog pin for potentiometer
 int t1, t2, t3, t4, t5, t6;

boolean feed = true; // condition for alarm

 char key;
 int r[6];

 void setup() 
  servo_test.attach(9);   // attach the signal pin of servo to pin9 of arduino
  pinMode(A0, OUTPUT);
  pinMode(A1, OUTPUT);
  pinMode(A2, OUTPUT);


 void loop() 

int buttonPress;
buttonPress = digitalRead(A3);

if (buttonPress==1)


 lcd.print("Time:  ");
 String t = "";
 t = rtc.getTimeStr(); 
 t1 = t.charAt(0)-48;
 t2 = t.charAt(1)-48;
 t3 = t.charAt(3)-48;
 t4 = t.charAt(4)-48;
 t5 = t.charAt(6)-48;
 t6 = t.charAt(7)-48;

 lcd.print("Date: ");

 if (t1==r[0] && t2==r[1] && t3==r[2] && t4==r[3]&& t5<1 && t6<3 && feed==true)

  servo_test.write(220);                   //command to rotate the servo to the specified angle 


void setFeedingTime()
  feed = true;
   int i=0;

  lcd.print("Set feeding Time");

    key = kpd.getKey();

    char j;




    r[i] = key-48;

    if (j==2)
      lcd.print(":"); j++;

  if (key == '#')
  {key=0; break; }

and my second code for bluetooth module (HC-05 and android app)

 #include <SoftwareSerial.h> // TX RX software library for bluetooth
 #include <Servo.h> // servo library 
Servo myservo; // servo name
int bluetoothTx = 10; // bluetooth tx to 10 pin
int bluetoothRx = 11; // bluetooth rx to 11 pin
SoftwareSerial bluetooth(bluetoothTx, bluetoothRx);
void setup()
  myservo.attach(9); // attach servo signal wire to pin 9
  //Setup usb serial connection to computer
//Setup Bluetooth serial connection to android
void loop()
  //Read from bluetooth and write to usb serial
  if(bluetooth.available()> 0 ) // receive number from bluetooth
    int servopos = bluetooth.read(); // save the received number to servopos
    Serial.println(servopos); // serial print servopos current number received from bluetooth
    myservo.write(servopos); // roate the servo the angle received from the android app

i want to combine them can somebody help me to combine them ? please i beg you

  • What is your specific problem in combining the codes? Generally you should avoid using delay(), since it is busy waiting. Use a non-blocking coding style like in the BlinkWithoutDelay example – chrisl Jul 24 '19 at 18:46
  • i just want to combine them i dont know how to start and to combine them :( – Trixie Sandobal Jul 24 '19 at 18:55
  • Start by eliminating all long delay() calls, like I suggested above. Then add all functions from one sketch into the other sketch. Then put the copy the code from void loop() and void setup() from one sketch into the corresponding functions of the other sketch. Then look for redundant code, that was in both sketches, and remove it. Then follow the code line by line, as if you were the one to execute it, and check, how the logic will work. If this is not to your liking, change the code to do it correctly. – chrisl Jul 24 '19 at 19:01
  • Please also look at this answer, where Duncan already explained the basics of combining sketches. – chrisl Jul 24 '19 at 19:03
  • I combined these in one of your apparently multiple questions in the Arduino forum. Why don't you look at your posts there? – CrossRoads Jul 25 '19 at 13:11

Browse other questions tagged or ask your own question.