Advertisement
pleasedontcode

"Nut Counter" rev_01

Jun 10th, 2024
434
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: "Nut Counter"
  13.     - Source Code NOT compiled for: Arduino Mega
  14.     - Source Code created on: 2024-06-10 11:58:19
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* DC motor is high until the IR sensor senses the */
  21.     /* number of nuts entered using bluetooth module app */
  22.     /* and the nuts fall in a cup placed in a disc(The */
  23.     /* disc has 8 cups and a NEMA 17 stepper motor at */
  24.     /* center). The disc should rotate 45 degrees after */
  25.     /* the nuts */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <SoftwareSerial.h>
  30. #include <IRremote.hpp>
  31. #include <Stepper.h>
  32.  
  33. /****** FUNCTION PROTOTYPES *****/
  34. void setup(void);
  35. void loop(void);
  36.  
  37. /***** DEFINITION OF Software Serial *****/
  38. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
  39. const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
  40. SoftwareSerial bluetooth_HC05_mySerial(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0);
  41.  
  42. /***** DEFINITION OF IR Sensor *****/
  43. const uint8_t IR_SENSOR_PIN = 2;
  44. IRrecv irrecv(IR_SENSOR_PIN);
  45. decode_results results;
  46.  
  47. /***** DEFINITION OF DC Motor *****/
  48. const uint8_t DC_MOTOR_PIN = 3;
  49.  
  50. /***** DEFINITION OF Stepper Motor *****/
  51. const int stepsPerRevolution = 200;  // Change this to fit the number of steps per revolution for your motor
  52. Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);  // Pins connected to the stepper motor
  53.  
  54. /***** VARIABLES *****/
  55. int nutCount = 0;
  56. int targetNutCount = 0;
  57.  
  58. void setup(void)
  59. {
  60.     // Initialize serial communication
  61.     Serial.begin(9600);
  62.     bluetooth_HC05_mySerial.begin(9600);
  63.    
  64.     // Initialize IR receiver
  65.     irrecv.enableIRIn(); // Start the IR receiver
  66.    
  67.     // Initialize DC motor pin
  68.     pinMode(DC_MOTOR_PIN, OUTPUT);
  69.     digitalWrite(DC_MOTOR_PIN, LOW); // Ensure the DC motor is off initially
  70.    
  71.     // Set the speed of the stepper motor
  72.     stepper.setSpeed(60); // Set the speed of the stepper motor
  73. }
  74.  
  75. void loop(void)
  76. {
  77.     // Check if data is available from the Bluetooth module
  78.     if (bluetooth_HC05_mySerial.available())
  79.     {
  80.         targetNutCount = bluetooth_HC05_mySerial.parseInt(); // Read the target nut count
  81.         Serial.print("Target Nut Count: ");
  82.         Serial.println(targetNutCount);
  83.     }
  84.  
  85.     // If the IR sensor detects a nut
  86.     if (irrecv.decode(&results))
  87.     {
  88.         nutCount++;
  89.         Serial.print("Nut Count: ");
  90.         Serial.println(nutCount);
  91.         irrecv.resume(); // Receive the next value
  92.  
  93.         // If the nut count reaches the target nut count
  94.         if (nutCount >= targetNutCount)
  95.         {
  96.             digitalWrite(DC_MOTOR_PIN, LOW); // Turn off the DC motor
  97.             delay(1000); // Wait for nuts to settle
  98.             stepper.step(stepsPerRevolution / 8); // Rotate the disc by 45 degrees
  99.             nutCount = 0; // Reset the nut count
  100.         }
  101.         else
  102.         {
  103.             digitalWrite(DC_MOTOR_PIN, HIGH); // Keep the DC motor running
  104.         }
  105.     }
  106.     else
  107.     {
  108.         digitalWrite(DC_MOTOR_PIN, HIGH); // Keep the DC motor running
  109.     }
  110. }
  111.  
  112. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement