Go Down

Topic: Combining codes (Read 143 times) previous topic - next topic

arowe4610

I am trying to combine 5 bits of code for an environmental monitor

I have a
DHT11 Sensor
LCD Screen
Utrasonic sensor
LDR
Servo

I am trying to have a breadboard and UNO board powering these sensors so when the temperature in a room goes above 25 degrees celcius the window will open. My code I have for this so far is in seperate parts these are

Code: [Select]


#include <dht.h>

dht DHT;

#define DHT11_PIN 7

void setup(){
  Serial.begin(9600);
}

void loop()
{
  int chk = DHT.read11(DHT11_PIN);
  Serial.print("Temperature = ");
  Serial.println(DHT.temperature);
  Serial.print("Humidity = ");
  Serial.println(DHT.humidity);
  delay(1000);
}



Code: [Select]

#include <LiquidCrystal.h>

LiquidCrystal lcd(12, 11, 5, 4, 3, 2);

void setup() {
  lcd.begin(16, 2);
  }

void loop() {
  lcd.print("hello, world!");
  delay(500);
  lcd.clear();
  delay(500);
}


Code: [Select]

/*
Adafruit Arduino - Lesson 14. Sweep
*/

#include <Servo.h>

int servoPin = 9;
 
Servo servo;  
 
int angle = 0;   // servo position in degrees
 
void setup()
{
  servo.attach(servoPin);
}
 
 
void loop()
{
  // scan from 0 to 180 degrees
  for(angle = 0; angle < 180; angle++)  
  {                                  
    servo.write(angle);              
    delay(15);                  
  }
  // now scan back from 180 to 0 degrees
  for(angle = 180; angle > 0; angle--)    
  {                                
    servo.write(angle);          
    delay(15);      
  }
}


Code: [Select]

#include "NewPing.h"

#define TRIGGER_PIN  12  // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN     11  // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.

NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.

void setup() {
  Serial.begin(115200); // Open serial monitor at 115200 baud to see ping results.
}

void loop() {
  delay(500);  // Wait 500ms between pings (about 2 pings/sec). 29ms should be the shortest delay between pings.
  unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
  Serial.print("Ping: ");
  Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
  Serial.println("cm");
}


and I also am wondering what code I would need to incorporate LDR into this?

Any help is much appreciated!

J-M-L

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

arowe4610

I don't know much about coding. I tried to combine them by seperating the setups and the void loops and uploading them to my board but it didn't work.

J-M-L

#3
Aug 13, 2017, 10:45 am Last Edit: Aug 13, 2017, 10:46 am by J-M-L
you need to merge the relevant parts of the setup() into one setup() and then redesign the loop to do everything that is needed

do you understand each of the codes?  if not this is where to start (possibly do some of the tutorials - if you don't learn to program you'll go nowhere and it will be a miserable experience trying to do things you don't understand)
Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

arowe4610

If anybody knows how to combine these codes could you post the completed code below please

PaulRB

Many people on this forum know how to merge your codes. But they won't just do it for you. There is the "Gigs and Collaborations" section of the forum where you can offer to pay someone to do this. But in this section of the forum, they are here to help you learn and understand. That won't happen if you do not put any effort in.

J-M-L

and we don't know how you want to combine them anyway...

Combining does not mean anything. You need to define exactly what your program needs to do.

From your initial explanation you need to read the temperature, test the temperature and based on the test (and window status) open or close the window. But we have not clue what is the LCD for? what is the Utrasonic sensor for ? what's the LDR for ?

I suggest you try to put together a small code that reads the temperature, test against 25°C and if you just crossed the threshold print on the Serial Monitor "Opening the window" and if you crossed below 25° then print on the Serial Monitor "Closing the window"

Please do not PM me for help,  others will benefit as well if you post your question publicly on the forums
Pas de messages privés SVP

terryking228

#7
Aug 14, 2017, 02:28 am Last Edit: Aug 14, 2017, 02:29 am by terryking228
Hi,
There is a suggested approach to combining code On This Page:
http://arduino-info.wikispaces.com/CombiningArduinoSketches
Regards, Terry King terry@yourduino.com  - Check great prices, devices and Arduino-related boards at http://YourDuino.com
HOW-TO: http://ArduinoInfo.Info

Go Up