Sound activated autonomous Sparki

This is a code to turn Sparki into an autonomous exploring robot that is activated by sound. He makes for an awesome little desk bot with his edge sensors preventing him from falling, and his obstacle sensors helping him to avoid collisions.

Here is the code:

#include <Sparki.h> // include the sparki library

int actions=2;

void setup()

{
sparki.servo(30);

}

void interact() {
randomSeed(analogRead(1));
int actions = random(1,5);
if (actions ==5)
{
delay(1000);
sparki.beep(400,150);
delay(250);
sparki.beep(450,150);
delay(250);
sparki.beep(400,150);
delay(250);
sparki.beep(350,150);
delay(250);

}
if (actions==4)
{
delay(1000);
sparki.beep(300,150);
delay(250);
sparki.beep(250,150);
delay(250);
sparki.beep(200,150);
delay(250);
sparki.beep(650,150);
delay(250);

}
if(actions==3){
delay(1000);
sparki.beep(500,150);
delay(250);
sparki.beep(550,150);
delay(250);
sparki.beep(600,150);
delay(250);
sparki.beep(650,150);
delay(250);

}
if(actions==2)
{
delay(1000);
sparki.beep(300,150);
delay(250);
sparki.beep(400,150);
delay(250);
sparki.beep(300,150);
delay(250);
sparki.beep(450,150);
delay(250);
}
if(actions==1){
delay(1000);
sparki.beep(400,150);
delay(250);
sparki.beep(250,150);
delay(250);
sparki.beep(300,150);
delay(250);
sparki.beep(350,150);
delay(250);

}
}
void freeRoam(){
sparki.clearLCD();
sparki.drawString(10,1, “I’m exploring”);
sparki.updateLCD();
delay(1000);
for (int i = 0; i < 60; i=i+1)
{
int edgeLeft = sparki.edgeLeft(); // measure the left edge IR sensor
int edgeRight = sparki.edgeRight(); // measure the right edge IR sensor

int threshold = 200; // if below this value, no surface underneath

if (edgeLeft < threshold) // if no surface underneath left sensor
{
sparki.moveBackward(4);
sparki.moveRight(20); // turn right
}

if (edgeRight < threshold) // if no surface underneath right sensor
{
sparki.moveBackward(4);
sparki.moveLeft(20); // turn left
}

if (sparki.ping() < 5) {
sparki.clearLCD();
sparki.print(sparki.ping());
sparki.updateLCD();
delay(100);
sparki.moveBackward(4);
sparki.moveLeft(20);
sparki.moveRight(20);
sparki.RGB(RGB_RED);
delay(100);
sparki.RGB(RGB_RED);
delay(100);
sparki.RGB(RGB_RED);
delay(100);
sparki.beep(250,150);
delay(250);
sparki.beep(250,150);
delay(250);
sparki.moveRight(20);
}

sparki.moveForward(); // move forward
delay(100); // wait 0.1 seconds

}
sparki.moveStop();
}
void loop(){
sparki.clearLCD();
sparki.drawString(10,1, “I’m listening”);
sparki.updateLCD();

sparki.beep(650,150);
delay(250);
sparki.beep(550,150);
delay(250);
delay(3000);

int soundA=analogRead(0);
delay(500);
sparki.clearLCD();
sparki.print(soundA);
sparki.updateLCD();
delay(1000);

if(soundA>0.5){
sparki.RGB(RGB_GREEN);
sparki.moveLeft(20);
sparki.moveRight(20);

interact();
freeRoam();

}
sparki.RGB(RGB_RED);
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s