Arduino Combining Sketches

I would like to combine below sketches in one:

  1. Sketch

#include <Arduino.h>
#include <Wire.h>
#include <EEPROM.h>
#include <WiFi.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_ADS1X15.h>
#include <DFRobot_ESP_EC.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

// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);

#define SENSOR_PIN 17 // ESP32 pin GIOP17 connected to DS18B20 sensor's DATA pin

OneWire oneWire(SENSOR_PIN);
DallasTemperature DS18B20(&oneWire);

DFRobot_ESP_EC ec;
Adafruit_ADS1115 ads;

float voltage, ecValue, temperature = 25;

String apiKey = "Api"; // Enter your Write API key from ThingSpeak

const char *ssid = "Network"; // wifi ssid and wpa2 key
const char *pass = "Networkpassword";
const char *server = "api.thingspeak.com";

WiFiClient client;

void setup() {
Serial.begin(115200);
EEPROM.begin(32); //needed EEPROM.begin to store calibration k in eeprom
ec.begin();
DS18B20.begin();
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3D for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;:wink:
;
}
delay(2000);
display.clearDisplay();

Serial.println("Connecting to ");
Serial.println(ssid);

WiFi.begin(ssid, pass);

while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
}

void loop() {
voltage = analogRead(A0); // A0 is the gpio 36
DS18B20.requestTemperatures();
temperature = DS18B20.getTempCByIndex(0); // read your temperature sensor to execute temperature compensation
ecValue = ec.readEC(voltage, temperature); // convert voltage to EC with temperature compensation

Serial.print("Temperature:");
Serial.print(temperature, 2);
Serial.println("ºC");

Serial.print("EC:");
Serial.println(ecValue, 2);

display.setTextSize(2);
display.setTextColor(WHITE);

display.setCursor(0, 10);
display.print("T:");
display.print(temperature, 2);
display.drawCircle(85, 10, 2, WHITE); // put degree symbol ( ° )
display.setCursor(90, 10);
display.print("C");

display.setCursor(0, 40);
display.print("EC:");
display.print(ecValue, 2);
display.display();
delay(1500);
display.clearDisplay();

ec.calibration(voltage, temperature); // calibration process by Serail CMD

if (client.connect(server, 80)) // "184.106.153.149" or api.thingspeak.com
{

String postStr = apiKey;
postStr += "&field1=";
postStr += String(temperature, 2);
postStr += "&field2=";
postStr += String(ecValue, 2);
postStr += "\r\n\r\n";
delay(500);

client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
delay(500);

}
client.stop();
}

  1. Sketch

const int RED_PIN = 9; //the digital pin for red pin
const int GREEN_PIN = 10; //the digital pin for green pin
const int BLUE_PIN = 11; //the digital pin for blue pin
const int temperaturePin = 0; //the analog read pin that is used to reat the temperature

void setup() {

pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
Serial.begin(9600); //the bound rate for serial monitor
}

void loop() {

float voltage, degreesC, degreesF; //get numbers with decimals

voltage = getVoltage(temperaturePin); //get voltage from the temperature pin
degreesC = (voltage - 0.5) * 100.0; //calculate the celcius degree
degreesF = degreesC * (9.0 / 5.0) + 32.0; // calculate the Fahrenheit degree

Serial.print("voltage: "); //print volrage in serial monitor
Serial.print(voltage);
Serial.print(" deg C: "); //print the celcious degree in serial monitor
Serial.print(degreesC);
Serial.print(" deg F: "); //print the farenheit degree in serial monitor
Serial.println(degreesF);

delay(1000);

if (degreesF > 40) //if the temperature is greather than 40 degree, turn off the RGB
{
// Off (all LEDs off):
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, LOW);
}

else if (degreesF >= 20)
// Blue (turn just the blue LED on): // if the temperature is larger than 20 degrees and smaller than 40 degrees, show blue color
{
digitalWrite(RED_PIN, LOW);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}

if (degreesF < 20) //if the temperature is below 20 degrees, show purple color
{
digitalWrite(RED_PIN, HIGH);
digitalWrite(GREEN_PIN, LOW);
digitalWrite(BLUE_PIN, HIGH);
}
}

float getVoltage(int pin) //get voltage

{
return (analogRead(pin) * 0.004882814); // conver 0 to 1023 value to 0 to 5 value (the true voltage)
}

Thanks for support.

That's the easy bit
What should the combined code do?

You will want to get rid of the delays.

Try enabling the the code tags in your post.

I didn't read the code much, but consider the tricks in:

Read the forum guidelines to see how to properly post code and some good information on making a good post.
Use the IDE autoformat tool (ctrl-t or Tools, Auto format) before posting code in code tags.

Please go back and fix your original post

The combined code should turn rgb on when the temperature rises above 30 degrees.

Now that you're back, can you please fix your original post as requested in post #4? When you are asking for help, it doesn't look good if you ignore requests.

1 Like

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