I wanna combine sketches but i don't know how to

hi
i don't know how to combine this sktches
i have 4 sketches and combine them into 2 code
(one in the sender arduino and other in the receiver arduino both wired to canbus mcp2515 )
ther first sketches is for controlling the leds by push buttons and their brightness by the potentiometer
and the seconds are for displaying the temperture and humidity on oled from a dht11
sender code
sketch1

#include <SPI.h>
#include <mcp2515.h>   
#define potPin1 A0
#define potPin2 A1
#define CS 10
int ReceivedMessage;
int push1 = 2;
int push2 = 3;
int flag = 0;
int s;
   int potValue1;
   int potValue2;
struct can_frame MyMsg;
MCP2515 mcp2515(CS);
void setup()
{
  while (!Serial);
  Serial.begin(9600);
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
 mcp2515.setNormalMode(); 
 pinMode(push1, INPUT_PULLUP); 
 pinMode(push2, INPUT_PULLUP); 
 MyMsg.can_id = 0x30; 
 MyMsg.can_dlc = 3; 
}
void loop()
{
 if(digitalRead(push1) == 0) 
 {
 while(digitalRead(push1) == 0);
 s = 1;
  potValue1 = analogRead(potPin1);
        Serial.println(potValue1);
  potValue1 = map(potValue1,0,1023,0,255);
 flag = 1;
 }
 if(digitalRead(push2) == 0) 
 {
 while(digitalRead(push2) == 0);
 s = 2;
   potValue2 = analogRead(potPin2);
        Serial.println(potValue2);
  potValue2 = map(potValue2,0,1023,0,255);
 flag = 1;
 }
 if(flag == 1) 
 {
 flag = 0;
 MyMsg.data[0] = s;
 MyMsg.data[1] = potValue1;
 MyMsg.data[2] = potValue2;
 mcp2515.sendMessage(&MyMsg); 
 }
 delay(20);
}

sketch2

#include <SPI.h>
#include <mcp2515.h>
#include "DHT.h"
DHT dht;
#define CS 10
struct can_frame MyMsg;
MCP2515 mcp2515(CS); // SPI CS
int DHTPIN = 4; // DHT11 on pin 2
float temperature, humidity;
void setup()
{
  Serial.begin(9600);
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); // CAN bus config
 mcp2515.setNormalMode(); // Normal mode
 dht.setup(DHTPIN); // Setup DHT11
}
void loop()
{
 humidity = dht.getHumidity(); // GEt humidity
 temperature = dht.getTemperature(); // Get temperature
 MyMsg.can_id = 0x30; // CAN ID
 MyMsg.can_dlc = 2; // 2 byte
 MyMsg.data[0] = (int)temperature; // Temp in data[0]
 MyMsg.data[1] = (int)humidity; // Humidity
 mcp2515.sendMessage(&MyMsg); // Send message
 delay(5000); // Wait 5 seconds
}

receiver code
sketch 1

#include <SPI.h>
#include <mcp2515.h>
#define CS 10
int LED;
int led1 = 5;
int led2 = 3;
struct can_frame MyMsg;
MCP2515 mcp2515(CS);
void setup()
{
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ);
 mcp2515.setNormalMode();
 pinMode(led1, OUTPUT); 
 pinMode(led2, OUTPUT); 
 digitalWrite(led1, 0);
 digitalWrite(led2, 0);
}
void loop()
{
 if(mcp2515.readMessage(&MyMsg) == MCP2515::ERROR_OK)
 {
 LED = MyMsg.data[0];
 if(LED == 1)
 {
        int x1 = MyMsg.data[1];
       Serial.println(x1);
         analogWrite(led1,x1);
 delay(500);
 digitalWrite(led1, 0);
 }
 if(LED == 2)
 {
               int x2 = MyMsg.data[2];
       Serial.println(x2);
         analogWrite(led2,x2);
 delay(500);
 digitalWrite(led2, 0);
 }
 } }

sketch2

#include <SPI.h>
#include <mcp2515.h>
#include <Wire.h>
 #include <Adafruit_GFX.h>  
 #include <Adafruit_SSD1306.h>  
 #define screen_width 128 // OLED display width, in pixels  
 #define screen_height 64 // OLED display height, in pixels  
 #define OLED_RESET 4  
 Adafruit_SSD1306 display(screen_width, screen_height);  
#define CS 10
int temperature, humidity;
struct can_frame MyMsg;
MCP2515 mcp2515(CS);
void setup()
{
    Serial.begin(9600);
 SPI.begin();
 mcp2515.reset();
 mcp2515.setBitrate(CAN_500KBPS, MCP_8MHZ); // Config CAN bus
 mcp2515.setNormalMode(); // Normal mode
   Serial.println("TEMPERATURE AND HUMIDITY");  
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);  
  display.clearDisplay();  
}
void loop()
{
 
 if(mcp2515.readMessage(&MyMsg) == MCP2515::ERROR_OK)
 {
 temperature = MyMsg.data[0]; // Temperature
 humidity = MyMsg.data[1]; // Humidity
   display.clearDisplay();  
   display.setTextSize(1);  
   display.setTextColor(SSD1306_WHITE);  
   display.setCursor(0, 0);  
   display.print("  TEMP. & HUMIDITY");  
   display.setCursor(0, 25);  
   display.print(" TEMPERATURE : ");  
   display.setCursor(90, 25);  
   display.print((int)temperature);  
   display.setCursor(0, 50);  
   display.print(" HUMIDITY  : ");  
   display.setCursor(90, 50);  
   display.print((int)humidity);  
   display.display();  
 }
}

thank you in advance

You need to write a new program that does what you want. It is OK to copy parts of other programs into the new code as long as

  1. those parts do exactly what you want, in the correct order

  2. don't conflict with any other parts of the code, especially variable names and types, and program functionality

1 Like

Be sure there is no pin conflict between codes.

Look at any program. they are all similar. at the top, you include the libraries. then you declare the variables. after that you write the functions. at the bottom, you have void(setup)

divide the code being added as noted above. include the libraries you need to add, insert the new variables you need, changing any that conflict with the existing code. paste in the new functions. add the necessary steps to void(setup)

it is a case of sorting apples from oranges, and sending the components of the incoming sketches to where they belong.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.