Help needed in merging codes; not sure what i need to get

Hello!

I'm trying to merge my code and have tried reading several tutorials on how to do it but I have no idea on how to get started on this piece of code.
Can someone help me please?

What I have :

  • humidity and temperature sensor (DHT11)
  • soil moisture sensor
  • water level sensor
  • a motor

The idea is to make a self-sustainable plantbox (a greenhouse) . If the humidity or temperature gets too high, then it will make the motor turn a little bit ( the motor is glued to a stick; turning the motor on will make the stick go higher which makes the roof of the greenhouse open; which helps with ventilation. This way it won't be too warm and the humidity will be fine too. As for the water level; the idea is that if the water level is too low that the plant will be watered. This was supposed to be automatic too but we don't have to do that. All that needs to happen is a LED lamp which turns on whenever the water level or soil moisture is too low.
Here are the codes, I'd be happy enough to just get a merged code; the rest will be do-able after.
(I dont have much at all, since I dont know where to start)

Thank you!

Codes:

soil moisture

/*
Soil Moisture Sensor
modified on 21 Feb 2019
by Saeed Hosseini @ Electropeak
Learn Electronics at ElectroPeak - Step by Step Guides & Tutorials
*/
#define SensorPin A0
float sensorValue = 0;
void setup() {
Serial.begin(9600);
}
void loop() {
for (int i = 0; i <= 100; i++)
{
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue/100.0;
Serial.println(sensorValue);
delay(30);
}

motor

// This Arduino example demonstrates bidirectional operation of a
// 28BYJ-48, using a VMA401 - ULN2003 interface board to drive the stepper.
// The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
// a factor of 68. One bipolar winding is on motor pins 1 & 3 and
// the other on motor pins 2 & 4. The step angle is 5.625/64 and the
// operating Frequency is 100pps. Current draw is 92mA.
////////////////////////////////////////////////
//declare variables for the motor pins
int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
int motorSpeed = 1200; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 60; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
//////////////////////////////////////////////////////////////////////////////
void setup() {
//declare the motor pins as outputs
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
Serial.begin(9600);
}
//////////////////////////////////////////////////////////////////////////////
void loop(){
if(count < 60 )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
anticlockwise();
count++;
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
}

water level

/*
Water Level Sensor
modified on 19 Sep 2020
by Mohammad Reza Akbari @ Electropeak
Learn Electronics at ElectroPeak - Step by Step Guides & Tutorials
*/

// Connect Sensor Output to A0
int Sensor = A0;

int ad_value;

void setup()
{
pinMode(Sensor, INPUT);
Serial.begin(9600);
}
void loop()
{
ad_value = analogRead(Sensor);

Serial.print("Output: ");
Serial.println(ad_value);

delay(500);
}

temperature and humidity sensor

#include <dht.h>

#define dht_apin A0 // Analog Pin sensor is connected to

dht DHT;

void setup(){

Serial.begin(9600);
delay(500);//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor

}//end "setup()"

void loop(){
//Start of Program

DHT.read11(dht_apin);

Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("%  ");
Serial.print("temperature = ");
Serial.print(DHT.temperature); 
Serial.println("C  ");

delay(5000);//Wait 5 seconds before accessing sensor again.

//Fastest should be once every two seconds.

}// end loop(


Sorry if I don't respond on time, I'm currently typing this on a school computer so I'll probably be able to only check the thread again by tomorrow or the day after.

It is easy to ,merge codes, but it may take additional coding to make them work together.

#define SensorPin A0
float sensorValue = 0;
int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
int motorSpeed = 1200; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 60; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
int Sensor = A0;
int ad_value;
#include <dht.h>
#define dht_apin A0 // Analog Pin sensor is connected to
dht DHT;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(Sensor, INPUT);
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
delay(500);//Delay to let system boot
Serial.println("DHT11 Humidity & temperature Sensor\n\n");
delay(1000);//Wait before accessing Sensor
}//end "setup()"
}

void loop() {
// put your main code here, to run repeatedly:
for (int i = 0; i <= 100; i++)
{
sensorValue = sensorValue + analogRead(SensorPin);
delay(1);
}
sensorValue = sensorValue/100.0;
Serial.println(sensorValue);
delay(30);
if(count < 60 )
clockwise();
else if (count == countsperrev * 2)
count = 0;
else
anticlockwise();
count++;
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
for(int i = 0; i < 8; i++)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void clockwise()
{
for(int i = 7; i >= 0; i--)
{
setOutput(i);
delayMicroseconds(motorSpeed);
}
}
void setOutput(int out)
{
digitalWrite(motorPin1, bitRead(lookup[out], 0));
digitalWrite(motorPin2, bitRead(lookup[out], 1));
digitalWrite(motorPin3, bitRead(lookup[out], 2));
digitalWrite(motorPin4, bitRead(lookup[out], 3));
ad_value = analogRead(Sensor);

Serial.print("Output: ");
Serial.println(ad_value);

delay(500);
DHT.read11(dht_apin);

Serial.print("Current humidity = ");
Serial.print(DHT.humidity);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(DHT.temperature);
Serial.println("C ");

delay(5000);//Wait 5 seconds before accessing sensor again.
//Fastest should be once every two seconds.

}// end loop(
}

If you have no idea yet where to start I conclude that your knowledge about the fundamental structure of every sketch is low.

Take a look into this tutorial:

Arduino Programming Course

It is easy to understand and has a good mixture between explaining important concepts and example-codes to get you going. So give it a try and report your opinion about this tutorial.

If you have a smartphone or any other access to the internet.
You can learn something even in the afternoon.

best regards Stefan

And if @StefanL38 's link just looks like a giant wall of "what do I do now", and you are thinking "it can't be this hard" heeeeeelp meeeeee!".

Then you might prefer to go to youtube.com, search there for "arduino programming" or "arduino basics" or use your imagination to

find a video course that you think you can tolerate, sit back and dig in.

There are dozens, just look for the prettiest girl or some nice old man or that foreign guy who yucks it up quite a bit but he gets the job done, whatever.

Poke around until some tutor matches your level of experience and feels like you are going to allow them to learn you up, you will still have to put in the work.

a7

Do it gradually. Try and combine two of them and get it working. Soil moisture and water level looks like an easy place to start as they're short.

(I had to change the DHT code a bit to match the librarty I have installed)

void setup()
{
  setupMoisture();
  setupMotor();
  setupWaterLevel();
  setupTempAndHumidity();
}

void loop()
{
  loopMoisture();
  loopMotor();
  loopWaterLevel();
  loopTempAndHumidity();
}

/*
  Soil Moisture Sensor
  modified on 21 Feb 2019
  by Saeed Hosseini @ Electropeak
  Learn Electronics at ElectroPeak - Step by Step Guides & Tutorials
*/
#define SensorPin A0
float sensorValue = 0;
void setupMoisture()
{
  Serial.begin(9600);
}
void loopMoisture()
{
  for (int i = 0; i <= 100; i++)
  {
    sensorValue = sensorValue + analogRead(SensorPin);
    delay(1);
  }
  sensorValue = sensorValue / 100.0;
  Serial.println(sensorValue);
  delay(30);
}


// This Arduino example demonstrates bidirectional operation of a
// 28BYJ-48, using a VMA401 - ULN2003 interface board to drive the stepper.
// The 28BYJ-48 motor is a 4-phase, 8-beat motor, geared down by
// a factor of 68. One bipolar winding is on motor pins 1 & 3 and
// the other on motor pins 2 & 4. The step angle is 5.625/64 and the
// operating Frequency is 100pps. Current draw is 92mA.
////////////////////////////////////////////////
//declare variables for the motor pins
int motorPin1 = 8; // Blue - 28BYJ48 pin 1
int motorPin2 = 9; // Pink - 28BYJ48 pin 2
int motorPin3 = 10; // Yellow - 28BYJ48 pin 3
int motorPin4 = 11; // Orange - 28BYJ48 pin 4
// Red - 28BYJ48 pin 5 (VCC)
int motorSpeed = 1200; //variable to set stepper speed
int count = 0; // count of steps made
int countsperrev = 60; // number of steps per full revolution
int lookup[8] = {B01000, B01100, B00100, B00110, B00010, B00011, B00001, B01001};
//////////////////////////////////////////////////////////////////////////////
void setupMotor()
{
  //declare the motor pins as outputs
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  Serial.begin(9600);
}
//////////////////////////////////////////////////////////////////////////////
void loopMotor()
{
  if (count < 60 )
    clockwise();
  else if (count == countsperrev * 2)
    count = 0;
  else
    anticlockwise();
  count++;
}
//////////////////////////////////////////////////////////////////////////////
//set pins to ULN2003 high in sequence from 1 to 4
//delay "motorSpeed" between each pin setting (to determine speed)
void anticlockwise()
{
  for (int i = 0; i < 8; i++)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}
void clockwise()
{
  for (int i = 7; i >= 0; i--)
  {
    setOutput(i);
    delayMicroseconds(motorSpeed);
  }
}
void setOutput(int out)
{
  digitalWrite(motorPin1, bitRead(lookup[out], 0));
  digitalWrite(motorPin2, bitRead(lookup[out], 1));
  digitalWrite(motorPin3, bitRead(lookup[out], 2));
  digitalWrite(motorPin4, bitRead(lookup[out], 3));
}


/*
  Water Level Sensor
  modified on 19 Sep 2020
  by Mohammad Reza Akbari @ Electropeak
  Learn Electronics at ElectroPeak - Step by Step Guides & Tutorials
*/

// Connect Sensor Output to A0
int Sensor = A0;

int ad_value;

void setupWaterLevel()
{
  pinMode(Sensor, INPUT);
  Serial.begin(9600);
}

void loopWaterLevel()
{
  ad_value = analogRead(Sensor);

  Serial.print("Output: ");
  Serial.println(ad_value);

  delay(500);
}

// temperature and humidity sensor

#include <DHT.h>

#define dht_apin A0 // Analog Pin sensor is connected to

DHT dht(A0, DHT11);

void setupTempAndHumidity()
{

  Serial.begin(9600);
  delay(500);//Delay to let system boot
  Serial.println("DHT11 Humidity & temperature Sensor\n\n");
  delay(1000);//Wait before accessing Sensor

}//end "setup()"

void loopTempAndHumidity()
{
  //Start of Program

  // dht.read11(dht_apin);

  Serial.print("Current humidity = ");
  Serial.print(dht.readHumidity());
  Serial.print("%  ");
  Serial.print("temperature = ");
  Serial.print(dht.readTemperature());
  Serial.println("C  ");

  delay(5000);//Wait 5 seconds before accessing sensor again.
  //Fastest should be once every two seconds.

}// end loop(

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