-
Notifications
You must be signed in to change notification settings - Fork 15
Example sketches
Here some typical cases of the library usage will be demonstrated. Feel free to use and adjust them at your own will.
In this example, we are demonstrating a very simple and common case, where the user wants to manually control the vehicle over a Serial connection. For simplicity reasons, the car will only abide by 5 commands (go straight, go back, turn left, turn right, stop) which will be one character long.
#include <AndroidCar.h>
#include <Servo.h>
#include <Wire.h>
const unsigned short ESC_PIN = 10; //the pin the esc will be connected to
const unsigned short SERVO_PIN = 11; //the pin the steering servo will be connected to
Car car(SERVO_PIN, ESC_PIN); //declare the car using the above pins
void setup() {
car.begin(); //initialize the car
Serial.begin(9600); //initialize serial input
}
void loop() {
handleInput();
}
void handleInput() { //handle serial input if there is any
if (Serial.available()) { //if there's something available in the serial port
char inChar = (char)Serial.read(); //read it
switch (inChar) {
case 'f':
car.setSpeed(100); //start moving the wheels forward
car.setAngle(0); //straighten the steering wheel
break;
case 'b':
car.setSpeed(-100); //start moving the wheels backward
car.setAngle(0); //straighten the steering wheel
break;
case 'l':
car.setAngle(-25); //put the steering wheel left
break;
case 'r':
car.setAngle(25); //put the steering wheel right
break;
default:
car.setSpeed(0); //stop the motors
car.setAngle(0); //straighten the steering wheel
break;
}
}
}
In this example, we collect data from two different ultra sound sensors that operate simultaneously and display the results of their measurements in centimeters, via the Serial port.
The sensors are instantiated before the setup()
function, then inside the setup()
they are attached to their respective pins and finally in the loop()
we collect their data every 100 milliseconds.
#include <AndroidCar.h>
#include <Servo.h>
#include <Wire.h>
Sonar frontSonar, backSonar; //initialize two ultra sound sensors
const int FRONT_TRIG_PIN = 38; //sensor's trig pin
const int FRONT_ECHO_PIN = 39; //sensor's echo pin
const int BACK_TRIG_PIN = 40;
const int BACK_ECHO_PIN = 41;
void setup() {
frontSonar.attach(FRONT_TRIG_PIN, FRONT_ECHO_PIN); //initialize the sensor with attach(trigger pin, echo pin)
backSonar.attach(BACK_TRIG_PIN, BACK_ECHO_PIN);
Serial.begin(9600); //start the serial
}
void loop() {
delay(100); //run the following every 100ms
Serial.print("Front: ");
Serial.print(frontSonar.getDistance()); //print the distance in centimeters
Serial.print(" Back: ");
Serial.println(backSonar.getDistance());
}
In this example, we collect data from an SHARP infra red sensor and display the results of the measurements in centimeters, via the Serial port.
The sensor is instantiated before the setup()
function, then inside the setup()
it is attached to its respective analog pin and finally in the loop()
we collect its data every 100 milliseconds.
#include <AndroidCar.h>
#include <Servo.h>
#include <Wire.h>
Sharp_IR frontIR; //instantiate the infra red sensor
const int IR_pin = A5; //set the pin that the infra red sensor will be providing data to
void setup() {
frontIR.attach(IR_pin); //attach (initialize) the sensor at the appropriate pin
Serial.begin(9600); //start the serial
}
void loop() {
delay(100); //run the following every 100 ms
Serial.println(frontIR.getDistance()); //print the distance in cm
}
In this example, we want to measure the traveled distance for the first 20 seconds (or 20 thousand milliseconds), as defined in the timeOver
variable. We begin by instantiating the Odometer, which we name encoder
. Then, in the setup()
function we attach it to the encoderPin
and initiate a measurement with begin()
. Additionally, we log down when that happened using millis()
. Next, in the loop()
we check if timeOver
seconds have passed since and if so, we detach()
the encoder
. If everything goes as planned, we the traveled distance will be printed every 200 milliseconds for the first timeOver
seconds, until the encoder
is detached. After that, we will keep getting 0
as the traveled distance.
#include <AndroidCar.h>
#include <Servo.h>
#include <Wire.h>
Odometer encoder;
unsigned long startTime;
unsigned int timeOver = 20000; //20 seconds
const int encoderPin = 19; //digital pin 19 (interrupt 4 on arduino mega)
void setup() {
Serial.begin(9600);
encoder.attach(encoderPin);
encoder.begin();
startTime = millis();
}
void loop() {
if (millis()-startTime>timeOver){ //count until timeOver seconds and then detach the encoder
encoder.detach();
}
delay(200);
Serial.println(encoder.getDistance());
}
In this example we want to to measure the angular displacement every 100 milliseconds and print it through the Serial port. We start by instantiating the gyroscope in a variable called gyro
. Then, in setup()
, we initiate the serial port as well as the gyroscope with gyro.attach()
. Next, we give it some time to get ready and begin the measurements with gyro.begin()
. Finally, in the loop function we print out the angular displacement of that moment with gyro.getAngularDisplacement()
every 100 milliseconds.
#include <AndroidCar.h>
#include <Servo.h>
#include <Wire.h>
Gyroscope gyro; //instantiate the gyroscope
unsigned long prevTime = 0;
const unsigned short INTERVAL = 100;
void setup() {
Serial.begin(9600);
gyro.attach(); //initialize the gyroscope
delay(1500); //wait to make sure it's ready
Serial.println("Start measuring!");
gyro.begin(); //start measuring NOW
}
void loop() {
gyro.update();
if (millis() - prevTime > INTERVAL){ //do this every INTERVAL
Serial.println(gyro.getAngularDisplacement()); //print angular displacement since last begin()
prevTime = millis();
}
}
A typical use of the Razorboard, would be to get the latest yaw value. This can be used to determine how much the car has "turned" in degrees. In the following sketch, we will determine how much the user has rotated the Razorboard in the yaw axis, from the initial position, within 5 seconds.
#include <AndroidCar.h>
#include <Servo.h>
#include <Wire.h>
Razorboard razor;
int initialHeading = 0;
unsigned long measurementTime = 0;
const unsigned short PRINT_OUT_TIME = 5000;
boolean printed = false; //variable to make sure we'll print only once
void setup() {
razor.attach(&Serial2); //attach the gyroscope to Serial2
Serial.begin(9600);
delay(2000); //wait for the razorboard to get initialized
Serial.println("Start measuring");
initialHeading = razor.getLatestHeading(); //get heading at the beginning of the measurement
if (initialHeading < 0) {
initialHeading += 360; //if the heading is negative, scale it into a positive
}
}
void loop() {
if (!printed && (millis() - measurementTime > PRINT_OUT_TIME)) { //if nothing's printed yet and it's after PRINT_OUT_TIME milliseconds
int currentHeading = razor.getLatestHeading(); //get the current heading
if (currentHeading < 0) {
currentHeading += 360; //scale it into a positive if it's negative
}
int diff = currentHeading - initialHeading; //get the two headings' difference
Serial.print("The difference in heading is: ");
Serial.print(abs(diff)); //print the absolute of the differnnce
Serial.println(" degrees");
printed = !printed; //declare that it's printed so you don't keep printing it
}
}