Sparki has one Ultrasonic range sensor in its eye ; one is to transmit sound wave where the other receives the sound from reflection. The distance is calculated using the time difference of transmission and reception.

File Download and Detail:

  • Lab Instruction
  • Quiz
  • Sample Code
    • Wall_Avoidance
#include <Sparki.h> // include the sparki library
 
void setup() {
  sparki.servo(SERVO_CENTER); // Center the Servo
}

void loop()
{
    sparki.moveForward(); // move Sparki forward
    int cm = sparki.ping(); // measures the distance with Sparki's eyes
        
    if(cm != -1) // make sure its not too close or too far
    { 
        if(cm < 15) // if the distance measured is less than 15 centimeters
        {
            sparki.RGB(RGB_RED); // turn the light red
            sparki.beep(); // beep!
            sparki.moveBackward(5); // back up 5 centimeters
            sparki.moveRight(45); // rotate right 45 degrees
        }
    }
    delay(100); // wait 0.1 seconds (100 milliseconds)
}