CCCPaste

"Nut Counter" rev_01

/********* 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: "Nut Counter"
	- Source Code NOT compiled for: Arduino Mega
	- Source Code created on: 2024-06-10 11:58:19

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* DC motor is high until the IR sensor senses the */
	/* number of nuts entered using bluetooth module app */
	/* and the nuts fall in a cup placed in a disc(The */
	/* disc has 8 cups and a NEMA 17 stepper motor at */
	/* center). The disc should rotate 45 degrees after */
	/* the nuts */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);

/***** DEFINITION OF Software Serial *****/
const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0 = A0;
const uint8_t bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1 = A1;
SoftwareSerial bluetooth_HC05_mySerial(bluetooth_HC05_mySerial_PIN_SERIAL_RX_A1, bluetooth_HC05_mySerial_PIN_SERIAL_TX_A0);

/***** DEFINITION OF IR Sensor *****/
const uint8_t IR_SENSOR_PIN = 2;
IRrecv irrecv(IR_SENSOR_PIN);
decode_results results;

/***** DEFINITION OF DC Motor *****/
const uint8_t DC_MOTOR_PIN = 3;

/***** DEFINITION OF Stepper Motor *****/
const int stepsPerRevolution = 200;  // Change this to fit the number of steps per revolution for your motor
Stepper stepper(stepsPerRevolution, 8, 9, 10, 11);  // Pins connected to the stepper motor

/***** VARIABLES *****/
int nutCount = 0;
int targetNutCount = 0;

void setup(void)
{
    // Initialize serial communication
    Serial.begin(9600);
    bluetooth_HC05_mySerial.begin(9600);
    
    // Initialize IR receiver
    irrecv.enableIRIn(); // Start the IR receiver
    
    // Initialize DC motor pin
    pinMode(DC_MOTOR_PIN, OUTPUT);
    digitalWrite(DC_MOTOR_PIN, LOW); // Ensure the DC motor is off initially
    
    // Set the speed of the stepper motor
    stepper.setSpeed(60); // Set the speed of the stepper motor
}

void loop(void)
{
    // Check if data is available from the Bluetooth module
    if (bluetooth_HC05_mySerial.available())
    {
        targetNutCount = bluetooth_HC05_mySerial.parseInt(); // Read the target nut count
        Serial.print("Target Nut Count: ");
        Serial.println(targetNutCount);
    }

    // If the IR sensor detects a nut
    if (irrecv.decode(&results))
    {
        nutCount++;
        Serial.print("Nut Count: ");
        Serial.println(nutCount);
        irrecv.resume(); // Receive the next value

        // If the nut count reaches the target nut count
        if (nutCount >= targetNutCount)
        {
            digitalWrite(DC_MOTOR_PIN, LOW); // Turn off the DC motor
            delay(1000); // Wait for nuts to settle
            stepper.step(stepsPerRevolution / 8); // Rotate the disc by 45 degrees
            nutCount = 0; // Reset the nut count
        }
        else
        {
            digitalWrite(DC_MOTOR_PIN, HIGH); // Keep the DC motor running
        }
    }
    else
    {
        digitalWrite(DC_MOTOR_PIN, HIGH); // Keep the DC motor running
    }
}

/* END CODE */