Connecting two different Arduino codes

Good day everyone, please I need some help . I am working on my final year project and it's a Robotic arm that has a dht11 temperature and humidity sensor attached to the end effector. I am using the Arduino IOT cloud platform to connect the dht11 to the internet for live feedback. I also want to be able to control the arms with the widgets I have built. However I am unable to combine the codes for the dht11 and the code for the control of 6 servo motors . Please can someone help
#dht11 #servomotors
Code for dht
// DHT Temperature & Humidity Sensor
// Unified Sensor Library Example
// Written by Tony DiCola for Adafruit Industries
// Released under an MIT license.

// REQUIRES the following Arduino libraries:

#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <DHT_U.h>

#define DHTPIN 2 // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14 --
// Pin 15 can work but DHT must be disconnected during program upload.

// Uncomment the type of sensor in use:
#define DHTTYPE DHT11 // DHT 11
//define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)

// See guide for details on sensor wiring and usage:
// Overview | DHT11, DHT22 and AM2302 Sensors | Adafruit Learning System

DHT_Unified dht(DHTPIN, DHTTYPE);

uint32_t delayMS;

void setup() {
Serial.begin(9600);
// Initialize device.
dht.begin();
Serial.println(F("DHTxx Unified Sensor Example"));
// Print temperature sensor details.
sensor_t sensor;
dht.temperature().getSensor(&sensor);
Serial.println(F("------------------------------------"));
Serial.println(F("Temperature Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("°C"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("°C"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("°C"));
Serial.println(F("------------------------------------"));
// Print humidity sensor details.
dht.humidity().getSensor(&sensor);
Serial.println(F("Humidity Sensor"));
Serial.print (F("Sensor Type: ")); Serial.println(sensor.name);
Serial.print (F("Driver Ver: ")); Serial.println(sensor.version);
Serial.print (F("Unique ID: ")); Serial.println(sensor.sensor_id);
Serial.print (F("Max Value: ")); Serial.print(sensor.max_value); Serial.println(F("%"));
Serial.print (F("Min Value: ")); Serial.print(sensor.min_value); Serial.println(F("%"));
Serial.print (F("Resolution: ")); Serial.print(sensor.resolution); Serial.println(F("%"));
Serial.println(F("------------------------------------"));
// Set delay between sensor readings based on sensor details.
delayMS = sensor.min_delay / 1000;
}

void loop() {
// Delay between measurements.
delay(delayMS);
// Get temperature event and print its value.
sensors_event_t event;
dht.temperature().getEvent(&event);
if (isnan(event.temperature)) {
Serial.println(F("Error reading temperature!"));
}
else {
Serial.print(F("Temperature: "));
Serial.print(event.temperature);
Serial.println(F("°C"));
}
// Get humidity event and print its value.
dht.humidity().getEvent(&event);
if (isnan(event.relative_humidity)) {
Serial.println(F("Error reading humidity!"));
}
else {
Serial.print(F("Humidity: "));
Serial.print(event.relative_humidity);
Serial.println(F("%"));
}
}
Code for Servo
void loop() {
pwm.setPWM(0, 0, pulseWidth(0));
pwm.setPWM(1, 0, pulseWidth(0));
pwm.setPWM(2, 0, pulseWidth(0));
pwm.setPWM(3, 0, pulseWidth(0));
pwm.setPWM(4, 0, pulseWidth(0));
pwm.setPWM(5 , 0, pulseWidth(0));
delay(1000);
pwm.setPWM(0, 0, pulseWidth(180));
pwm.setPWM(1, 0, pulseWidth(180));
pwm.setPWM(2, 0, pulseWidth(180));
pwm.setPWM(3, 0, pulseWidth(180));
pwm.setPWM(4, 0, pulseWidth(180));
pwm.setPWM(5, 0, pulseWidth(180));
delay(1000);
}

int pulseWidth(int angle)
{
int pulse_wide, analog_value;
pulse_wide = map(angle, 0, 180, MIN_PULSE_WIDTH, MAX_PULSE_WIDTH);
analog_value = int(float(pulse_wide) / 1000000 * FREQUENCY * 4096);
return analog_value;
}

see combining codes

The best way to gain friends on the forum is to read and follow the guides.
If you go back and edit your post, then insert CODE TAGS, helpers will be more likely to play with you.

If you have already tried gluing the two programs together, show that, and add comments or error listings of the parts where you’re having trouble

It’s also a good idea to post a detailed block diagram of how you see the hardware being connected, and a schematic with pin names etc - remember to detail your power sources.

You should post code by using code-tags
There is an automatic function for doing this in the Arduino-IDE
just three steps

  1. press Ctrl-T for autoformatting your code
  2. do a rightclick with the mouse and choose "copy for forum"
  3. paste clipboard into write-window of a posting

best regards Stefan

You can not have two instances of the loop function in one program. Copy all the code from one and put it at the end of the other. The two codes will run one after the other. You might want to rethink your use of delays, and why you have not used a for loop for all of those servo control lines.

Thank You i would do just that

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