IR Sensor operation

I am using Arduino UNO R3
I want to achive following code. Pl. help.
I expect the motor to start even though sensor is blocked. Any suggestions?

Is noted that when sensor is NOT BLOCKED it's output is HIGH and when blocked it is LOW!

if (digitalRead(IR_sensor1) == LOW)// Check the state of the IR sensor@ Arduino pin
{
digitalWrite(motor_pin1, LOW); // Stop the motor
digitalWrite(motor_pin2, LOW);
delay(5000);
digitalWrite(motor_pin1, HIGH); // Restart the motor
digitalWrite(motor_pin2, LOW);
}

Schematic? The use of code tags?

No clue how pins 1 & 2 are connected but you at no point make pin 2 high. Where is the rest of your code?

Ron

Here is the complete sketch I am trying to execute, I hv placed 2 LEDs in place of motor_pin 1 & 2 and checking for motor_pin1 only...... it only flashes once every after delay.....It is expcted that it should stay ON there untill the sensor is blocked,,,then re start after delay................is it toooooo much?
SKETCH->
int IRsensor = 2; // Define the pin for the IR sensor

int motor_pin1 = 9; // Define the pins for the motor
int motor_pin2 = 10;

void setup()
{
pinMode(IRsensor, INPUT); // Set the IR sensor pin as an input

pinMode(motor_pin1, OUTPUT); // Set the motor pins as outputs
pinMode(motor_pin2, OUTPUT);
}

void loop()
{
if (digitalRead(IRsensor) == HIGH) // Check the state of the IR sensor (Sensor not Blocked)
{

digitalWrite(motor_pin1, HIGH); //motor starts in one direction 
digitalWrite(motor_pin2, LOW); 

}
if (digitalRead(IRsensor) == LOW) // Check the state of the IR sensor (Sensor Blocked)
{

digitalWrite(motor_pin1, LOW); //motor stopped 
digitalWrite(motor_pin2, LOW); 
delay(5000);    //motor will re-start after delay
digitalWrite(motor_pin1, HIGH); // Run the motor in same dirction as above
digitalWrite(motor_pin2, LOW);  //  
delay(1500);// delay for re starting the loop

}

} //void loop ends

Thanks in adv.

post your code with code tags, it's a headache to read it like this.
And post a link to your ir-sensor.

1 Like

How do you mean with "tags"? Do you mean he should make it more readable?

@idrab Please read this

1 Like

Please read this:

Code Tags
Click on the < CODE/ > button, then paste your code where the highlighted 'type or paste code here ' is.

The code will then be easier to read and copy into the IDE.

Noted. Thanks.

To do this I think you need to understand the StateChange example and record the time when the sensor first becomes blocked.

Then, using the state change concepts you could rewrite your code to:

  1. turn on the motor when it becomes unblocked
  2. turn off the motor when it becomes blocked (and record the time)
  3. turn on the motor when it has been blocked for 5000ms

You don't need to re- turn on the motor if it is already running every time you go through loop.

This is your code properly formatted and using code tags:

int IRsensor = 2;  // Define the pin for the IR sensor

int motor_pin1 = 9;  // Define the pins for the motor
int motor_pin2 = 10;

void setup() {
  pinMode(IRsensor, INPUT);  // Set the IR sensor pin as an input

  pinMode(motor_pin1, OUTPUT);  // Set the motor pins as outputs
  pinMode(motor_pin2, OUTPUT);
}

void loop() {
  if (digitalRead(IRsensor) == HIGH)  // Check the state of the IR sensor (Sensor not Blocked)
  {

    digitalWrite(motor_pin1, HIGH);  //motor starts in one direction
    digitalWrite(motor_pin2, LOW);
  }
  if (digitalRead(IRsensor) == LOW)  // Check the state of the IR sensor (Sensor Blocked)
  {

    digitalWrite(motor_pin1, LOW);  //motor stopped
    digitalWrite(motor_pin2, LOW);
    delay(5000);                     //motor will re-start after delay
    digitalWrite(motor_pin1, HIGH);  // Run the motor in same dirction as above
    digitalWrite(motor_pin2, LOW);   //
    delay(1500);                     // delay for re starting the loop
  }

}  //void loop ends

I pointed out earlier that at no point in your code do you change your motor pin 2? I am asking for the last time?
I did load your code on an Arduino Uno and as to the code it runs just fine. Making pin 2 High drives pin 9 High and making pin 2 low drives pin 9 low.

I also asked for a schematic? Guess not so if you can't follow forum guidance my time here is done. Have a nice day.

Ron

1 Like

Fix it.

Schematic for IR Sensor / Arduino

(attachments)

What motor is on pins 9 and 10? Where is power for the motor coming from?

Obviously from pins 9 and 10. That's why they are set low and high in the code. :sweat_smile:
Let's hope the motor looks like this:

1 Like