#include <AccelStepper.h>
#include "HX711.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// new
#include <WiFi.h>
#include <ThingSpeak.h>
unsigned long int ChannelNumber = 2628596;
const char* writeAPIKey = "PVPDDL1304HOFIJM";
const char* readAPIKey = "VD1J40LPX08544KW";
const char* server = "api.thingspeak.com";
// Stepper Motor Connections
#define STEP_PIN 32
#define DIR_PIN 33
#define ENABLE_PIN 25
// Load Cell Pins (Primary for Weight Detection)
#define LOADCELL_DOUT_PIN 4
#define LOADCELL_SCK_PIN 16
// Load Cell Pins (Secondary for Restock Detection)
#define LOADCELL_DOUT_PIN_2 17
#define LOADCELL_SCK_PIN_2 19
// LED Pins
#define RED_LED_PIN 26
#define GREEN_LED_PIN 27
// Switch Pins
#define STOCK_SWITCH_PIN 12 // Switch for stocking mechanism
#define REMOVE_WEIGHT_SWITCH_PIN 13 // Switch to remove 50kg from total weight
// Emergency Stop Button Pin
#define EMERGENCY_STOP_PIN 14 // Define the pin for the emergency stop button
// Ultrasonic Sensor Connections
const int trigPin = 5;
const int echoPin = 18;
// Distance and Weight Thresholds
const float distanceThreshold = 20.0; // 20 cm
const float weightThreshold = 25.0; // 25 kg
// Inventory Management Variables
int item1Count = 0; // Items added when motor runs clockwise
int item2Count = 0; // Items added when motor runs counterclockwise
float totalWeight = 35; // Total accumulated weight
float restockWeight = 95;
// Create an instance of the AccelStepper library
AccelStepper stepper(AccelStepper::DRIVER, STEP_PIN, DIR_PIN);
// Create instances of the HX711 library
HX711 scale;
HX711 scale2; // Secondary load cell for restocking
LiquidCrystal_I2C lcd(0x27, 20, 4); // I2C address 0x27, 20 columns, 4 rows
// Calibration factors for the load cells (adjust as needed)
const float calibrationFactor1 = 420.0; // Calibration factor for primary load cell
const float calibrationFactor2 = 420.0; // Calibration factor for secondary load cell
// Password Variables
const String correctPassword = "1234"; // Set your password here
String enteredPassword = "";
void setup() {
// Set up the stepper motor
pinMode(ENABLE_PIN, OUTPUT);
digitalWrite(ENABLE_PIN, LOW); // Enable the driver
stepper.setMaxSpeed(1000); // Set max speed (steps per second)
stepper.setAcceleration(500); // Set acceleration (steps per second^2)
// Set up the ultrasonic sensor
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
// Set up the load cells
scale.begin(LOADCELL_DOUT_PIN, LOADCELL_SCK_PIN);
scale.set_scale(calibrationFactor1); // Set the calibration factor for primary load cell
scale.tare(); // Reset the scale to 0
scale2.begin(LOADCELL_DOUT_PIN_2, LOADCELL_SCK_PIN_2);
scale2.set_scale(calibrationFactor2); // Set the calibration factor for secondary load cell
scale2.tare(); // Reset the secondary scale to 0
// Set up LEDs
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
digitalWrite(RED_LED_PIN, LOW); // Ensure LEDs are off initially
digitalWrite(GREEN_LED_PIN, LOW);
// Set up switches
pinMode(STOCK_SWITCH_PIN, INPUT_PULLUP); // Stock switch
pinMode(REMOVE_WEIGHT_SWITCH_PIN, INPUT_PULLUP); // Remove weight switch
// Set up the emergency stop button
pinMode(EMERGENCY_STOP_PIN, INPUT_PULLUP); // Emergency stop button
// Set up the LCD
lcd.begin(20, 4);
lcd.init();
lcd.backlight();
Serial.begin(115200);
lcd.clear();
lcd.print("Enter Password:");
enteredPassword = ""; // Reset entered password
getPassword(); // Get password input
//New
// Initialising WiFi
char ssid[] = "Wokwi-GUEST"; // Replace with your WiFi SSID
char pass[] = ""; // Replace with your WiFi password
WiFiClient client;
WiFi.begin(ssid, pass);
while(WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println();
Serial.println("WiFi Connected.");
Serial.println(WiFi.localIP());
ThingSpeak.begin(client);
}
void loop() {
// Check if the emergency stop button is pressed
if (digitalRead(EMERGENCY_STOP_PIN) == HIGH) {
// If the emergency stop button is pressed
lcd.clear();
lcd.print("Emergency Shutdown");
stepper.stop(); // Stop the stepper motor
digitalWrite(RED_LED_PIN, LOW); // Turn off LEDs
digitalWrite(GREEN_LED_PIN, LOW);
while (true) {
// Stay in this loop until the board is reset
}
}
// Only run the main functionality if the password is correct
if (enteredPassword == correctPassword) {
runMainFunctionality();
} else {
lcd.setCursor(0, 0);
lcd.print("Access Denied! ");
delay(1000); // Show access denied message for a second
lcd.clear();
lcd.print("Enter Password: ");
enteredPassword = ""; // Reset entered password
getPassword(); // Get password input
}
}
void getPassword() {
// This function simulates getting a password input
// In a real application, you would read from a keypad or serial input
while (true) {
if (Serial.available()) {
char ch = Serial.read();
if (ch == '\n') { // Enter key pressed
break;
} else {
enteredPassword += ch; // Append character to entered password
lcd.setCursor(0, 1);
lcd.print("Password: " + enteredPassword);
}
}
}
lcd.clear();
}
void runMainFunctionality() {
long duration;
float distance;
float weight;
// Trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the echo pulse duration
duration = pulseIn(echoPin, HIGH);
// Convert duration to distance
distance = (duration * 0.034) / 2;
// Read the weight from the primary load cell
if (scale.wait_ready_timeout(200)) {
weight = scale.get_units();
Serial.print("Weight: ");
Serial.print(weight);
Serial.println(" Kg");
} else {
Serial.println("Primary HX711 not found.");
return; // Exit loop if scale is not ready
}
// Read the weight from the secondary load cell for restocking
if (scale2.wait_ready_timeout(200)) {
restockWeight = scale2.get_units() * 10; // Get restock weight
Serial.print("Restock Weight: ");
Serial.print(restockWeight);
Serial.println(" Kg");
} else {
Serial.println("Secondary HX711 not found.");
}
// Check if the stock switch is ON
if (digitalRead(STOCK_SWITCH_PIN) == LOW) {
// Only allow stocking if the remove weight switch is OFF
if (digitalRead(REMOVE_WEIGHT_SWITCH_PIN) == HIGH) {
if (distance < distanceThreshold) {
if (weight > 0) {
if (weight > weightThreshold) {
stepper.setSpeed(-200); // Negative speed for counterclockwise rotation
digitalWrite(RED_LED_PIN, HIGH); // Turn on red LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off green LED
item2Count++; // Increment Item 2 count
totalWeight += weight; // Update total weight
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("Total W: ");
lcd.print(totalWeight);
Serial.println("Weight limit exceeded, spinning counterclockwise!");
} else {
stepper.setSpeed(200); // Positive speed for clockwise rotation
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(GREEN_LED_PIN, HIGH); // Turn on green LED
item1Count++; // Increment Item 1 count
totalWeight += weight; // Update total weight
lcd.clear();
lcd.setCursor(0, 2);
lcd.print("Total W: ");
lcd.print(totalWeight);
Serial.println("Conveyor running, spinning clockwise...");
}
}
stepper.runSpeed(); // Continuously run the stepper motor at the set speed
} else {
stepper.stop(); // Stop the motor if distance is greater than or equal to the threshold
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off green LED
Serial.println("Distance too great, stopping conveyor.");
}
} else {
// If removing switch is ON, stop the motor
stepper.stop();
digitalWrite(RED_LED_PIN, LOW); // Turn off red LED
digitalWrite(GREEN_LED_PIN, LOW); // Turn off green LED
Serial.println("Removing in progress, stopping stocking conveyor.");
}
}
// Display item counts and total weight on the LCD
lcd.setCursor(0, 0);
lcd.print("Item 1: ");
lcd.print(item1Count);
lcd.setCursor(0, 1);
lcd.print("Item 2: ");
lcd.print(item2Count);
if (totalWeight < restockWeight) {
lcd.setCursor(0, 3);
lcd.print("Restock Needed! ");
Serial.println("Restock needed!");
ThingSpeak.setField(1, totalWeight); // Set Field 1 with totalWeight value
int responseCode = ThingSpeak.writeFields(ChannelNumber, writeAPIKey);
if (responseCode == 200) {
Serial.println("Update successful");
} else {
lcd.setCursor(0, 3);
lcd.print("Restock Not Needed");
}
delay(1000); // Shorter delay to improve responsiveness
// Check if the remove weight switch is pressed
if (digitalRead(REMOVE_WEIGHT_SWITCH_PIN) == LOW) {
if (digitalRead(STOCK_SWITCH_PIN) == HIGH) {
if(totalWeight<50)
{
lcd.clear();
lcd.print("Add More Weights");
delay(1000);
lcd.clear();
}
else{
totalWeight -= 50.0; // Decrease total weight by 50 kg
if (totalWeight < 0) {
totalWeight = 0; // Prevent negative weight
}
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Decreased by 50kg");
lcd.setCursor(0, 2);
lcd.print("Total W: ");
lcd.print(totalWeight);
delay(3000);
lcd.clear(); // Delay to show the message
}
}
}
}
}
Loading
esp32-devkit-c-v4
esp32-devkit-c-v4
Loading
ssd1306
ssd1306