#include <Servo.h>
const int coolingFanPin = 8; // Pin connected to the cooling fan
const int loadCellPin = A0; // Analog pin connected to the load cell
const int servoPin = 9; // Pin connected to the servo motor
const int stepperPin1 = 4; // Pin for controlling stepper motor step signal
const int stepperPin2 = 5; // Pin for controlling stepper motor direction signal
const int dcMotorPin = 6; // Pin connected to the DC motor
const int fanDuration = 180000; // Duration for running the cooling fan (3 minutes in milliseconds)
const int waitWeight = 100; // Weight threshold for closing the hatch (in grams)
const int stepperSteps1 = 200; // Number of steps for the first stepper motor movement
const int stepperSteps2 = 400; // Number of steps for the second stepper motor movement
const int dcMotorDuration = 3000; // Duration for running the DC motor (3 seconds in milliseconds)
Servo hatchServo; // Create a servo object for the hatch
void setup() {
Serial.begin(9600); // Set serial communication baud rate
pinMode(coolingFanPin, OUTPUT);
pinMode(loadCellPin, INPUT);
hatchServo.attach(servoPin); // Attach the servo motor to the specified pin
pinMode(stepperPin1, OUTPUT);
pinMode(stepperPin2, OUTPUT);
pinMode(dcMotorPin, OUTPUT);
}
void loop() {
if (Serial.available() > 0) { // Check for incoming serial message
char incomingByte = Serial.read();
if (incomingByte == 'S') { // Start command received from Arduino Mega
Serial.println("Starting process...");
// Step 1: Run the cooling fan
digitalWrite(coolingFanPin, HIGH);
Serial.println("Running cooling fan...");
delay(fanDuration);
digitalWrite(coolingFanPin, LOW);
// Step 2: Open the hatch (180 degrees)
hatchServo.write(180);
Serial.println("Opening hatch...");
delay(2000); // Add a short delay for servo movement
// Step 3: Wait for weight and close hatch
int weight = getWeight(); // Read weight from load cell
while (weight < waitWeight) {
weight = getWeight();
delay(1000); // Check weight every second
}
hatchServo.write(0); // Close the hatch
Serial.println("Hatch closed (weight reached).");
// Step 4: Run the stepper motor (200 steps)
runStepperMotor(stepperSteps1);
// Step 5: Run the DC motor for 3 seconds
digitalWrite(dcMotorPin, HIGH);
Serial.println("Running DC motor...");
delay(dcMotorDuration);
digitalWrite(dcMotorPin, LOW);
// Step 6: Run the stepper motor (400 steps)
runStepperMotor(stepperSteps2);
Serial.println("Process completed.");
} else {
Serial.println("Invalid command received.");
}
}
}
int getWeight() {
int weightRaw = analogRead(loadCellPin);
// Implement your specific calibration logic here to convert raw ADC value to weight
// This example assumes a linear conversion (replace with your calibration function)
return weightRaw / 10; // Replace with your calibration function
}
void runStepperMotor(int steps) {
for (int i = 0; i < steps; i++) {
// Implement your specific stepper motor control logic here
// This example assumes a simple stepping sequence (replace with your driver's logic)
digitalWrite(stepperPin1, HIGH);
delayMicroseconds(1000);
digitalWrite(stepperPin1, LOW);
delayMicroseconds(1000);
digitalWrite(stepperPin2, !digitalRead(stepperPin2)); // Toggle direction
}
}