#include <WiFi.h>
#include <ThingSpeak.h>
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
long myChannelNumber = 2621914;
const char *myWriteAPIKey = "5SY0UGQK5NMCD78G";
int statusCode;
const int irPin1 = 15;
const int irPin2 = 19; // IR sensor analog input pin
const int ledPin1 = 13;
const int ledPin2 = 5; // LED pin
const int buzzerPin = 12; // Buzzer pin
const int threshold = 500; // Adjust IR sensor threshold value
WiFiClient Client;
void setup() {
Serial.begin(115200); // Initialize serial communication
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, pass);
// Wait for WiFi to connect
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("\nConnected to WiFi.");
ThingSpeak.begin(Client);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(buzzerPin, OUTPUT);
pinMode(irPin1, INPUT);
pinMode(irPin2, INPUT);
}
void loop() {
int irValue1 = analogRead(irPin1);
int irValue2 = analogRead(irPin2);
Serial.print("IR1: ");
Serial.print(irValue1);
Serial.print(" IR2: ");
Serial.println(irValue2);
if (irValue1 < threshold) {
Serial.println("Object detected at IR1");
digitalWrite(ledPin2, HIGH);
tone(buzzerPin, 750); // Produce a 750 Hz sound
delay(1000);
noTone(buzzerPin);
digitalWrite(ledPin2, LOW);
}
else if (irValue2 < threshold) {
Serial.println("Object detected at IR2");
digitalWrite(ledPin1, HIGH);
tone(buzzerPin, 650); // Produce a 650 Hz sound
delay(1000);
noTone(buzzerPin);
digitalWrite(ledPin1, LOW);
}
else {
digitalWrite(ledPin2, LOW);
digitalWrite(ledPin1, LOW);
noTone(buzzerPin);
}
// Update ThingSpeak
ThingSpeak.setField(1, irValue1);
ThingSpeak.setField(2, irValue2);
statusCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
if(statusCode == 200) { //successful writing code
Serial.println("Channel update successful.");
}
else {
Serial.print("Problem Writing data. HTTP error code: ");
Serial.println(statusCode);
}
delay(15000); // Update ThingSpeak every 15 seconds
}