Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 6

#include <Wire.

h>

#include <Adafruit_GFX.h>

#include <Adafruit_SSD1306.h>

// Define OLED display dimensions and I2C address

#define SCREEN_WIDTH 128

#define SCREEN_HEIGHT 64

#define OLED_RESET -1

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);

// Define ultrasonic sensor pins

const int trigPin = 6; // Trigger pin for ultrasonic sensor

const int echoPin = 7; // Echo pin for ultrasonic sensor

const int inductiveSensorPin = 2; // Pin for inductive proximity sensor

const int capacitiveSensorPin = 3; // Pin for capacitive proximity sensor

const int relayInductivePin = 4; // Pin for the relay controlled by inductive sensor

const int relayCapacitivePin = 5; // Pin for the relay controlled by capacitive sensor

const int relayUltrasonicPin = 10; // Pin for the relay controlled by ultrasonic sensor

const int resetButtonPin = 8; // Pin for the reset button

const int screenSwitchPin = 9; // Pin for switching screens

int inductiveCount = 0; // Counter for inductive sensor

int capacitiveCount = 0; // Counter for capacitive sensor

bool prevInductiveState = HIGH; // Previous state of the inductive sensor

bool prevCapacitiveState = HIGH; // Previous state of the capacitive sensor

bool displayMainScreen = true; // Flag to track which screen to display

void setup() {

pinMode(inductiveSensorPin, INPUT);
pinMode(capacitiveSensorPin, INPUT);

pinMode(resetButtonPin, INPUT_PULLUP); // Use internal pull-up resistor

pinMode(screenSwitchPin, INPUT_PULLUP); // Use internal pull-up resistor

pinMode(relayInductivePin, OUTPUT);

pinMode(relayCapacitivePin, OUTPUT);

pinMode(relayUltrasonicPin, OUTPUT);

digitalWrite(relayInductivePin, LOW); // Ensure relay is off initially

digitalWrite(relayCapacitivePin, LOW); // Ensure relay is off initially

digitalWrite(relayUltrasonicPin, HIGH); // Ensure relay is off initially

pinMode(trigPin, OUTPUT);

pinMode(echoPin, INPUT);

// Initialize OLED display

if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64

Serial.println(F("SSD1306 allocation failed"));

for(;;);

display.display();

delay(2000); // Pause for 2 seconds

// Display initial screen based on flag

if (displayMainScreen) {

displayMain();

} else {

displaySecond();

// Display initial sensor counts


updateDisplay();

void loop() {

bool inductiveDetected = digitalRead(inductiveSensorPin) == LOW; // Assuming LOW means object


detected

bool capacitiveDetected = digitalRead(capacitiveSensorPin) == LOW; // Assuming LOW means object


detected

bool resetButtonPressed = digitalRead(resetButtonPin) == LOW; // Assuming LOW means button


pressed

bool screenSwitchPressed = digitalRead(screenSwitchPin) == LOW; // Assuming LOW means button


pressed

// Control the relay based on the inductive sensor input

digitalWrite(relayInductivePin, inductiveDetected ? HIGH : LOW);

// Control the relay based on the capacitive sensor input

digitalWrite(relayCapacitivePin, capacitiveDetected ? HIGH : LOW);

// Check for state changes in the inductive sensor

if (inductiveDetected && !prevInductiveState) {

inductiveCount++;

updateDisplay();

prevInductiveState = inductiveDetected;

// Check for state changes in the capacitive sensor

if (capacitiveDetected && !prevCapacitiveState) {

capacitiveCount++;

updateDisplay();

prevCapacitiveState = capacitiveDetected;
// Check if the reset button is pressed

if (resetButtonPressed) {

inductiveCount = 0;

capacitiveCount = 0;

updateDisplay();

// Check if the screen switch button is pressed

if (screenSwitchPressed) {

displayMainScreen = !displayMainScreen; // Toggle the screen flag

if (displayMainScreen) {

displayMain();

} else {

displaySecond();

// Ultrasonic sensor logic

long duration;

int distance;

// Clear the trigger pin

digitalWrite(trigPin, LOW);

delayMicroseconds(2);

// Set the trigger pin HIGH for 10 microseconds

digitalWrite(trigPin, HIGH);

delayMicroseconds(10);

digitalWrite(trigPin, LOW);
// Read the echo pin

duration = pulseIn(echoPin, HIGH);

// Calculate the distance

distance = duration * 0.034 / 2;

// Check if distance is less than 10 cm

if (distance <= 10) {

digitalWrite(relayUltrasonicPin, LOW);

delay(5000); // Wait for 5 seconds

digitalWrite(relayUltrasonicPin, HIGH);

void updateDisplay() {

if (displayMainScreen) {

displayMain();

} else {

displaySecond();

void displayMain() {

display.clearDisplay();

display.setTextSize(2);

display.setTextColor(SSD1306_WHITE);

display.setCursor(0, 10);

display.print("PLASTIC: ");

display.print(inductiveCount);

display.setCursor(0, 50);

display.print("CAN: ");
display.print(capacitiveCount);

display.display();

void displaySecond() {

display.clearDisplay();

display.setTextSize(2);

display.setTextColor(SSD1306_WHITE);

display.setCursor(0, 0);

display.println("THE");

display.setCursor(0, 16);

display.println("THINKERS");

display.setTextSize(1);

display.setCursor(0, 40);

display.println("LUASSKAN AURAMU");

display.display();

You might also like