go_away

Author Topic: Sharp IR Sensor with arduino  (Read 589 times)

0 Members and 1 Guest are viewing this topic.

Offline CheetofingersTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Sharp IR Sensor with arduino
« on: April 19, 2014, 01:23:54 PM »
Hello, I am having trouble with the IR sensor.

I am trying to make a robot that runs 2 dc motors and 2 IR sensor with arduino.  I am using a breadboard, arduino uno, 2dc motor, 2 sharp infrared sensor (says 2Y0A21 for model?) and an h bridge chip.  The way I want to have it set up is the pin connected to the IR sensor will tell the motors to start or stop based on distance from the IR sensor. A chart i read online says the IR sensor is supposed to return a voltage to the pin based on the distance it reads. (around 3v max at 15cm )
http://3.bp.blogspot.com/-jd2TQQlOo1Q/TzKvUP5uE-I/AAAAAAAAAZ8/o_TNffud7_g/s1600/Slika04.jpg
 
When i first upload my code and have the sensor pointed in a direction, the motors will run based on how far the sensor is from something.  Right now it will only run when something is from 10cm -40cm (2V).  However they only work correctly RIGHT AFTER i upload my code to the arduino.  After that the motors will stay spinning or stay stopped.  Shouldn't the void loop just keep looping and get new values though? Or do I need to write another loop to constantly acquire new voltage values for the pin for when the distance changes? Any ideas?

Here is the code.
const int switchPin = 2;    // switch input
const int motor1Pin = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor2Pin = 4;    // H-bridge leg 2 (pin 7, 2A)
const int MOTOR1PIN = 5;
const int MOTOR2PIN = 6;
const int enablePin = 9;    // H-bridge enable pin
const int ENABLEPIN = 10;
int sensorpin = 0;
double value;
 void setup()
 {

    // set all the other pins you're using as outputs:
    pinMode(MOTOR1PIN, OUTPUT);
    pinMode(MOTOR2PIN, OUTPUT);
    pinMode(motor1Pin, OUTPUT);
    pinMode(motor2Pin, OUTPUT);
    pinMode(enablePin, OUTPUT);
    pinMode(ENABLEPIN, OUTPUT);

    // set enablePin high so that motor can turn on:
    digitalWrite(enablePin, HIGH);
    digitalWrite(ENABLEPIN, HIGH);
  }
  void loop()
  {
    value = analogRead(sensorpin);
    // if the sensor pin is greater than 2Volts
    if (value >2) {
      digitalWrite(motor1Pin, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor2Pin, HIGH);  // set leg 2 of the H-bridge high
      digitalWrite(MOTOR1PIN, LOW);
      digitalWrite(MOTOR2PIN, HIGH);
    }
    else
    {
      digitalWrite(enablePin, LOW);
      digitalWrite(ENABLEPIN, LOW
    }
  }
« Last Edit: April 19, 2014, 01:28:55 PM by Cheetofingers »

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Sharp IR Sensor with arduino
« Reply #1 on: April 19, 2014, 02:40:58 PM »
Your loop() code never turns the enable pin high again if the condition changes.

You want the loop to do something like:
"if the read value is within the range"
" then enable, and set direction"
" else disable"

Also, using different case but otherwise the same name for variables with different meaning leads to hard-to-reason-about code. Much better to call it "enablePin1" and "enablePin2" for example.

Offline CheetofingersTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Sharp IR Sensor with arduino
« Reply #2 on: April 19, 2014, 07:43:31 PM »
oh wow i see now, i set the voltage to high in the setup, so it doesn't know to return to that voltage in loop function. So obvious.

going to try it soon, thanks!



Offline CheetofingersTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Sharp IR Sensor with arduino
« Reply #3 on: April 20, 2014, 01:01:15 PM »
okay, maybe there is something wrong with my hardware now. I think the code should be good....

It is not reading the voltage like it should be.  when I have the code to set the motors to run when it reads above 2 voltage it always runs even when i have my IR sensor pointed far away where the voltage should be less than 2. 

Here is what the code looks like now, made it easier to read too.

const int motor1Pin1 = 3;    // H-bridge leg 1 (pin 2, 1A)
const int motor1Pin2 = 4;    // H-bridge leg 2 (pin 7, 2A)
const int motor2Pin1 = 5;
const int motor2Pin2 = 6;
const int enablePin1 = 9;    // H-bridge enable pin
const int enablePin2 = 10;
int sensorpin = 0;
double value;
void setup()
 {
    pinMode(motor1Pin1, OUTPUT);
    pinMode(motor1Pin2, OUTPUT);
    pinMode(motor2Pin1, OUTPUT);
    pinMode(motor2Pin2, OUTPUT);
    pinMode(enablePin1, OUTPUT);
    pinMode(enablePin2, OUTPUT);
  }
  void loop()
  {
    double value;
    value1 = analogRead(sensorpin);
    if (value > 2)
      {
      digitalWrite(enablePin1, HIGH);
      digitalWrite(enablePin2, HIGH);
      digitalWrite(motor1Pin1, LOW);   // set leg 1 of the H-bridge low
      digitalWrite(motor1Pin2, HIGH);  // set leg 2 of the H-bridge high
      digitalWrite(motor2Pin1, LOW);
      digitalWrite(motor2Pin2, HIGH);
    }
    else
    {
      digitalWrite(enablePin1, LOW);
      digitalWrite(enablePin2, LOW);
    }
 }

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Sharp IR Sensor with arduino
« Reply #4 on: April 20, 2014, 06:49:15 PM »
value will be between 0 and 1023 for the range of 0..5V. A value of 2V is approximately 410. You are testing against "2".

Offline CheetofingersTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Sharp IR Sensor with arduino
« Reply #5 on: April 21, 2014, 10:21:05 AM »
??? really? I thought the arduino reads the pin voltage?

I read something where someone uses an algorithm with a power function that puts the value between 0 to 1023 but I didn't use that?

Anyways I will test the sensor with those values when i can.

Offline jkerns

  • Robot Overlord
  • ****
  • Posts: 251
  • Helpful? 12
Re: Sharp IR Sensor with arduino
« Reply #6 on: April 21, 2014, 01:22:18 PM »
" value1 = analogRead(sensorpin);
    if (value > 2) "
Are you using to different variable - one for the A/D and a different one for the if?


And, what jwatte said.  http://arduino.cc/en/Reference/analogRead
« Last Edit: April 21, 2014, 01:24:19 PM by jkerns »
I get paid to play with robots - can't beat that with a stick.

http://www.ltu.edu/engineering/mechanical/bachelor-science-robotics-engineering.asp

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,343
  • Helpful? 81
Re: Sharp IR Sensor with arduino
« Reply #7 on: April 21, 2014, 03:55:48 PM »
??? really? I thought the arduino reads the pin voltage?


The documentation is pretty clear: http://arduino.cc/en/Reference/analogRead

Offline CheetofingersTopic starter

  • Jr. Member
  • **
  • Posts: 9
  • Helpful? 0
Re: Sharp IR Sensor with arduino
« Reply #8 on: April 21, 2014, 07:58:05 PM »
Ah didn't know about that page. so the int value is based on a data conversion from the voltage.

Thanks fellas

 


Get Your Ad Here

data_list