Sparki Relay Race

Hi everyone

So as you probably know by now I work in an FE College and we have recently purchased 6 Sparki’s for the level 2, level 3 and degree level ICT students as an enhancement to the curriculum.

In celebration of this, we ran a taster session for the Key Stage 3 and GCSE students in the local area and coded a basic relay race for the Sparki robots to complete.

Click here to watch a video of the code in action.

DSC_0008.JPG

Here is the code for the Sparki’s:

THE FIRST ROBOT

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

void setup() {
 sparki.servo(0);
 sparki.clearLCD();
 sparki.println("1st Sparki");
 sparki.updateLCD();
 delay(1000);
 findWall();
 avoidWall();
 findWall();
 avoidWall();
 findRobot();
 delay(100000);
}
void findWall()
{
 sparki.ping();
delay(1000);
while (sparki.ping()>10)
{
 sparki.moveForward();
delay(200);
}
}
void avoidWall()
{
 sparki.ping();
delay(1000);
if (sparki.ping()<20)
{
 sparki.moveRight(90);
}
}
void findRobot()
{
 sparki.ping();
 delay(1000);
 while (sparki.ping()>8)
 {
 sparki.moveForward(5);
 sparki.ping();
 delay(1000);
 if (sparki.ping()<=8)
 {
 sparki.servo(0);
 sparki.clearLCD();
 sparki.println("Found 2nd Sparki");
 sparki.updateLCD();
 delay(1000);
sparki.beep();
sparki.gripperClose(10);
delay(1000);
sparki.gripperOpen(10);
delay(1000);
sparki.gripperStop();
sparki.moveStop();
delay(100000);
}
}
}
void loop() {
 // put your main code here, to run repeatedly:

}

THE SECOND ROBOT

#include <Sparki.h> // include the sparki library
void setup()
{}
void loop() {

 sparki.servo(0);
 sparki.clearLCD();
 sparki.println("2nd Sparki");
 sparki.updateLCD();
 delay(1000);
 sparki.ping();
delay(1000);
if (sparki.ping()<20)
{
 delay (10000);
sparki.beep();
sparki.moveBackward(10);
sparki.moveLeft(180);
 
 findWall();
 avoidWall();
 findTheFinishLine();
 delay(100000);
}
delay(1000);
}

void findWall()
{
 sparki.ping();
delay(1000);
while (sparki.ping()>10)
{
 sparki.moveForward();
delay(200);
}
}
void avoidWall()
{
 sparki.ping();
delay(1000);
if (sparki.ping()<20)
{
 sparki.moveRight(90);
}
}
void findTheFinishLine()
{
 sparki.ping();
delay(1000);
while (sparki.ping()>10)
{
 sparki.moveForward(5);
}
int lineLeft = sparki.lineLeft(); 
int lineRight = sparki.lineRight(); 
sparki.clearLCD();
sparki.println(lineLeft);
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.println(lineRight);
sparki.updateLCD();
delay(1000);
if ((lineLeft<600) && (lineRight<600))
{
sparki.beep();
sparki.RGB(0,250,0);
sparki.clearLCD();
sparki.println("complete");
sparki.updateLCD();
delay(3000);
}
}



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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s