Noob here, help needed.

hello, i got a problem with the code. initially i wear 2 arduino, and currently i want to wear just 1 arduino. how to combine 2 codes into 1 code only? I really need help.

here the code :

//Master

/* Sensor LM 35
Mencetak temperatur ke serial monitor */
#include <Wire.h>
#include <Keypad.h>
#include <LiquidCrystal_I2C.h>

int state = 0; // 0 = otomatis & 1 = manual

//Variable untuk keypad

const int inPin = 0;

const byte ROWS = 4; // jumlah baris = 4
const byte COLS = 3; // jumlah kolom = 3
// Mendefiniskan keypad map
char keys[ROWS][COLS] = {
{‘1’,‘2’,‘3’},
{‘4’,‘5’,‘6’},
{‘7’,‘8’,‘9’},
{’*’,‘0’,’#’}
};
// Menyambungkan keypad ROW0, ROW1, ROW2 dan ROW3 ke pin digital arduino.
byte rowPins[ROWS] = { 0, 1 , 7, 8 };
// Menyambungkan keypad COL0, COL1 and COL2 ke pin digital arduino.
byte colPins[COLS] = { 9, 10, 13 };

//Menciptakan ‘objek keypad’
Keypad kpd = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );

// Inisialisasi library tadi dengan nomor pin
// yang digunakan untuk interface
LiquidCrystal_I2C lcd(0x3F, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);

void setup() {
// Kita set bit rate dari komunikasi serialnya 9600 bps
Serial.begin(9600);

// Banyaknya kolom yang dipakai 16. Dan barisnya
// sebanyak 2. Itu dari sananya.
lcd.begin(16, 2);

// Tuliskan pesannya ke LCD
lcd.print(“Suhunya ini :”);
Wire.begin(); // join i2c bus (address optional for master)
}

void loop() {
int alarm = 0;
char key = kpd.getKey();
//mengeset hasil pembacaan sensor LM 35
int value = analogRead(inPin);

/* mengubah hasil pembacaan sensor LM 35
ke dalam milivolt */
float millivolts = (value / 1024.0) * 5000;

// mengkonversi ke dalam Celcius
float celcius = millivolts / 10;

// Pindahkan kursor ke baris 2, kolom paling kiri
lcd.setCursor(0, 1);

// Tuliskan ke LCD!!
lcd.print(celcius); lcd.print(" C");
if(state == 1){
lcd.print(" MODE:M");
}else if(state == 0){
lcd.print(" MODE:A");
}
int out;
Wire.beginTransmission(4); // transmit to device #4
// int a = 3;
// Wire.write(a);
Serial.print("statek loop = ");
Serial.println(state);
if(key == ‘*’){
state = 0;
Serial.println(state);
}else if(key == ‘#’) {
state = 1;
Serial.println(state);
}else if(key == ‘9’){
alarm = 9;
Serial.println(alarm);
}

if(state == 0){
if (celcius < 30.00) {
out=1;
} else if (celcius >= 30.00 && celcius <= 32.00) {
out=2;
} else if (celcius > 32.00) {
out=3;
}

}else if(state == 1){
if(key == ‘1’){
Serial.println(“satu”);
out=4;
}else if(key == ‘2’){
out=5;
Serial.println(“dua”);
} else if (key == ‘3’) {
out=2;
Serial.print(“tiga”);
}

if(alarm == 9){
Wire.write(alarm);

}else{
Wire.write(out);
}
Wire.endTransmission();
//tunggu 1 detik
delay(1000);
}
}


/Slave
#include <Wire.h>

//define names for the 4 Digital pins On the Arduino 7,8,9,10
//These data pins link to 4 Relay board pins IN1, IN2, IN3, IN4

#define RELAY1 8
#define RELAY2 9

int valPIR = LOW;
int statePIR = LOW; // we start, assuming no motion detected
int inputPIR = 7; // choose the input pin (for PIR sensor)
int lampstate = 0; //kondisi lampu
int fanstate = 0; //kondisi kipas
int pinALARM = 13;
int statemove = 0;
int counter = 0;

void setup()
{
// Initialise the Arduino data pins for OUTPUT
pinMode(RELAY1, OUTPUT);
pinMode(RELAY2, OUTPUT);
pinMode(inputPIR, INPUT);
pinMode(pinALARM, OUTPUT);
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for output
}

void loop()
{
delay(100);
}

// function that executes whenever data is received from master
// this function is registered as an event, see setup()
void receiveEvent(int howMany)
{
int x = Wire.read(); // receive byte as an integer
Serial.print(“X”);
Serial.println(x);
if (x == 1 && lampstate == 0) { //lampu nyalain
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,HIGH);
lampstate = 1;
} else if (x == 2 && lampstate == 1 || x ==2 && fanstate == 1) { //matiin lampu atau kipas
digitalWrite(RELAY1,HIGH);
lampstate = 0;
digitalWrite(RELAY2,HIGH);
fanstate = 0;
} else if (x == 3 && fanstate == 0) { //kipas nyalain
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,LOW);
fanstate = 1;
} else if (x == 4) { //lampu nyalain mode darurat
digitalWrite(RELAY1,LOW);
digitalWrite(RELAY2,HIGH);
lampstate = 1;
} else if (x == 5) { //kipas nyalain mode darurat
digitalWrite(RELAY1,HIGH);
digitalWrite(RELAY2,LOW);
fanstate = 1;
}

valPIR = digitalRead(inputPIR); // read input value
if(valPIR == HIGH && statePIR == LOW){
//kirim sinyal untuk menyalakan alarm
digitalWrite(pinALARM, HIGH);
statemove = 1;
statePIR = HIGH;
}
Serial.print("sensork = ");
Serial.println(x);
if (statemove = 1) {
counter++;
}
if (counter >= 30 || x == 9 ) {
statemove = 0;
counter = 0;
digitalWrite(pinALARM, LOW);
statePIR = LOW;
}
}


help me, please :frowning:

To make it easy for people to help you please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Your code is too long for me to study quickly without copying to a text editor.

Also use the AutoFormat tool to indent your code for easier reading.

This Simple Merge Demo may help.

Before trying to merge the programs go through the two of them carefully to make sure they are not both trying to use the same resource - for examples an I/O pin. If you find conflicts change something in one of the separate programs and make sure it still works with the change.

…R

Hi, What is your code supposed to do? What does to code do?

Thanks.. Tom, :)

Your chances of getting help are increased if the post title contains an indication as to what you want help with.

A lot of people probably dont even read posts with 'help wanted' titles.

TAGS PLEASE