Skip to content
Dimitris Platis edited this page Feb 17, 2015 · 20 revisions

Introduction

Here some typical cases of the library usage will be demonstrated. Feel free to use and adjust them at your own will. These examples are also included in the Smartcar Sensors library, under the /examples folder.

Examples

###Sonar 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 <Smartcar_sensors.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());
}

###Sharp_IR 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 <Smartcar_sensors.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(A5); //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
}

###Odometer 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 <Smartcar_sensors.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());
}

###Gyroscope 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 <Smartcar_sensors.h>
#include <Wire.h>

Gyroscope gyro;

void setup() {
  Serial.begin(9600);
  gyro.attach();
  delay(1500);
  Serial.println("Start measuring!");
  gyro.begin();
}

void loop() {
  Serial.println(gyro.getAngularDisplacement());
  delay(100);
}

Clone this wiki locally