Using IR sensor to stop Dc motors

Hey guys, I am currently trying to stop my motors using an IR sensor (HW-201) I am using the L6205N motor controller, 2 dc motors, and an Arduino uno. I want the motors to stop when there is a wall in front ( approximately 5cm), but they are not stopping. Any help would be appreciated.

int IN1A= 9;
int IN2A = 8;
int IN1B = 3;
int IN2B = 4;
int EnableA= 10;
int EnableB= 2;
const int IR = A0;

bool val;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600);

pinMode (IN1A, OUTPUT);
pinMode(IN2A, OUTPUT);
pinMode(IN1B, OUTPUT);
pinMode(IN2B, OUTPUT);
pinMode(EnableA, INPUT);
pinMode(EnableB, INPUT);
pinMode(IR, INPUT);

}

void loop() {
// put your main code here, to run repeatedly:
digitalWrite( EnableA, HIGH);
digitalWrite(EnableB, HIGH);
digitalWrite(IN1A, LOW); //backward, right wheel,
digitalWrite(IN2A, 1); //foreward, right wheel
digitalWrite(IN1B, LOW); //backward, left wheel
digitalWrite(IN2B, 1); //foreward, left wheel
digitalWrite(EnableA, 1);
digitalWrite(EnableB, 1);
val =analogRead(IR);

if(val==HIGH){
Serial.println("BLack");
digitalWrite(IN1A, LOW); //backward, right wheel,
digitalWrite(IN2A, LOW); //foreward, right wheel
digitalWrite(IN1B, LOW); //backward, left wheel
digitalWrite(IN2B, LOW);
}
else{
Serial.println("White");
digitalWrite(IN1A, LOW); //backward, right wheel,
digitalWrite(IN2A, 1); //foreward, right wheel
digitalWrite(IN1B, LOW); //backward, left wheel
digitalWrite(IN2B, 1);
digitalWrite(EnableA, 1);
digitalWrite(EnableB, 1);

also I want to slow down the motor speed but it does not seem to be working, can I have any tips on that?

bool val;
...
val =analogRead(IR);

oops

Wait I am kinda new to this as I am a first year student, is there something wrong with this on my end?

Read these:


Always :
Show us a good schematic of your proposed circuit. Show us a good image of your ‘actual’ wiring.
Give links to components.


In the Arduino IDE, use Ctrl T or CMD T to format your code then copy the complete sketch.

Use the </> icon from the ‘reply menu’ to attach the copied sketch.

An analogRead has 1023 ways of providing a Boolean true, and only one of providing a false

so should i be using digitalread instead? as it is only true or false, correct?

Incorrect.
digitalRead returns HIGH or LOW.

It depends on what hardware you have connected to your I/O port named "IR", and how you want to interpret it. So please provide a wiring diagram. Links to the mysterious parts would be useful, too.

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