B-328 with Sharp IR sensors code

I am working on a robot using the B-328 as my base. My robot must go ten feet and pass through a tunnel for a competition. I have two dc motors linked together in parallel working off of one of the motor controllers. I am programming with the Pololu USB AVR programmer. I am trying to use two hobby servos also linked together with a Y harness/splitter to run off of the second motor controller. I also want to initials one of two possible outputs (LED) based on the input of the sensors. I am going to have two IR sensors mounted on the L/R of my chassis. Once the left sensor detects and object, I would like my servos to actuate and the appropriate LED to light up until the object is no longer detected within the sensors range. At which point I would like to turn off the LED and retract the servo to the original position. I need a code that will run this sequence of events: analyzing sensor response, activate correct LED/servo, hold, retract servo/LED after sensors are “clear”. Can someone nudge me in the correct direction where I could possibly find the resources needed to compile this code?

Thanks in advance. If you couldn’t tell, I’m a total beginner.


You might want to try playing with our example programs first. There is an example of controlling a motor in there. Generally, we recommend you start with the simple examples and build on them.

- Jeremy

Ok so I’m right now setting up my two basic motors for simple driving of my robot. I am using the B-328 with the Pololu USB AVR programmer. I know how to use this code:

I got this code from the Pololu AVR library command reference. With this code, I understand how to drive my motors forward or backwards with a variable speed. What I do not know is how to autonomously switch my motor settings. Say I want to start with motor 1 at 255 and motor 2 at -255. This much I understand. Now what code would I use to make motor 1 switch to -255 and motor 2 to 255 after 2 seconds? I have scavenged the command reference but could not find that part of code. Thanks in advance.

To change the speed of the motors, you can call the function set_motors() again with the speeds you want.

- Jeremy

How would I do that after a certain period of time. Wouldn’t I need another line of code to say change speed after 2 seconds?

You can use the function delay_ms() between the set_motors() calls. Many of our example programs do things based on timing (including the blink LED example). You should go through those examples and familiarize yourself with how they work.

- Jeremy

First off, I’d like to thank you Jeremy for still replying to my stupidly basic posts. I really do appreciate all the help that you are giving a compete beginner.

So, I have read through all the examples that pertain to the B-328 and I think I have gotten my basic code for driving my motors.

void set_motors(255, 255); delay_ms(2000); void set_motors(0, 0); delay_ms(100); void set_motors(-255, -255); delay_ms(2000);
This code will run both motors at full speed for 2 seconds. Then both motors will stop spinning completely momentarily, and then both will spin in the opposite direction for two seconds, I think (and hope!).

So, my question now is pertaining to the sensors. Using these sensors,https://www.pololu.com/catalog/product/1134, I would like to run motors. From my understanding, these use PWM. So, using the Pololu AVR library reference, pulse_in_start(const unsigned char pulse_pins[], unsigned char num_pins) I have to use this code to receive the sensor’s readings. Should the sensor reading return high, meaning that the sensor has detected an object, I would have to utilize this code void set_digital_output(unsigned char pin, unsigned char output_state); placing high on output_state to turn on an led on the appropriate pin. It is my understanding that driving the pin high will give it power thus lighting the led. Now, assuming that all the above stated code is right and does what I want it to do, I want to know how to write and if-then statement. So that ONLY when I get a high PWM reading off of the sensor do I drive the appropriate pin high thus turning on the LED. Thanks for all the help again in reviewing my code and helping me further that code.

That sharp sensor does not use PWM. Instead, the sensor outputs a digital signal that goes low when detecting an object. By design, the LED on the sensor will turn on when it detects an object. You might try specifying a pin as an digital input using the function set_digital_input() and call is_digital_input_high() to read the input value of that pin. Using an if statement with is_digital_input_high() will allow you to do some operation based on whether or not an object is detected.

- Jeremy