Advertisement
pleasedontcode

**Veggie Cutter** rev_01

Dec 4th, 2024
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: **Veggie Cutter**
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-12-04 19:09:42
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* the motor is 775DC on off switch and 12 volt jack */
  21.     /* is attached ,the blade mechanism is driven by */
  22.     /* motor,it is battery operated,it uses a switch */
  23. /****** END SYSTEM REQUIREMENTS *****/
  24.  
  25. /* START CODE */
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <EasyButton.h> // https://github.com/evert-arias/EasyButton
  29. #include <Servo.h> // Added for Servo control
  30.  
  31. /****** FUNCTION PROTOTYPES *****/
  32. void setup(void);
  33. void loop(void);
  34.  
  35. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  36. const uint8_t pushbutton_PushButton_PIN_D2      = 2;
  37. #define TRIG_PIN 7
  38. #define ECHO_PIN 8
  39. #define START_BUTTON 2 // This conflicts with pushbutton_PushButton_PIN_D2
  40. #define STOP_BUTTON 3
  41. #define CONVEYOR_PIN 5
  42. #define BLADE_SERVO_PIN 9
  43.  
  44. // Constants
  45. const int CUT_DELAY = 500; // Delay between cuts in milliseconds
  46. const int DISTANCE_THRESHOLD = 10; // Distance to detect vegetable (in cm)
  47.  
  48. // Objects
  49. Servo bladeServo;
  50.  
  51. // Variables
  52. bool machineOn = false;
  53.  
  54. void setup(void)
  55. {
  56.     // put your setup code here, to run once:
  57.     pinMode(pushbutton_PushButton_PIN_D2, INPUT_PULLUP);
  58.  
  59.     // Serial Monitor for Debugging
  60.     Serial.begin(9600);
  61.  
  62.     // Pin Modes
  63.     pinMode(TRIG_PIN, OUTPUT);
  64.     pinMode(ECHO_PIN, INPUT);
  65.     pinMode(START_BUTTON, INPUT_PULLUP);
  66.     pinMode(STOP_BUTTON, INPUT_PULLUP);
  67.     pinMode(CONVEYOR_PIN, OUTPUT);
  68.  
  69.     // Servo Initialization
  70.     bladeServo.attach(BLADE_SERVO_PIN);
  71.     bladeServo.write(0); // Blade in rest position
  72.  
  73.     // Initial States
  74.     digitalWrite(CONVEYOR_PIN, LOW);
  75. }
  76.  
  77. void loop(void)
  78. {
  79.     // put your main code here, to run repeatedly:
  80.     // Check for Start/Stop Buttons
  81.     if (digitalRead(START_BUTTON) == LOW) {
  82.         machineOn = true;
  83.         Serial.println("Machine Started");
  84.     }
  85.     if (digitalRead(STOP_BUTTON) == LOW) {
  86.         machineOn = false;
  87.         Serial.println("Machine Stopped");
  88.     }
  89.  
  90.     if (machineOn) {
  91.         digitalWrite(CONVEYOR_PIN, HIGH); // Start Conveyor
  92.         int distance = getDistance();
  93.  
  94.         if (distance <= DISTANCE_THRESHOLD) {
  95.             Serial.println("Vegetable Detected");
  96.             digitalWrite(CONVEYOR_PIN, LOW); // Stop Conveyor
  97.             cutVegetable(); // Perform Cutting
  98.             delay(1000); // Wait before restarting conveyor
  99.             digitalWrite(CONVEYOR_PIN, HIGH); // Restart Conveyor
  100.         }
  101.     } else {
  102.         digitalWrite(CONVEYOR_PIN, LOW); // Stop Conveyor
  103.     }
  104. }
  105.  
  106. // Function to Measure Distance
  107. int getDistance() {
  108.     // Send Trigger Pulse
  109.     digitalWrite(TRIG_PIN, LOW);
  110.     delayMicroseconds(2);
  111.     digitalWrite(TRIG_PIN, HIGH);
  112.     delayMicroseconds(10);
  113.     digitalWrite(TRIG_PIN, LOW);
  114.  
  115.     // Measure Echo Pulse Duration
  116.     long duration = pulseIn(ECHO_PIN, HIGH);
  117.  
  118.     // Calculate Distance in cm
  119.     int distance = duration * 0.034 / 2;
  120.     return distance;
  121. }
  122.  
  123. // Function to Control Blade Servo for Cutting
  124. void cutVegetable() {
  125.     Serial.println("Cutting...");
  126.     bladeServo.write(90); // Move blade down
  127.     delay(CUT_DELAY); // Cutting time
  128.     bladeServo.write(0); // Move blade back to rest
  129.     delay(CUT_DELAY);
  130. }
  131.  
  132. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement