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);
}
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
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.