Need help with code please

hi all i am not sure is this correct category
need help with my code behaving strange!?
everything works good but :frowning_face:
i need motor to stop if sensor does not detect object in 2 seconds
and it is not stopping
// Pins
const int IR_SENSOR_PIN = 2;
const int MOTOR_PIN = 9;
const int BUTTON_PIN = 7;
const int COUNTER_PIN = 13; // Digital counter output pin

// Variables
int sensorState = LOW;
int lastSensorState = LOW;
unsigned long sensorStartTime = 0;
const unsigned long SENSOR_TIMEOUT = 2000;
bool sequenceStarted = true;
int counter = 0; // Counter variable

void setup() {
// Initialize the pins
pinMode(IR_SENSOR_PIN, INPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(MOTOR_PIN, OUTPUT);
pinMode(COUNTER_PIN, OUTPUT);
digitalWrite(MOTOR_PIN, LOW); // Start with the motor off

// Set the initial sensor state
sensorState = digitalRead(IR_SENSOR_PIN);
lastSensorState = sensorState;
}

void loop() {
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == LOW) {
sequenceStarted = false;
digitalWrite(MOTOR_PIN, LOW); // Turn off the motor
delay(100); // Stop the motor for 2 seconds
digitalWrite(MOTOR_PIN, HIGH); // Turn on the motor again
}

// Check the IR sensor state
sensorState = digitalRead(IR_SENSOR_PIN);

// If the sensor state has changed
if (sensorState != lastSensorState) {
// If the sensor is low
if (sensorState == LOW) {
// Start the sequence
sequenceStarted = true;
sensorStartTime = millis();
delay(100); // Record the start time
digitalWrite(MOTOR_PIN, HIGH); // Turn on the motor
counter++; // Increment the counter
digitalWrite(COUNTER_PIN, counter % 2); // Output the counter value to the digital pin
} else {
// Sensor is high, stop the motor
sequenceStarted = false;
digitalWrite(MOTOR_PIN, LOW); // Turn off the motor
}
}

// If the sensor is continuously low for 5 seconds
if (sensorState == LOW && sequenceStarted && millis() - sensorStartTime >= SENSOR_TIMEOUT) {
// Stop the motor
sequenceStarted = true;
digitalWrite(MOTOR_PIN, HIGH); // Turn off the motor
}

// Update the last sensor state
lastSensorState = sensorState;
}

please for support
i am solving this and it just not working for me :slight_smile: :rage:
thank you in advance :slight_smile:

if (sensorState == LOW && sequenceStarted && millis() - sensorStartTime >= SENSOR_TIMEOUT) {
// Stop the motor
sequenceStarted = true;
digitalWrite(MOTOR_PIN, HIGH); // Turn off the motor
}

// Update the last sensor state
lastSensorState = sensorState;
}

i hope this the part you want to check here in stopping section you set motor pin to high just turn the pin to low

if (sensorState == LOW && sequenceStarted && millis() - sensorStartTime >= SENSOR_TIMEOUT) {
// Stop the motor
sequenceStarted = true;
digitalWrite(MOTOR_PIN, LOW); // Turn off the motor
}

// Update the last sensor state
lastSensorState = sensorState;
}

and also when you present code please use <code/> when you type and place the code part in it .it will help others to look up the code faster

1 Like

HI when i set this value to LOW motor does not stop also.
but when sensor detect object and motor stop is starts again after 2 seconds even if object is still present at sensor.
i was thinking wiring was problem.
i am not sure anymore
thank you

i think the IR sensor you are using will return LOW when an object is placed near it can you check the output of the IR sensor when object is placed infront of it

1 Like

Yes you are right seems sensor is working in the opposite way.

if (sensorState == HIGH && sequenceStarted && millis() - sensorStartTime >= SENSOR_TIMEOUT) {
// Stop the motor
sequenceStarted = true;
digitalWrite(MOTOR_PIN, LOW); // Turn off the motor
}

// Update the last sensor state
lastSensorState = sensorState;
}

Then try this i hope it will help

1 Like

thank you
i was not checking sensor because it has led, i was thinking led on is sensor high but no...
thank you for support and quick response :handshake:

1 Like

This is the normal way for the IR sensor (this is often used on Line Tracking robots).

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.