sbcshop / MotorShield

Designed to help you get your Raspberry Pi based Robot. This board can control DC motor as well as the stepper motor. You can connect your IR and Ultrasonic sensors to tell your robot about its environment. You can easily make your Line Following, Object following, Wall following, Maze-Solver Robots.
https://shop.sb-components.co.uk/products/motorshield-for-raspberry-pi
52 stars 47 forks source link

Using IR Sensors #10

Open ehansi opened 5 years ago

ehansi commented 5 years ago

Hello,

I bought an analog IR distance sensor (https://www.robotshop.com/en/sharp-gp2y0a21yk0f-ir-range-sensor.html) and wanted to test it on the motor-shield. I've connected VCC to +, GND to -, and V0 to OP on the board. Running the provided test program there seems to be no reaction, IR is never triggered. Tested on both IR connectors. I'm pretty new to all of this so I guess I'm missing something very basic. Thanks for any help, people!

sgarret3 commented 5 years ago

My problems were because something is backwards here for the IR sensors only. When I use this example code:

-------------------------------

from PiMotor import Sensor ir1 = Sensor ("IR1",0) ir1.trigger() if ir1.Triggered: print("Obstruction Detected")

-------------------------------

This code would only enter the IF statement when there was NOT an obstruction, which was causing me misery when I kept putting something in front of the sensor, and it would never enter the IF statement. I had to rewrite the code to follow this form:

-------------------------------

from PiMotor import Sensor ir1 = Sensor ("IR1",0) ir1.trigger() if ir1.Triggered: print("Nothing Detected")

keep going on like normal

else: print("Obstruction Detected")

code for when IR sees an obstruction

-------------------------------

PiMotor.py has an IR pin voltage backwards (checking for pin voltage up when they mean down or vice versa) but I don't know how to fix it. This also applies to IR2 input.

If you just want a piece of code as an example. Here is a very unfinished, untested, (and poorly written) chunk of something I am preparing for some kids to demonstrate a little tank navigating a simple maze. You might want to take a look if you want to see the indents, since this editor is not saving my indentations above which Python requires.

Tank.txt

mc03225 commented 5 years ago

Hi, Take a backup of PiMotor.py, then change line 233 if input_state != True: (it was set to equals ==) Now try while True: if ir1.iRcheck(): print("Detected") else: print("Not Detected")

I hope this helps?