Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* Pleasedontcode.com **********
- Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- - Terms and Conditions:
- You have a non-exclusive, revocable, worldwide, royalty-free license
- for personal and commercial use. Attribution is optional; modifications
- are allowed, but you're responsible for code maintenance. We're not
- liable for any loss or damage. For full terms,
- please visit pleasedontcode.com/termsandconditions.
- - Project: **Veggie Cutter**
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-12-04 19:09:42
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* the motor is 775DC on off switch and 12 volt jack */
- /* is attached ,the blade mechanism is driven by */
- /* motor,it is battery operated,it uses a switch */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
- #include <Servo.h> // Added for Servo control
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t pushbutton_PushButton_PIN_D2 = 2;
- #define TRIG_PIN 7
- #define ECHO_PIN 8
- #define START_BUTTON 2 // This conflicts with pushbutton_PushButton_PIN_D2
- #define STOP_BUTTON 3
- #define CONVEYOR_PIN 5
- #define BLADE_SERVO_PIN 9
- // Constants
- const int CUT_DELAY = 500; // Delay between cuts in milliseconds
- const int DISTANCE_THRESHOLD = 10; // Distance to detect vegetable (in cm)
- // Objects
- Servo bladeServo;
- // Variables
- bool machineOn = false;
- void setup(void)
- {
- // put your setup code here, to run once:
- pinMode(pushbutton_PushButton_PIN_D2, INPUT_PULLUP);
- // Serial Monitor for Debugging
- Serial.begin(9600);
- // Pin Modes
- pinMode(TRIG_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- pinMode(START_BUTTON, INPUT_PULLUP);
- pinMode(STOP_BUTTON, INPUT_PULLUP);
- pinMode(CONVEYOR_PIN, OUTPUT);
- // Servo Initialization
- bladeServo.attach(BLADE_SERVO_PIN);
- bladeServo.write(0); // Blade in rest position
- // Initial States
- digitalWrite(CONVEYOR_PIN, LOW);
- }
- void loop(void)
- {
- // put your main code here, to run repeatedly:
- // Check for Start/Stop Buttons
- if (digitalRead(START_BUTTON) == LOW) {
- machineOn = true;
- Serial.println("Machine Started");
- }
- if (digitalRead(STOP_BUTTON) == LOW) {
- machineOn = false;
- Serial.println("Machine Stopped");
- }
- if (machineOn) {
- digitalWrite(CONVEYOR_PIN, HIGH); // Start Conveyor
- int distance = getDistance();
- if (distance <= DISTANCE_THRESHOLD) {
- Serial.println("Vegetable Detected");
- digitalWrite(CONVEYOR_PIN, LOW); // Stop Conveyor
- cutVegetable(); // Perform Cutting
- delay(1000); // Wait before restarting conveyor
- digitalWrite(CONVEYOR_PIN, HIGH); // Restart Conveyor
- }
- } else {
- digitalWrite(CONVEYOR_PIN, LOW); // Stop Conveyor
- }
- }
- // Function to Measure Distance
- int getDistance() {
- // Send Trigger Pulse
- digitalWrite(TRIG_PIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIG_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIG_PIN, LOW);
- // Measure Echo Pulse Duration
- long duration = pulseIn(ECHO_PIN, HIGH);
- // Calculate Distance in cm
- int distance = duration * 0.034 / 2;
- return distance;
- }
- // Function to Control Blade Servo for Cutting
- void cutVegetable() {
- Serial.println("Cutting...");
- bladeServo.write(90); // Move blade down
- delay(CUT_DELAY); // Cutting time
- bladeServo.write(0); // Move blade back to rest
- delay(CUT_DELAY);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement