int FORWARD = 1;
int REVERSE = 0;
int IR5_status = 0;
/*** IRSensor5 ***/
int IR5 = 23; //GPIO23---23 of Nodemcu--IR5
/*** Stepper Motor 2 ***/
int STEP_PIN_2 = 22; //GPIO22---22 of Nodemcu--Step of stepper motor driver
int DIR_PIN_2 = 1; //GPIO1---Tx of Nodemcu--Direction of stepper motor driver
int Sleep_2 = 2; //GPIO2---2 of Nodemcu-Control Sleep Mode on A4988
int MS1_2 = 16; //GPIO16---16 of Nodemcu--MS1 for A4988
int MS2_2 = 4; //GPIO4---4 of Nodemcu--MS2 for A4988
int MS3_2 = 0; //GPIO0---0 of Nodemcu--MS3 for A4988
/*** IRSensor6 ***/
int IR6 = 21; //GPIO21---21 of Nodemcu--IR6
/*** Stepper Motor 3 ***/
int STEP_PIN_3 = 19; //GPIO19---19 of Nodemcu--Step of stepper motor driver
int DIR_PIN_3 = 18; //GPIO18---18 of Nodemcu--Direction of stepper motor driver
int Sleep_3 = 35; //GPIO35---35 of Nodemcu-Control Sleep Mode on A4988
int MS1_3 = 36; //GPIO16---VP of Nodemcu--MS1 for A4988
int MS2_3 = 39; //GPIO4---VN of Nodemcu--MS2 for A4988
int MS3_3 = 34; //GPIO0---34 of Nodemcu--MS3 for A4988
/*** DC Motor ***/
int DCMotor = 5; //GPIO5---5 of Nodemcu--DC Motor
/*** IRSensor7 ***/
int IR7 = 17; //GPIO17---17 of Nodemcu--IR7
/* Motor on till IR detects */
void stepMotor(int dir_pin, int step_pin, int direction, int IR_SENSOR){
int IR_STATUS = 0;
digitalWrite(dir_pin, direction); // Write 1 to the direction pin to make the stepper rotates CW ,and 0 to make it rotates CCW
while(1){
digitalWrite(step_pin, HIGH); // This sends a HIGH signal (logic 1) to the STEP_PIN, instructing the stepper driver to take a step
delayMicroseconds(700); // Waits 500 microseconds (0.5 milliseconds) to ensure the driver registers the step
digitalWrite(step_pin, LOW); // This sends a LOW signal (logic 0) to the STEP_PIN, completing the step pulse
delayMicroseconds(700); // Another 500-microsecond delay before sending the next step pulse, controlling the motor’s speed
IR_STATUS = digitalRead(IR_SENSOR);
if(IR_STATUS == 0){
break;
}
}
}
/* Stepper Motor ON for a period of time */
void StepperON(int dir_pin, int step_pin, int direction, int Tperiod_ms){
int StartTime = millis();
digitalWrite(dir_pin, direction); // Write 1 to the direction pin to make the stepper rotates CW ,and 0 to make it rotates CCW
while(millis() - StartTime < Tperiod_ms){
digitalWrite(step_pin, HIGH); // This sends a HIGH signal (logic 1) to the STEP_PIN, instructing the stepper driver to take a step
delayMicroseconds(700); // Waits 500 microseconds (0.5 milliseconds) to ensure the driver registers the step
digitalWrite(step_pin, LOW); // This sends a LOW signal (logic 0) to the STEP_PIN, completing the step pulse
delayMicroseconds(700); // Another 500-microsecond delay before sending the next step pulse, controlling the motor’s speed
}
}
void setup() {
Serial.begin(115200);
pinMode(IR5, INPUT); //Set IR5 as input
pinMode(DIR_PIN_2, OUTPUT); // Set DIR_PIN_2 as digital OUTPUT
pinMode(STEP_PIN_2, OUTPUT); // Set STEP_PIN_2 as digital OUTPUT
pinMode(IR6, INPUT); //Set IR6 as input
pinMode(DIR_PIN_3, OUTPUT); // Set DIR_PIN_3 as digital OUTPUT
pinMode(STEP_PIN_3, OUTPUT); // Set STEP_PIN_3 as digital OUTPUT
pinMode(DCMotor, OUTPUT); //Set DCMotor as output
pinMode(IR7, INPUT); //Set IR7 as input
}
void loop() {
IR5_status = digitalRead(IR5);
if(IR5_status == 0){
Serial.println("IR5 --> Bottle Detected");
delay(1000);
stepMotor(DIR_PIN_2, STEP_PIN_2, FORWARD, IR6); // Stepper 2 rotates till IR6 detects
digitalWrite(DCMotor, HIGH); // DC Motor ON
StepperON(DIR_PIN_3, STEP_PIN_3, FORWARD, 3000); // Stepper 2 rotates FORWARD for 5 secs
digitalWrite(DCMotor, LOW); // DC Motor OFF
delay(1000);
StepperON(DIR_PIN_3, STEP_PIN_3, REVERSE, 3000); // Stepper 2 rotates REVERSE for 5 secs
stepMotor(DIR_PIN_2, STEP_PIN_2, FORWARD, IR7); // Stepper 2 rotates till IR7 detects
}
delay(10);
}
IRSensor5 _ Bottle Arrived
StepperMotor 2
DC motor
Stepper Motor 3
IR sensor 6 _ Capping
IR sensor 7 _ next step