im creating a smart dustbin that can automatically open is lid while sensing someone in front of it also it can sense temp,humid and CO and calculate trash level and upload those data to thinkspeak. im having trouble to merge this two codes together so that they can work at the same time. the 1st one is for open/close lid using servomotor and ultrasonic and the 2nd code basically for the rest. IM USING ARDUINO NANO 33 IOT.
#include <Servo.h> //servo library
Servo servo;
int trigPin = 5;
int echoPin = 6;
int servoPin = 7;
int led= 13;
long aver[3]; //array for average
void setup() {
Serial.begin(9600);
servo.attach(servoPin);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
servo.write(0); //close cap on power on
delay(100);
servo.detach();
}
void measure() {
digitalWrite(10,HIGH);
digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);
digitalWrite(trigPin, LOW);
pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
dist = (duration/2) / 29.1; //obtain distance
}
void loop() {
for (int i=0;i<=2;i++) { //average distance
measure();
aver[i]=dist;
delay(10); //delay between measurements
}
dist=(aver[0]+aver[1]+aver[2])/3;
if ( dist<50 ) {
//Change distance as per your need
servo.attach(servoPin);
delay(1);
servo.write(0);
delay(3000);
servo.write(150);
delay(1000);
servo.detach();
}
}
#include <Servo.h> //servo library
Servo servo;
int servoPin = 7;
const int soundSensorPin = A0; // Define the analog pin for sound sensor
#include <WiFiNINA.h>
#include <WiFiSSLClient.h>
#include <DHT.h>
// Replace with your network credentials
const char* ssid = "snekib";
const char* password = "snekib654";
// Replace with your ThingSpeak API key
String apiKey = "ZECXNGXNTB6U5B3R";
// Replace with your ThingSpeak channel ID
const unsigned long channelId = 2059264;
// Set DHT22 sensor pin
const int dhtPin = 4;
// Set MQ-7 sensor pin
const int mq7Pin = A1;
// Set HC-SR04 sensor pins
const int TRIG_PIN = 6;
const int ECHO_PIN = 5;
float duration_us, distance_cm;
long DustbinL = 60, trashlvl;
// Initialize DHT sensor
DHT dht(dhtPin, DHT22);
// Initialize WiFi client
WiFiSSLClient client;
// ThingSpeak server details
const char* server = "api.thingspeak.com";
const int httpsPort = 443;
void setup() {
Serial.begin(9600);
pinMode(soundSensorPin, INPUT);
servo.attach(servoPin);
servo.write(0); //close cap on power on
delay(100);
servo.detach();
pinMode(TRIG_PIN, OUTPUT); // set arduino pin to output mode
pinMode(ECHO_PIN, INPUT); // set arduino pin to input mode
// Connect to Wi-Fi network
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Initialize DHT sensor
dht.begin();
}
void loop() {
// Wait a few seconds between measurements
delay(5000);
int sensorValue = analogRead(soundSensorPin);
if (sensorValue < 770 || sensorValue > 800)
{
Serial.print("clap detected");
Serial.println(sensorValue);
servo.attach(servoPin);
delay(1);
servo.write(0);
delay(3000);
servo.write(150);
delay(1000);
servo.detach();
}
delay(10);
// Read temperature and humidity from DHT22 sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Print temperature and humidity to serial monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C");
Serial.print("\t");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print(" %");
Serial.println();
// Read carbon monoxide data from MQ-7 sensor
int mq7Value = analogRead(mq7Pin);
// Convert analog reading to ppm using MQ-7 sensor curve
float mq7Voltage = mq7Value * (5.0 / 1023.0);
float mq7Resistance = (5.0 - mq7Voltage) / mq7Voltage;
float mq7PPM = pow(10.0, ((log10(mq7Resistance) - 1.65) / (-2.63)));
// Print carbon monoxide data to serial monitor
Serial.print("Carbon Monoxide: ");
Serial.print(mq7PPM);
Serial.print(" ppm");
Serial.println();
// Read distance from HC-SR04 sensor
// generate 10-microsecond pulse to TRIG pin
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
// measure duration of pulse from ECHO pin
duration_us = pulseIn(ECHO_PIN, HIGH);
// calculate the distance
distance_cm = 0.017 * duration_us;
trashlvl = ((DustbinL - distance_cm) / DustbinL) * 100;
// print the value to Serial Monitor
Serial.print("distance: ");
Serial.print(distance_cm);
Serial.println(" cm");
Serial.print("Trash level: ");
Serial.print(trashlvl);
Serial.println(" %");
// Build HTTP request URL with sensor data
String url = "/update?api_key=";
url += apiKey;
url += "&field1=";
url += String(temperature);
url += "&field2=";
url += String(humidity);
url += "&field3=";
url += String(mq7PPM);
url += "&field4=";
url += String(trashlvl);
// Send HTTP request to ThingSpeak
if (client.connect(server, httpsPort)) {
Serial.println("Sending data to ThingSpeak...");
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + server + "\r\n" +
"User-Agent: Arduino/1.0\r\n" +
"Connection: close\r\n\r\n");
} else {
Serial.println("Failed to connect to ThingSpeak");
}
// Wait for server response
while (client.connected()) {
String line = client.readStringUntil('\r');
if (line.startsWith("HTTP/1.1")) {
if (line.indexOf("200 OK") == -1) {
Serial.println("ThingSpeak server error");
}
}
}
// Close connection
client.stop();
}