Please help me with this code. I am working on a project using the following components:
- L298N motor driver
- Water pump (with only positive and negative terminals)
- Arduino IR sensor modules (3 units) for obstacle detection
- TTP223B capacitive touch sensor
- Arduino Uno
- Breadboard
- Arduino ULN2003 motor driver with 28BYJ-48 5V stepper motors
I'm trying to create the following functionality using these sensors:
- Stepper motors 1 and 2 should rotate in clockwise and counterclockwise directions, respectively.
- If infrared sensor 1 detects an object, the stepper motors should stop and water pump 1 should operate for 21 seconds.
- After the water pump stops, the stepper motors should resume operation.
- If infrared sensor 2 detects an object, water pump 2 should operate for 9 seconds.
- After the water pump stops, the stepper motors should resume operation.
- If infrared sensor 3 detects an object, the code should end (reset).
Currently, I have composed the code based on these requirements, but no matter how much I modify it, either the stepper motors rotate, only water pump 1 operates, or only water pump 2 operates. Only one of these three functions seems to work continuously. I'm not sure what part of the code is causing the issue. Could you please let me know what might be wrong with this code?
#define MOTOR_DRIVER_PIN1 7
#define MOTOR_DRIVER_PIN2 6
#define MOTOR_DRIVER_PIN3 5
#define MOTOR_DRIVER_PIN4 4
#define INFRARED_SENSOR1_PIN A2
#define INFRARED_SENSOR2_PIN A1
#define INFRARED_SENSOR3_PIN A3
#define STEPPER_MOTOR1_PIN1 10
#define STEPPER_MOTOR1_PIN2 11
#define STEPPER_MOTOR1_PIN3 12
#define STEPPER_MOTOR1_PIN4 13
#define STEPPER_MOTOR2_PIN1 2
#define STEPPER_MOTOR2_PIN2 8
#define STEPPER_MOTOR2_PIN3 3
#define STEPPER_MOTOR2_PIN4 9
#define WATER_PUMP_PIN 14
#define PUMP_ON_TIME_SENSOR1 21000
#define PUMP_ON_TIME_SENSOR2 9000
int motorPattern[8][4] = {
{1, 0, 0, 1},
{1, 0, 0, 0},
{1, 1, 0, 0},
{0, 1, 0, 0},
{0, 1, 1, 0},
{0, 0, 1, 0},
{0, 0, 1, 1},
{0, 0, 0, 1}
};
int motorIndex = 0;
unsigned long previousMillisMotor = 0;
unsigned long previousMillisPump1 = 0;
unsigned long previousMillisPump2 = 0;
bool pump1State = false;
bool pump2State = false;
void setup() {
pinMode(MOTOR_DRIVER_PIN1, OUTPUT);
pinMode(MOTOR_DRIVER_PIN2, OUTPUT);
pinMode(MOTOR_DRIVER_PIN3, OUTPUT);
pinMode(MOTOR_DRIVER_PIN4, OUTPUT);
pinMode(STEPPER_MOTOR1_PIN1, OUTPUT);
pinMode(STEPPER_MOTOR1_PIN2, OUTPUT);
pinMode(STEPPER_MOTOR1_PIN3, OUTPUT);
pinMode(STEPPER_MOTOR1_PIN4, OUTPUT);
pinMode(STEPPER_MOTOR2_PIN1, OUTPUT);
pinMode(STEPPER_MOTOR2_PIN2, OUTPUT);
pinMode(STEPPER_MOTOR2_PIN3, OUTPUT);
pinMode(STEPPER_MOTOR2_PIN4, OUTPUT);
pinMode(INFRARED_SENSOR1_PIN, INPUT);
pinMode(INFRARED_SENSOR2_PIN, INPUT);
pinMode(INFRARED_SENSOR3_PIN, INPUT);
pinMode(WATER_PUMP_PIN, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis();
if(currentMillis - previousMillisMotor >= 1) {
previousMillisMotor = currentMillis;
runStepperMotor(STEPPER_MOTOR1_PIN1, STEPPER_MOTOR1_PIN2, STEPPER_MOTOR1_PIN3, STEPPER_MOTOR1_PIN4);
runStepperMotor(STEPPER_MOTOR2_PIN1, STEPPER_MOTOR2_PIN2, STEPPER_MOTOR2_PIN3, STEPPER_MOTOR2_PIN4);
}
if (digitalRead(INFRARED_SENSOR1_PIN) == HIGH && !pump1State) {
pump1State = true;
previousMillisPump1 = currentMillis;
digitalWrite(WATER_PUMP_PIN, HIGH);
}
if (digitalRead(INFRARED_SENSOR2_PIN) == HIGH && !pump2State) {
pump2State = true;
previousMillisPump2 = currentMillis;
digitalWrite(WATER_PUMP_PIN, HIGH);
}
if (pump1State && currentMillis - previousMillisPump1 >= PUMP_ON_TIME_SENSOR1) {
pump1State = false;
digitalWrite(WATER_PUMP_PIN, LOW);
}
if (pump2State && currentMillis - previousMillisPump2 >= PUMP_ON_TIME_SENSOR2) {
pump2State = false;
digitalWrite(WATER_PUMP_PIN, LOW);
}
if (digitalRead(INFRARED_SENSOR3_PIN) == HIGH) {
resetAll();
}
}
void runStepperMotor(int pin1, int pin2, int pin3, int pin4) {
digitalWrite(pin1, motorPattern[motorIndex][0]);
digitalWrite(pin2, motorPattern[motorIndex][1]);
digitalWrite(pin3, motorPattern[motorIndex][2]);
digitalWrite(pin4, motorPattern[motorIndex][3]);
motorIndex = (motorIndex + 1) % 8;
}
void resetAll() {
digitalWrite(MOTOR_DRIVER_PIN1, LOW);
digitalWrite(MOTOR_DRIVER_PIN2, LOW);
digitalWrite(MOTOR_DRIVER_PIN3, LOW);
digitalWrite(MOTOR_DRIVER_PIN4, LOW);
digitalWrite(STEPPER_MOTOR1_PIN1, LOW);
digitalWrite(STEPPER_MOTOR1_PIN2, LOW);
digitalWrite(STEPPER_MOTOR1_PIN3, LOW);
digitalWrite(STEPPER_MOTOR1_PIN4, LOW);
digitalWrite(STEPPER_MOTOR2_PIN1, LOW);
digitalWrite(STEPPER_MOTOR2_PIN2, LOW);
digitalWrite(STEPPER_MOTOR2_PIN3, LOW);
digitalWrite(STEPPER_MOTOR2_PIN4, LOW);
digitalWrite(WATER_PUMP_PIN, LOW);
motorIndex = 0;
}
If you could help me, I would truly appreciate it. I've spent countless nights trying to solve this but haven't been successful. Thank you in advance.