This Arduino compatible library has been written specifically for the Hobby Components Obstacle Avoidance Robot Car Kit (HCKITS0060). It is used the demo sketch shipped with the kit and also provides an easy means of controlling the robot car using your own sketches. The library includes functions for controlling the robot cars motors and for taking reading from the ultrasonic sensor.
You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.
On Windows:
My Documents\Arduino\libraries\
On Mac:
Documents/Arduino/libraries/
Linux:
Usually found within the users home area under /Arduino/libraries/
Using the HCOARC library
To use the library just include the HCOARC.h header file and then create an instance of the library. E.g:
- #include "HCOARC.h"
- HCOARC HCOARC;
To initialise the library place the following line in the Setup() loop at the top of the sketch:
- HCMotor.Init();
The following functions are available with this library:
- void Attach(MotorNum, PinA, PinB);
MotorNum is a value used to reference which motor you wish to attach. Valid values are:
LEFT_MOTOR - Attach the left motor (motor 0)
RIGHT_MOTOR - Attach the right motor (motor 1)
PinA is the digital pin number connected to INA of the H-Bride module.
PinB is the digital pin number connected to INB of the H-Bride module.
- void Detach(MotorNum);
MotorNum the motor you wish to detach. Valid values are:
LEFT_MOTOR - Detach the left motor (motor 0)
RIGHT_MOTOR - Detach the right motor (motor 1)
- void Speed(MotorNum, Percent);
MotorNum is the motor you wish to change the speed for. Valid values are:
LEFT_MOTOR - Change the speed for the left motor
RIGHT_MOTOR - Change the speed for the right motor
Percent is the motor speed specified in percent where 0 - full stop and 100 is full speed.
- void Direction(MotorNum, Direction);
MotorNum is the motor you wish to change the speed for. Valid values are:
LEFT_MOTOR - Change the direction for the left motor
RIGHT_MOTOR - Change the direction for the right motor
Direction is the rotational direction. Valida values are
FORWARD - Motor will rotate in the forward direction.
REVERSE - Motor will rotate in the reverse direction.
- void Ultrasonic_Init(Echo, Trig);
Echo is the digital pin number connected to the Echo pin on the sensor.
Trig is the digital pin number connected to the Trig pin on the sensor.
- void Ultrasonic_Init(Echo, Trig);
Echo is the digital pin number connected to the Echo pin on the sensor.
Trig is the digital pin number connected to the Trig pin on the sensor.
- void Ultrasonic_Init(Power, Gnd, Echo, Trig);
Power is the digital pin number connected to the Power pin on the sensor.
Gnd is the digital pin number connected to the Gnd pin on the sensor.
Echo is the digital pin number connected to the Echo pin on the sensor.
Trig is the digital pin number connected to the Trig pin on the sensor.
- volatile uint8_t Ultrasonic_Raw(void);
100us ticks as type Byte,
- /* FILE: OARC_Example.h
- DATE: 13/09/18
- VERSION: 0.1
- AUTHOR: Andrew Davies
- 13/09/18 version 0.1: Original version
- This is the demo sketch which is pre-flashed into the Nano shipped in the
- Hobby Components Obsicle Avoidance Robot Car Kit (HCKITS0060). The sketch is
- provided as an example of how to use the HCOARC library created specifically for this
- kit. You can find more information about this library on our support forum here:
- HTTPS://HOBBYCOMPONENTS.COM/OARC
- You may copy, alter and reuse this code in any way you like, but please leave
- reference to HobbyComponents.com in your comments if you redistribute this code.
- This software may not be used directly for the purpose of selling products that
- directly compete with Hobby Components Ltd's own range of products.
- THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
- EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
- HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
- INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
- REASON WHATSOEVER.
- */
- #include "HCOARC.h"
- HCOARC HCOARC;
- #define LED_PIN 13
- void setup()
- {
- HCOARC.Init();
- HCOARC.Ultrasonic_Init(A2, A5, A4, A3);
- HCOARC.Attach(LEFT_MOTOR, 2, 3);
- HCOARC.Attach(RIGHT_MOTOR, 4, 5);
- pinMode(LED_PIN, OUTPUT);
- for(byte i = 0; i < 5; i++)
- {
- delay(500);
- digitalWrite(LED_PIN, HIGH);
- delay(500);
- digitalWrite(LED_PIN, LOW);
- }
- HCOARC.Speed(LEFT_MOTOR, 100);
- HCOARC.Speed(RIGHT_MOTOR, 100);
- }
- void loop()
- {
- byte Distance = HCOARC.Ultrasonic_Raw();
- if(Distance >= 25)
- {
- HCOARC.Direction(LEFT_MOTOR, FORWARD);
- HCOARC.Direction(RIGHT_MOTOR, FORWARD);
- }else
- {
- digitalWrite(LED_PIN, HIGH);
- HCOARC.Direction(LEFT_MOTOR, REVERSE);
- HCOARC.Direction(RIGHT_MOTOR, FORWARD);
- delay(100);
- digitalWrite(LED_PIN, LOW);
- }
- }
Disclaimer: Libraries, example code, and diagrams are provided as an additional free service by Hobby Components and are not sold as part of this product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.
Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.