entertainment bot Sparki


#1

Hi everyone

Don’t know if anyone would be interested in this but I wrote a code to turn Sparki into a generic entertainment robot. He uses his ping sensors to follow hands, his line sensors to dance/move/play a tune and his right and left light sensors also for dancing and tune playing. If you ask me he’s as good as my MiP…but I am biased lol.
Anyway its a good way to get the little ones interested in Sparki as they can interact a bit more with the robot, covering the light sensors and moving their hands and things, rather than the programming which is a bit beyond them yet.
Feel free to try it out
Ash :slight_smile:

#include <Sparki.h>

void setup()
{
	float lightLeft = 0;
	float lightCenter = 0;
	float lightRight = 0;
	float threshold = 800;
	float edgeLeft = 0;
	float edgeRight = 0;
        sparki.clearLCD(); // wipe the LCD clear
        sparki.drawString(40, 4, "HELLO");
        sparki.updateLCD(); // put the drawings on the screen
        delay(1000);
	sparki.moveLeft(30);
	sparki.moveRight(30);
	delay(1000);
	sparki.beep(640, 200);
	delay(200);
	sparki.beep(440, 200);
	delay(100);
	while(true)
	{
		lightLeft = sparki.lightLeft();
		lightCenter = sparki.lightCenter();
		lightRight = sparki.lightRight();
		edgeLeft = sparki.edgeLeft();
		edgeRight = sparki.edgeRight();
		sparki.servo(0);
		sparki.RGB(0,220,0);
		if(((lightCenter>lightLeft)&&(lightRight>lightLeft)))
		{
			delay(400);
			sparki.beep(840, 300);
			sparki.moveForward(3);
			sparki.moveRight(40);
			sparki.beep(440, 500);
			sparki.RGB(0,250,0);
			sparki.moveLeft(90);
			sparki.moveForward(3);
			sparki.beep(640, 200);
			delay(200);
			sparki.beep(740, 200);
			sparki.moveBackward(3);
			sparki.moveForward(5);
			sparki.RGB(220,0,0);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(640, 400);
			sparki.moveRight(90);
			sparki.moveBackward(5);
			sparki.beep(540, 400);
			sparki.RGB(0,0,220);
		}
		else
		{
			delay(1000);
		}
		if(((lightLeft>lightRight)&&(lightCenter>lightRight)))
		{
			delay(400);
			sparki.beep(840, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(640, 800);
			delay(200);
			sparki.beep(440, 800);
			delay(200);
			sparki.beep(340, 400);
			delay(200);
			sparki.beep(440, 400);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
		}
		else
		{
			delay(1000);
		}
		if(((edgeLeft<threshold)&&(sparki.ping()>10)))
		{
			sparki.RGB(220,0,0);
			sparki.beep(840, 400);
			sparki.moveLeft(90);
			sparki.beep(440, 200);
			sparki.moveRight(90);
			sparki.beep(340, 200);
			delay(100);
			sparki.beep(540, 200);
			sparki.RGB(0,0,220);
			sparki.beep(440, 400);
			sparki.moveForward(5);
			delay(200);
			sparki.beep(640, 400);
			delay(200);
			sparki.beep(740, 500);
			sparki.moveBackward(5);
			delay(200);
		}
		else
		{
		}
		if(((edgeRight<threshold)&&(sparki.ping()>10)))
		{
			sparki.beep(740, 600);
			delay(400);
			sparki.beep(540, 400);
			delay(400);
			sparki.beep(540, 200);
			delay(200);
			sparki.beep(440, 200);
			delay(200);
			sparki.beep(640, 400);
			delay(400);
			sparki.beep(740, 200);
			delay(400);
			sparki.beep(740, 200);
		}
		else
		{
		}
		sparki.servo(0);
		delay(1000);
		if((sparki.ping()<10))
		{
			sparki.moveForward(7);
			sparki.servo(90);
			delay(1000);
			if((sparki.ping()<10))
			{
				sparki.moveRight(90);
				sparki.moveForward(7);
				sparki.servo(-(90));
				delay(1000);
				if((sparki.ping()<10))
				{
					sparki.moveLeft(90);
					sparki.moveForward(7);
				}
				else
				{
					sparki.moveBackward(5);
					sparki.RGB(0,0,220);
					sparki.moveLeft(180);
				}
			}
			else
			{
				sparki.servo(-(90));
				delay(1000);
				if((sparki.ping()<10))
				{
					sparki.moveLeft(90);
					sparki.moveForward(7);
				}
				else
				{
				}
			}
		}
		else
		{
			sparki.servo(90);
			delay(1000);
			if((sparki.ping()<10))
			{
				sparki.moveRight(90);
				sparki.moveForward(7);
			}
			else
			{
				sparki.servo(-(90));
				delay(1000);
				if((sparki.ping()<10))
				{
					sparki.moveLeft(90);
					sparki.moveForward(7);
				}
				else
				{
					sparki.moveForward(5);
					sparki.RGB(0,0,220);
					sparki.moveRight(90);
					sparki.moveLeft(90);
				}
			}
		}
		delay(1000);
	}
}

void loop()
{
}

#2

Oh if you follow the link you will see a video of this code in action too…
:slight_smile: Ash

drive.google.com/file/d/0B6y9IT … sp=sharing


#3

so following on from this I tweaked the remote control code to include some of my favourite functions…I didnt realise it would hold so many!
this is the new code for anyone who may be interested :slight_smile: :slight_smile:

[code]#include <Sparki.h> // include the sparki library

void setup()
{
float lightLeft = 0;
float lightCenter = 0;
float lightRight = 0;
float threshold = 800;
float lineCentre = 0;
float edgeLeft = 0;
float edgeRight = 0;
}

// /------^-----
// | |
// | 69 70 71 |
// | 68 64 67 |
// | 7 21 9 |
// | 22 25 13 |
// | 12 24 94 |
// | 8 28 90 |
// | 66 82 74 |
// ____________/

void loop()
{
int code = sparki.readIR();
if(code != -1){
sparki.print("Received code: ");
sparki.println(code);
}

switch(code){

// Movement buttons
case 70: {sparki.moveForward(); break;}
case 21: {sparki.moveBackward(); break;}
case 67:
case 71: {sparki.moveRight(); break;}
case 68:
case 69: {sparki.moveLeft(); break;}
case 64: {sparki.moveStop();
sparki.gripperStop();
break;}

// Gripper Buttons
case 9: {sparki.gripperOpen(); break;}
case 7: {sparki.gripperClose(); break;}

// RGB Rainbow
case 74:
{

 sparki.clearLCD(); 
 delay(1000);
 sparki.drawString(40, 4, "RAINBOW");
 sparki.updateLCD();
 delay(1000);
 sparki.beep(440,400);
 delay(220);
 sparki.beep(640,200);

sparki.RGB(RGB_RED);
delay(500);

sparki.RGB(RGB_ORANGE);
delay(500);

sparki.RGB(RGB_YELLOW);
delay(500);

sparki.RGB(RGB_GREEN);
delay(500);

sparki.RGB(RGB_BLUE);
delay(500);

sparki.RGB(RGB_INDIGO);
delay(500);

sparki.RGB(RGB_VIOLET);
delay(500);

sparki.RGB(RGB_WHITE);
delay(500);

sparki.RGB(RGB_OFF);
delay(500);
};break;

// Servo
case 13: {sparki.servo(SERVO_RIGHT); break;}
case 25: {sparki.servo(SERVO_CENTER); break;}
case 22: {sparki.servo(SERVO_LEFT); break;}

// functions
case 8:
{
for (int i = 0; i < 10; i=i+1)
{
int left = sparki.lightLeft(); // measure the left light sensor
int center = sparki.lightCenter(); // measure the center light sensor
int right = sparki.lightRight(); // measure the right light sensor

if ( (center > left) && (center > right) ) // if the center light is the strongest
{
sparki.RGB(0,220,0);
sparki.moveForward(5); // move forward
}

if ( (left > center) && (left > right) ) // if the left light is the strongest
{
sparki.RGB(220,0,0);
sparki.moveLeft(30); // turn left
}

if ( (right > center) && (right > left) ) // if the right light is the strongest
{
sparki.RGB(0,0,220);
sparki.moveRight(30); // turn right
}

delay(100); // wait 0.1 seconds
}
}
;break;
case 28:
{
sparki.clearLCD();
delay(1000);
sparki.drawString(40, 4, “HELLO”);
sparki.updateLCD();
delay(3000);
}
break;

case 90:
{
for (int i = 0; i < 10; i=i+1)
{
sparki.servo(0);
int lineCentre = sparki.lineCenter();
int threshold = 700;
if ((lineCentre<threshold))
{
sparki.RGB(0,250,0);
sparki.moveForward(5);
delay(2000);
}
else
{
sparki.beep(440, 1000);
sparki.RGB(250,0,0);
sparki.moveRight(90);
}

            }

}
;break;
case 94:
{
delay(400);
sparki.beep(840, 400);
delay(200);
sparki.beep(640, 400);
delay(200);
sparki.beep(440, 400);
delay(200);
sparki.beep(340, 400);
delay(200);
sparki.beep(640, 800);
delay(200);
sparki.beep(440, 800);
delay(200);
sparki.beep(340, 400);
delay(200);
sparki.beep(440, 400);
delay(200);
sparki.beep(640, 400);
delay(200);
}
;break;

case 66:
{
sparki.beep(740, 600);
delay(400);
sparki.beep(540, 400);
delay(400);
sparki.beep(540, 200);
delay(200);
sparki.beep(440, 200);
delay(200);
sparki.beep(640, 400);
delay(400);
sparki.beep(740, 200);
delay(400);
sparki.beep(740, 200);
;break;
}
case 82:
{
sparki.RGB(220,0,0);
sparki.beep(840, 400);
sparki.moveLeft(90);
sparki.beep(440, 200);
sparki.moveRight(90);
sparki.beep(340, 200);
delay(100);
sparki.beep(540, 200);
sparki.RGB(0,0,220);
sparki.beep(440, 400);
sparki.moveForward(5);
delay(200);
sparki.beep(640, 400);
delay(200);
sparki.beep(740, 500);
sparki.moveBackward(5);
delay(200);
;break;
}

case 12:
{
for (int i = 0; i < 10; i=i+1)
{
sparki.servo(0);
if((sparki.ping()<40))
{
sparki.RGB(255,0,0);
sparki.beep(440, 400);
sparki.moveBackward(10);
sparki.moveRight(90);
}
else
{
sparki.moveForward(20);
}
}
}

;break;
case 24:
{
for (int i = 0; i < 3; i=i+1)
{
sparki.servo(0);
delay(1000);
if((sparki.ping()<10))
{
sparki.moveForward(7);
sparki.servo(90);
delay(1000);
if((sparki.ping()<10))
{
sparki.moveRight(90);
sparki.moveForward(7);
sparki.servo(-(90));
delay(1000);
if((sparki.ping()<10))
{
sparki.moveLeft(90);
sparki.moveForward(7);
}
else
{
sparki.moveBackward(5);
sparki.RGB(0,0,220);
sparki.moveLeft(180);
}
}
else
{
sparki.servo(-(90));
delay(1000);
if((sparki.ping()<10))
{
sparki.moveLeft(90);
sparki.moveForward(7);
}
else
{
}
}
}
else
{
sparki.servo(90);
delay(1000);
if((sparki.ping()<10))
{
sparki.moveRight(90);
sparki.moveForward(7);
}
else
{
sparki.servo(-(90));
delay(1000);
if((sparki.ping()<10))
{
sparki.moveLeft(90);
sparki.moveForward(7);
}
else
{
sparki.moveForward(5);
sparki.RGB(0,0,220);
sparki.moveRight(90);
sparki.moveLeft(90);
}
}
}
delay(1000);
}

break;
default:
break;
}
}}

        [/code]

#4

Neat! I was just wondering whether I could use the spark screen and the remote to make a system for the kids to program without a computer, just the remote, and this looks close to that.

Eg: Assign certain buttons on a remote certain functions (or make a custom keyboard for this–turn, forward, grasp, repeat, end repeat, and so on), and let kids type out a sequence that would show up on sparki’s screen that they could then run. Wasn’t sure if this would get too complex for the Arduino though.


#5

sponge - that sounds really neat! That’s definitely something Sparki could handle easily, as long as it was just a list of commands. Let me know if you do this, if not I might :wink:


#6

This is exceptionally decent and wonderful post.
I am very happy joine this forum.
I like it exceptionally much…!!!


#7

Great project. Keep us posting on progress.


#8

iv just started working on a blog based on Sparki and programming, check it out:

www.ashbotandsparki.wordpress.com