How to combine a variety of coding in one Arduino code

I have a project to make a smart home system. I made a wide range of functions, such as doorbell, remote, fan control etc. All functions were tested and feasible. However, I don't know how to combine a variety of coding in one Arduino code?
Could someone/a teacher help me? Thank you very much.

doorbell

#include "Buzzer.h"
#include "Sounds.h"
#include "PH20Port.h"

PH20Port buzzerplay(P9);

#include "Buzzer.h"

int touch_PIN2  = 2;
Buzzer mBuzzer = Buzzer(buzzerplay.pin1());
Buzzer buzzer(buzzerplay.pin1());

void setup() {

pinMode(touch_PIN2,INPUT);
}

void loop()  {

   if (digitalRead(touch_PIN2)==HIGH) {
    mBuzzer.bendTones(1500, 2500, 1.05, 20, 8);
    mBuzzer.bendTones(2499, 1500, 1.05, 25, 8);
  } else {
    buzzer.noTone();
  }

}
#include "EM_TTP229.h"
EM_TTP229 mTTP229;
int SCLPin = 9, SDOPin = 8;
String Read_Key () {
 String key_name = mTTP229.GetKeyMap();
 char * result = (char *)key_name.c_str();
 return result;
}
#include <Servo.h>

String item;
volatile int number;
String password[]={"0", "0", "0"};

Servo servo_12;

void setup(){
  item = "";
  number = 0;
  mTTP229.initTTP229(SCLPin, SDOPin);
  Serial.begin(9600);
  servo_12.attach(12);
}

void loop(){
  item = Read_Key();
  if (item == "1" && number == 0) {
    Serial.println(item);
    password[(int)(0)] = item;
    number = 1;
    delay(100);

  }
  if (item == "2" && number == 1) {
    Serial.println(item);
    password[(int)(1)] = item;
    number = 2;
    delay(100);

  }
  if (item == "3" && number == 2) {
    Serial.println(item);
    password[(int)(2)] = item;
    number = 3;
    delay(100);

  }
  if (password[(int)(0)] == "1" && (password[(int)(1)] == "2" && password[(int)(2)] == "3")) {
    Serial.println("hello");
    servo_12.write(0);
    delay(2000);
    password[(int)(0)] = "0";
    password[(int)(1)] = "0";
    password[(int)(2)] = "0";
    number = 0;

  } else {
    servo_12.write(90);
    delay(20);

  }

}

remote_control

#include "IR_remote.h"
#include "keymap.h"
IRremote ir(4);

#include <Servo.h>

Servo servo_12;
Servo servo_13;

void setup(){
  ir.begin();

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  servo_12.attach(12);
  servo_13.attach(13);
}

void loop(){
  // 按下1键,同时打开风扇和黄灯
  if (ir.getIrKey(ir.getCode(),2) == EM_IR_KEYCODE_1) {
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);

  }
  // 按下2键,同时关闭风扇和黄灯
  if (ir.getIrKey(ir.getCode(),2) == EM_IR_KEYCODE_2) {
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);

  }
  // 按下3键,打开门
  if (ir.getIrKey(ir.getCode(),2) == EM_IR_KEYCODE_4) {
    servo_12.write(90);
    delay(20);

  }
  // 按下4键,关闭门
  if (ir.getIrKey(ir.getCode(),2) == EM_IR_KEYCODE_3) {
    servo_12.write(0);
    delay(20);

  }
  // 按下5键,打开窗
  if (ir.getIrKey(ir.getCode(),2) == EM_IR_KEYCODE_6) {
    servo_13.write(90);
    delay(20);

  }
  // 按下4键,关闭窗
  if (ir.getIrKey(ir.getCode(),2) == EM_IR_KEYCODE_5) {
    servo_13.write(0);
    delay(20);

  }

}

Temperature_and_humidity


#include "SoftI2CMaster.h"
#include "LiquidCrystal_SoftI2C.h"
#include "DHT.h"

LiquidCrystal_SoftI2C mylcd(0x27,16,2,7,A0);
DHT dhtA3(A3, 11);



void setup(){
  Serial.begin(9600);
  dhtA3.begin();
  mylcd.begin(16, 2);

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void loop(){

  float t = dhtA3.readTemperature();
  float h = dhtA3.readHumidity();
  mylcd.setCursor(0, 0);
  mylcd.print("Temp: ");
  mylcd.print(dhtA3.readTemperature());
  mylcd.print("C");
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.println(" *C ");
  mylcd.setCursor(0, 1);
  mylcd.print("Humdity:");
  mylcd.print(dhtA3.readHumidity());
  mylcd.print("%");
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.print(" %\t");
  delay(3000);
    
  if (dhtA3.readTemperature() > 30 || dhtA3.readHumidity() > 70) {
    digitalWrite(5,HIGH);
    digitalWrite(6,LOW);

  } else {
    digitalWrite(5,LOW);
    digitalWrite(6,LOW);

  }
 
  
}

Rain


#include <Wire.h>
#include "TM1650.h"
#include <Servo.h>

TM1650 tm_4display;
volatile int _light;
volatile int yudi;
volatile int item;
Servo servo_13;

void setup(){
  Serial.begin(9600);
  Wire.begin();
  tm_4display.init();
  _light = 0;
  yudi = 0;
  item = 0;
  tm_4display.clear();
  servo_13.attach(13);
}

void loop(){
  
  _light = analogRead(A2);
  // 将连接在A1接口的雨滴传感器的返回值赋予yudi变量
  yudi = analogRead(A1);
  tm_4display.displayString(_light);
  Serial.print(String(" light:") + String(_light));
  Serial.print(",");
  Serial.println(String("yudi:") + String(yudi));
  delay(500);
  // 判断光线模拟值是否大于100,如果是打开窗户;否则,关闭窗户
  if (_light > 100) {
    // 雨滴传感器感应都雨水时,数值会变小,否则数值变大。此处判断雨滴模拟返回值是否小于200,如果是,则关闭窗户;否则打开窗户
    if (yudi < 300) {
      servo_13.write(90);
      delay(300);

    } else {
      servo_13.write(0);
      delay(3000);

    }

  } else {
    servo_13.write(90);
    delay(20);

  }

}

Welcome to the community :slight_smile:

You cannot have more than one void setup() or void loop() in any program.

You will need an input section that monitors all your sensors and when an input arrives that needs code to be run use some decision tree. Perhaps you should use "switch" structure to execute the code required for that input.

1 Like

Combine them one at a time. Take one of your programs and choose a second to add to it. Add all the new include statements from the second. Try and compile it & fix bugs. Add anything new you need to setup. Compile and fix. Same for loop. Don't try & do it all at once.

Before you get started though, you can help make it simpler by moving everything in loop in each program to a new function with a name that describes what it does and call it from loop. Compile fix & test. It makes it easier to combine them.

1 Like

https://en.wikiversity.org/wiki/Arduino_Sketch_Merge

1 Like

Grumpy Mike's tutorial: Here

1 Like

I try my best that the program can run, but some functions are not working.

#include "IR_remote.h"
#include "keymap.h"
IRremote ir(4);

#include "EM_TTP229.h"
EM_TTP229 mTTP229;
int SCLPin = 9, SDOPin = 8;
String Read_Key () {
  String key_name = mTTP229.GetKeyMap();
  char * result = (char *)key_name.c_str();
  return result;
}
#include <Servo.h>
#include <Wire.h>
#include "TM1650.h"
#include "DHT.h"

String item;
volatile int num;
volatile int i;
String password[] = {"0", "0", "0"};

Servo servo_12;
Servo servo_13;
TM1650 tm_4display;
volatile int light;
DHT dhtA0(A0, 11);
DHT dhtA3(A3, 11);

void setMotor8833(int speedpin, int dirpin, int speed)
{
  if (speed == 0)
  {
    digitalWrite(dirpin, LOW);
    analogWrite(speedpin, 0);
  }
  else if (speed > 0)
  {
    digitalWrite(dirpin, LOW);
    analogWrite(speedpin, speed);
  }
  else
  {
    digitalWrite(dirpin, HIGH);
    analogWrite(speedpin, 255 + speed);
  }
}

void yaokong() {
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_3) {
    servo_12.write(0);
    delay(500);

  }
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_4) {
    servo_12.write(90);
    delay(500);

  }
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_5) {
    servo_13.write(90);
    delay(500);

  }
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_6) {
    servo_13.write(0);
    delay(500);

  }
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_7) {
    setMotor8833(5, 6, 100);

  }
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_8) {
    setMotor8833(5, 6, 0);

  }
}

void mimakaimen() {
  item = Read_Key();
  if (item == "1" && num == 0) {
    Serial.println(item);
    password[(int)(0)] = item;
    num = 1;
    delay(100);

  }
  if (item == "2" && num == 1) {
    Serial.println(item);
    password[(int)(1)] = item;
    num = 2;
    delay(100);

  }
  if (item == "3" && num == 2) {
    Serial.println(item);
    password[(int)(2)] = item;
    num = 3;
    delay(100);

  }
  if (password[(int)(0)] == "1" && (password[(int)(1)] == "2" && password[(int)(2)] == "3")) {
    Serial.println("hello");
    servo_12.write(0);
    delay(500);
    delay(2000);
    password[(int)(0)] = "0";
    password[(int)(1)] = "0";
    password[(int)(2)] = "0";
    num = 0;

  } else {
    servo_12.write(90);
    delay(500);

  }
}

void wengan() {
  tm_4display.clear();
  light = analogRead(A2);
  tm_4display.displayString(light);
  if (digitalRead(A2) == 0) {
    servo_13.write(90);
    delay(500);

  } else {
    if (light > 200) {
      servo_13.write(0);
      delay(500);

    } else {
      servo_13.write(90);
      delay(500);

    }

  }
  if (dhtA3.readTemperature() > 35) {
    setMotor8833(5, 6, 100);

  } else {
    setMotor8833(5, 6, 0);

  }
}

void setup() {
  item = "";
  num = 0;
  i = 1;
  ir.begin();

  mTTP229.initTTP229(SCLPin, SDOPin);
  Serial.begin(9600);
  servo_12.attach(12);
  servo_13.attach(13);
  Wire.begin();
  tm_4display.init();
  servo_12.write(90);
  delay(500);
  servo_13.write(0);
  delay(500);
  tm_4display.clear();
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  digitalWrite(5, LOW);
  digitalWrite(6, LOW);
  light = 0;
  pinMode(7, INPUT);
  dhtA0.begin();
  dhtA3.begin();
}

void loop() {
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_1) {
    i = 1;
    tm_4display.displayString(i);

  }
  if (ir.getIrKey(ir.getCode(), 2) == EM_IR_KEYCODE_2) {
    i = 0;

  }
  if (i == 0) {
    mimakaimen();
    wengan();

  } else {
    yaokong();

  }

}

You could tell us which functions and you could tell us what "not working" means. As is, you have not included any information that we can use to help you.

Sorry about that. I am new player for Arduino.


Only the digital clock can display the luminescence. Other function like remote, window servo, door access control are not feasible.

Did you follow the suggestion to incorporate 1 thing at a time? That is add one thing and get the code to work before adding another part. Then if something does not work you know that the one thing that was changed is the cause.

If you try to do it all at once, I can see that you would have trouble.

2 Likes

time to find the bug = number of lines of code added UNtested * 10 minutes.

So trying to be fast turns out to be real slow.
best regards Stefan

1 Like

Check out my tutorial on Multi-tasking in Arduino and turn each of your loops into a task that runs with out delays
See How to write Timers and Delays in Arduino for how to get rid of the delays.
multitaskingDiagramSmall

1 Like
#include "EM_TTP229.h"
EM_TTP229 mTTP229;
int SCLPin = 9, SDOPin = 8;
String Read_Key () {
  String key_name = mTTP229.GetKeyMap();
  char * result = (char *)key_name.c_str();
  return result;
}
#include <Servo.h>

String item;
volatile int number;
String password[] = {"0", "0", "0"};

Servo servo_12;


#include "Buzzer.h"
#include "Sounds.h"
#include "PH20Port.h"

PH20Port buzzerplay(P9);

#include "Buzzer.h"

int touch_PIN2  = 2;
Buzzer mBuzzer = Buzzer(buzzerplay.pin1());
Buzzer buzzer(buzzerplay.pin1());


#include <Wire.h>
#include "TM1650.h"

TM1650 tm_4display;
volatile int _light;
volatile int yudi;
Servo servo_13;

#include "SoftI2CMaster.h"
#include "LiquidCrystal_SoftI2C.h"
#include "DHT.h"

LiquidCrystal_SoftI2C mylcd(0x27, 16, 2, 7, A0);
DHT dhtA3(A3, 11);

void setup() {
  Serial.begin(9600);
  item = "";
  number = 0;
  mTTP229.initTTP229(SCLPin, SDOPin);

  servo_12.attach(12);

  pinMode(touch_PIN2, INPUT);

  Wire.begin();
  tm_4display.init();
  _light = 0;
  yudi = 0;
  tm_4display.clear();
  servo_13.attach(13);

  dhtA3.begin();
  mylcd.begin(16, 2);

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void door() {
  item = Read_Key();
  if (item == "1" && number == 0) {
    Serial.println(item);
    password[(int)(0)] = item;
    number = 1;
    delay(100);

  }
  if (item == "2" && number == 1) {
    Serial.println(item);
    password[(int)(1)] = item;
    number = 2;
    delay(100);

  }
  if (item == "3" && number == 2) {
    Serial.println(item);
    password[(int)(2)] = item;
    number = 3;
    delay(100);

  }
  if (password[(int)(0)] == "1" && (password[(int)(1)] == "2" && password[(int)(2)] == "3")) {
    Serial.println("hello");
    servo_12.write(0);
    delay(2000);
    password[(int)(0)] = "0";
    password[(int)(1)] = "0";
    password[(int)(2)] = "0";
    number = 0;

  } else {
    servo_12.write(90);
    delay(1000);

  }

}

void doorbell() {
  if (digitalRead(touch_PIN2) == HIGH) {
    mBuzzer.bendTones(1500, 2500, 1.05, 20, 8);
    mBuzzer.bendTones(2499, 1500, 1.05, 25, 8);
  } else {
    buzzer.noTone();
  }
}

void window() {
  // 将连接在A2接口的光敏传感器的模拟值赋给light变量
  _light = analogRead(A2);
  // 将连接在A1接口的雨滴传感器的返回值赋予yudi变量
  yudi = analogRead(A1);
  tm_4display.displayString(_light);
  Serial.print(String(" light:") + String(_light));
  Serial.print(",");
  Serial.println(String("yudi:") + String(yudi));
  delay(2000);
  // 判断光线模拟值是否大于100,如果是打开窗户;否则,关闭窗户
  if (_light > 100) {
    // 雨滴传感器感应都雨水时,数值会变小,否则数值变大。此处判断雨滴模拟返回值是否小于200,如果是,则关闭窗户;否则打开窗户
    if (yudi < 600) {
      servo_13.write(90);
      delay(500);

    } else {
      servo_13.write(0);
      delay(3000);

    }

  } else {
    servo_13.write(90);
    delay(20);

  }
}

void TandH() {
  float t = dhtA3.readTemperature();
  float h = dhtA3.readHumidity();
  mylcd.setCursor(0, 0);
  mylcd.print("Temp: ");
  mylcd.print(dhtA3.readTemperature());
  mylcd.print("C");
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.println(" *C ");
  mylcd.setCursor(0, 1);
  mylcd.print("Humdity:");
  mylcd.print(dhtA3.readHumidity());
  mylcd.print("%");
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.print(" %\t");
  delay(3000);

  if (dhtA3.readTemperature() > 35 || dhtA3.readHumidity() > 75) {
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);

  } else {
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);

  }
}

void loop() {
  door();
  TandH();
  doorbell();
  window();
}

Thank you! Above functions are feasible, but need to push a long time (5 second/button) ,such as door access control, then the door will open.
Could you teach me how can i delay each functions?

You are working on an informatic project. And what is most needed in an informatic project is information. Sufficient information.

The description above is too less to give an answer.
Please descripe in normal words the functionality you want to have.

something like that:

I type the code on the keypad.
If code is correct move servo very slowly to the opposite position (delayed movement of the servo)

I'm pretty sure that this is not what you want. But what is it that you want?
Give a detailed description of what a bystanding person can watch what is happening.

by the way: did you start to add one single functionality to one sketch?

I can tell you from my 38 years programming-experience
The Time to find one bug rises exponentially with the number of lines of code you add without testing them.

If you combine four sketches at once the time to find bugs will be 16 times longer than
adding only one sketch at a time and then test.

You might reach very quickly to have all lines of code into one sketch. But it will take you a lot of hours to make the code work.
That is the reason why everybody here recommends add only one function and then immidiately test.

If your favorite joy of programming is heavily prolooonged debugging you are on the right track.
best regards Stefan

Thank you for your kind recommendation. I was tested every function and i want to combine them to one coding because of final project.
There are Automation system:

  1. buzzer sensor (doorbell)
  2. temperature and humidity sensor (control fan on/off)
  3. photosensitive sensor and water droplets sensor (control window.servo)

About the Manual system:

  1. door access control (push password button to open door)
  2. Remote (control door, window and fan on/off), i think that it is difficult part.

it seems that the most difficult part for you is to provide a

D E T A I L E D

description of the functionality that you want

you have written something about

what shall be delayed? You have to describe

T H A T

in detail

For the door access control, when i push the password button, i need to hold each button around 7 seconds. I think that the void loop() is affected.
When i use the remote, other automation function may be affected.

I want to separate action automation system and manual system, and reduce TX buffer...

void loop() {
  door();
  TandH();
  doorbell();
  window();

How to use bufferedOut into loop?

post the code that shows this behaviour. Without the code I can only speculate:

  • reduce the delays from 7000 to 50
  • remove the while-loops that waits until xy

does this help you? NO!
The only thing that really helps is posting an actual code-version.
You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

writing such a sentence clearly shows that your ability to understand and write english is very limited.
So I recommend you type all your descriptions in your native language and then let do google-translate the translation. The grammar will not be brilliant but it will be 10 times more effective
that such short and poor words like


#include "EM_TTP229.h"
EM_TTP229 mTTP229;
int SCLPin = 9, SDOPin = 8;
String Read_Key () {
  String key_name = mTTP229.GetKeyMap();
  char * result = (char *)key_name.c_str();
  return result;
}
#include <Servo.h>

String item;
volatile int number;
String password[] = {"0", "0", "0"};

Servo servo_12;


#include "Buzzer.h"
#include "Sounds.h"
#include "PH20Port.h"

PH20Port buzzerplay(P9);

#include "Buzzer.h"

int touch_PIN2  = 2;
Buzzer mBuzzer = Buzzer(buzzerplay.pin1());
Buzzer buzzer(buzzerplay.pin1());


#include <Wire.h>
#include "TM1650.h"

TM1650 tm_4display;
volatile int _light;
volatile int yudi;
Servo servo_13;

#include "SoftI2CMaster.h"
#include "LiquidCrystal_SoftI2C.h"
#include "DHT.h"

LiquidCrystal_SoftI2C mylcd(0x27, 16, 2, 7, A0);
DHT dhtA3(A3, 11);

void setup() {
  Serial.begin(9600);
  item = "";
  number = 0;
  mTTP229.initTTP229(SCLPin, SDOPin);

  servo_12.attach(12);

  pinMode(touch_PIN2, INPUT);

  Wire.begin();
  tm_4display.init();
  _light = 0;
  yudi = 0;
  tm_4display.clear();
  servo_13.attach(13);

  dhtA3.begin();
  mylcd.begin(16, 2);

  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
}

void door() {
  item = Read_Key();
  if (item == "1" && number == 0) {
    Serial.println(item);
    password[(int)(0)] = item;
    number = 1;
    delay(100);

  }
  if (item == "2" && number == 1) {
    Serial.println(item);
    password[(int)(1)] = item;
    number = 2;
    delay(100);

  }
  if (item == "3" && number == 2) {
    Serial.println(item);
    password[(int)(2)] = item;
    number = 3;
    delay(100);

  }
  if (password[(int)(0)] == "1" && (password[(int)(1)] == "2" && password[(int)(2)] == "3")) {
    Serial.println("hello");
    servo_12.write(0);
    delay(2000);
    password[(int)(0)] = "0";
    password[(int)(1)] = "0";
    password[(int)(2)] = "0";
    number = 0;

  } else {
    servo_12.write(90);
    delay(1000);

  }

}

void doorbell() {
  if (digitalRead(touch_PIN2) == HIGH) {
    mBuzzer.bendTones(1500, 2500, 1.05, 20, 8);
    mBuzzer.bendTones(2499, 1500, 1.05, 25, 8);
  } else {
    buzzer.noTone();
  }
}

void window() {
  // 将连接在A2接口的光敏传感器的模拟值赋给light变量
  _light = analogRead(A2);
  // 将连接在A1接口的雨滴传感器的返回值赋予yudi变量
  yudi = analogRead(A1);
  tm_4display.displayString(_light);
  delay(2000);
  // 判断光线模拟值是否大于100,如果是打开窗户;否则,关闭窗户
  if (_light > 100) {
    // 雨滴传感器感应都雨水时,数值会变小,否则数值变大。此处判断雨滴模拟返回值是否小于200,如果是,则关闭窗户;否则打开窗户
    if (yudi < 600) {
      servo_13.write(90);
      delay(500);

    } else {
      servo_13.write(0);
      delay(3000);

    }

  } else {
    servo_13.write(90);
    delay(20);

  }
}

void TandH() {
  float t = dhtA3.readTemperature();
  float h = dhtA3.readHumidity();
  mylcd.setCursor(0, 0);
  mylcd.print("Temp: ");
  mylcd.print(dhtA3.readTemperature());
  mylcd.print("C");
  Serial.print("Temp: ");
  Serial.print(t);
  Serial.println(" *C ");
  mylcd.setCursor(0, 1);
  mylcd.print("Humdity:");
  mylcd.print(dhtA3.readHumidity());
  mylcd.print("%");
  Serial.print("Hum: ");
  Serial.print(h);
  Serial.print(" %\t");
  delay(3000);

  if (dhtA3.readTemperature() > 35 || dhtA3.readHumidity() > 75) {
    digitalWrite(5, HIGH);
    digitalWrite(6, LOW);

  } else {
    digitalWrite(5, LOW);
    digitalWrite(6, LOW);

  }
}

void loop() {
  door();
  TandH();
  doorbell();
  window();
}