Sparki the Timer Robot-Even more improvements

Hey Everyone

So you are probably sick to death of hearing about the timer robot code but I’m just trying to perfect it….

DSC_0350.JPG

Anyway, the one problem I had with the previous code was that once you had used the robot to set a timer it became a little long winded upon using it again, to be given instructions each time and to have to scroll through all of the options to get to the latest ones.

To fix this, I decided to add in an option at the start of the code where you could tell the robot if you had used the program before. If yes, the quick options function is triggered, which just makes it quicker and more user friendly.

Let me know what you think:

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

void setup() {
 sparki.RGB(RGB_OFF);
 sparki.servo(60);
}

void rightAndLeftChoice()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "RIGHT = YES: LEFT = NO");
 sparki.updateLCD();
 delay (5000);
}
void alarmTimeReached()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm time reached");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Swipe your hand across my eyes to stop the alarm");
 sparki.updateLCD();
 delay (5000);
 sparki.ping();
 while (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);
 }
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm off");
 sparki.beep();
 delay(500);
 sparki.updateLCD();
delay(5000);
}

void fiveMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "5 minute alarm set");
 sparki.beep();
 sparki.updateLCD();
 delay (3000);
 for (int i = 0; i < 3; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void tenMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "10 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 6; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void fifteenMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "15 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 9; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void twentyMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "20 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 12; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void twentyfiveMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "25 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 15; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void thirtyMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "30 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 18; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void sixtyMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "60 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 36; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);

}
}
void fiveMinuteAlarmSet()
{
 fiveMinuteAlarm();
 alarmTimeReached();
 delay(100000);
}
void tenMinuteAlarmSet()
{
 tenMinuteAlarm();
 alarmTimeReached();
 delay(100000);
}
void fifteenMinuteAlarmSet()
{
 fifteenMinuteAlarm();
 alarmTimeReached();
 delay(100000);
}
void twentyMinuteAlarmSet()
{
 twentyMinuteAlarm();
 alarmTimeReached();
 delay(100000);
}
void twentyfiveMinuteAlarmSet()
{
 twentyfiveMinuteAlarm();
 alarmTimeReached();
 delay(100000);
}
void thirtyMinuteAlarmSet()
{
 thirtyMinuteAlarm();
 alarmTimeReached();
 delay(100000);
}
void sixtyMinuteAlarmSet()
{
 sixtyMinuteAlarm();
 alarmTimeReached();
 delay(100000);
}
 
void slowOptions()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "A 5 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();

 if (sparki.lightRight()<sparki.lightLeft())
 {
 fiveMinuteAlarmSet();
 
 }
else
{
 sparki.clearLCD();
 sparki.drawString(10,2, "A 10 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 tenMinuteAlarmSet();
 }
else
{
 sparki.clearLCD();
 sparki.drawString(10,2, "A 15 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 fifteenMinuteAlarmSet();
 
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 20 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 twentyMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 25 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 twentyfiveMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 30 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 thirtyMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 60 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 sixtyMinuteAlarmSet();
 
 }
 }
 }
 }
 }
}
}
 }
void quickOptions()
{
 sparki.clearLCD();
 sparki.drawString(10,1, "The alarm times will appear on screen");
 sparki.beep();
 sparki.updateLCD();
 delay (3000);
 sparki.clearLCD();
 sparki.drawString(10,1, "Select RIGHT for yes and LEFT for no");
 sparki.beep();
 sparki.updateLCD();
 delay (6000);
 sparki.clearLCD();
 for (int i = 0; i < 5; i=i+1)
 {
 sparki.drawString(10,1, "5 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 
 if (sparki.lightRight()<sparki.lightLeft())
 {
 fiveMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,1, "10 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 if (sparki.lightRight()<sparki.lightLeft())
 {
 tenMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,1, "15 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 if (sparki.lightRight()<sparki.lightLeft())
 {
 fifteenMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,1, "20 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 if (sparki.lightRight()<sparki.lightLeft())
 {
 twentyMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,1, "25 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 if (sparki.lightRight()<sparki.lightLeft())
 {
 twentyfiveMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,1, "30 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 if (sparki.lightRight()<sparki.lightLeft())
 {
 thirtyMinuteAlarmSet();
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,1, "60 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 if (sparki.lightRight()<sparki.lightLeft())
 {
 sixtyMinuteAlarmSet();
 }
 }
 }
 }
 }
 }
 }
 }
 sparki.clearLCD();
 sparki.drawString(10,1, "OK maybe later");
 sparki.beep();
 sparki.updateLCD();
 delay (3000);
}
void loop()
{
 int left = sparki.lightLeft();
 int right = sparki.lightRight();
 sparki.beep(640, 400);
 delay(500);
 sparki.beep(340,400);
 delay(500);
 sparki.clearLCD();
 sparki.drawString(10,2, "Hello");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Would you like to set an alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Choose RIGHT sensor for yes");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Choose LEFT sensor for no");
 sparki.updateLCD();
 delay (5000);
 rightAndLeftChoice();
 if (right<left)
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "Have you set an alarm with me before?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "RIGHT = Yes : LEFT = No");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 if (right>left)
 {
 slowOptions();
 }
 
 else 
 {
 quickOptions();
 }
 }
 sparki.clearLCD();
 sparki.drawString(10,2, "Ok maybe later");
 sparki.updateLCD();
 delay (5000);
 sparki.beep(450,400);
 delay(500);
 sparki.beep(750,400);
delay(500);
delay (100000);
}



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);
}
}



Boomerang Bot Improvements

HI everyone

So a while ago I used variables to get Sparki to explore an area and return to the home zone.

I decided to improve this code using functions, constant variables, and a better way for the Sparki to get more accurately into the home area.

Code below: let me know what you think 🙂

And if you follow this link, you can watch a video of the code being completed.

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

const int response1 = random(1,3);

 const int response2 = random(45, 90);
 
 const int response3 = random(1,3); 

 const int response4 = random(45, 90);

 const int response5 = random(1, 3);
 
 const int response6 = random(45, 90);
 
 const int response7 = random(1, 3);
 
 const int home=(response1*10+3);
 
void setup()
{
 sparki.servo(0);
}
void loop()
{
 startingHomeZone();
 movingForward();
 movingBack();
 homeAdjust();
}
void startingHomeZone()
{
 delay(1000);
 sparki.beep();
 sparki.moveLeft(30);
 delay(1000);
 sparki.moveRight(30);
 sparki.clearLCD();
 sparki.println("home zone");
 sparki.updateLCD();
 delay(1000);
}

void movingForward()
{
 sparki.clearLCD();
 randomSeed(analogRead(1));
int response1 = random(1,3);
 sparki.clearLCD();
 sparki.print(response1);
 sparki.updateLCD();
 delay(1000);
 sparki.moveForward(response1 *10);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response2 = random(45, 90);
 sparki.clearLCD();
 sparki.print(response2);
 sparki.updateLCD();
 delay(1000);
 sparki.moveLeft(response2);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response3 = random(1,3);
 sparki.clearLCD();
 sparki.print(response3);
 sparki.updateLCD();
 delay(1000);
 sparki.moveForward(response3*10);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response4 = random(45, 90);
 sparki.clearLCD();
 sparki.print(response4);
 sparki.updateLCD();
 delay(1000);
 sparki.moveRight(response4);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response5 = random(1, 3);
 sparki.clearLCD();
 sparki.print(response5);
 sparki.updateLCD();
 delay(1000);
 sparki.moveForward(response5 *10);
 delay(1000);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response6 = random(45, 90);
 sparki.clearLCD();
 sparki.print(response6);
 sparki.updateLCD();
 delay(1000);
 sparki.moveLeft(response6);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response7 = random(1, 3);
 sparki.clearLCD();
 sparki.print(response7);
 sparki.updateLCD();
 delay(1000);
 sparki.moveForward(response7 *10);
 delay(1000);
}
 void movingBack()
{
 sparki.moveLeft(180);
 delay(1000);
 sparki.moveForward(response7 *10);
 delay(1000);
 sparki.moveRight(response6);
 delay(1000);
 sparki.moveForward(response5*10);
 delay(1000);
 sparki.moveLeft(response4);
 delay(1000);
 sparki.moveForward(response3*10);
 delay(1000);
 sparki.moveRight(response2);
 delay(1000);

}

void homeAdjust()
{
 int home=(response1*10+3);
 sparki.moveForward(home);
 sparki.moveLeft(180);
 delay(1000);
 sparki.moveForward(2);
 sparki.beep();
 sparki.clearLCD();
 sparki.drawString(10,1, "home");
 sparki.updateLCD();
 delay(100000);
}

 

Entertainment bot improvements

So in an earlier post I shared my entertainment bot code, which included:

  • a version of robot snap
  • some snazzy dance moves
  • some random sentences (to appear conversational) displaying on the LCD
  • along with the traditional flashing LED’s
  • random bleeps and beeps

All in all, this code turns Sparki into the generic toy robot everybody wants to play with.

DSC_0464.JPG

Recently I have been enjoying creating the more practical codes for timers, but on returning to this more playful code I have had some improvements to make.

Check out the full, improved code below and let me know what you think 🙂

 

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

void setup()
{
 sparki.servo(60);
 sparki.clearLCD();
 sparki.drawString(10, 1, "Hello human");
 sparki.updateLCD();
 delay(3000);
 sparki.beep();
 sparki.RGB(255,0,0);
 sparki.clearLCD();
 sparki.drawString(10, 1, "beginning search");
 sparki.updateLCD();
 delay(3000);
 sparki.beep();
 sparki.clearLCD();
 sparki.drawString(10, 1, "looking for human");
 sparki.updateLCD();
 delay(3000);
 sparki.ping();
 delay(1000);
 if (sparki.ping() <10)
 {
 sparki.moveForward(10);
 sparki.clearLCD();
sparki.drawString(10,2, "yey friends!");
sparki.updateLCD();
delay(3000);}
if (sparki.ping()>10)
{
 sparki.moveLeft(30);
 sparki.moveRight(30);
 sparki.beep(840, 400);
 delay(200);
 sparki.beep(640, 400);
 delay(200);
 sparki.beep(440, 400);
 delay(200);
 sparki.beep(340, 400);
}
 for (int i = 0; i < 5; i=i+1)
 {
dance();
delay(1000);
entertainment1();
delay(1000);
dance2();
delay(1000);
conversation();
delay(1000);
melody();
delay(1000);
robotSnap();
delay(1000);
rainbow();
delay(1000);
conversation();
delay(1000);
dance();
delay(1000);
entertainment1();
delay(1000);
melody();
delay(1000);
rockPaperScissors();
delay(1000);
dance2();
delay(1000);
rainbow();
delay(1000);
conversation();
delay(1000);

}
}
void dance()
{
 sparki.moveRight(90);
 sparki.moveForward(5);
 sparki.moveLeft(90);
 sparki.beep();
 sparki.moveBackward(5);
 sparki.moveRight(90);
 sparki.moveForward(5);
 sparki.beep();
 sparki.moveLeft(90);
 sparki.moveBackward(5);
 sparki.moveRight(90);
 sparki.beep();
 sparki.moveForward(5);
 sparki.moveLeft(90);
 sparki.moveBackward(5);
}
void dance2()
{
 sparki.moveLeft(90);
 sparki.moveForward(5);
 sparki.beep();
 sparki.moveBackward(5);
 sparki.moveForward(5);
 sparki.RGB(RGB_BLUE);
 sparki.moveBackward(5);
 sparki.beep();
 sparki.moveForward(5);
 sparki.moveRight(180);
 sparki.RGB(RGB_GREEN);
 sparki.moveLeft(7);
 sparki.beep();
 sparki.moveRight(14);
 sparki.moveLeft(7);
 sparki.RGB(RGB_RED);
}
 
void rainbow()
{
 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.beep();
 
 sparki.RGB(RGB_BLUE);
 delay(500);
 
 sparki.RGB(RGB_INDIGO);
 delay(500);
 
 sparki.RGB(RGB_VIOLET);
 delay(500);
 sparki.beep();
 
 sparki.RGB(RGB_WHITE);
 delay(500);
 
 sparki.RGB(RGB_OFF);
 delay(500);
 }
void melody()
{
 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);
}
void entertainment1()
{
 sparki.clearLCD();
 sparki.drawString(10, 1, "I am in entertainment BOT mode");
 sparki.updateLCD();
 delay(1000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 delay(1000);
 sparki.beep(640, 200);
 delay(200);
 sparki.beep(440, 200);
 delay(100);
 int lightLeft = sparki.lightLeft();
 int lightCenter = sparki.lightCenter();
 int lightRight = sparki.lightRight();
 int edgeLeft = sparki.edgeLeft();
 int edgeRight = sparki.edgeRight();
 sparki.servo(0);
 sparki.RGB(0,220,0);
 if(((lightCenter>lightLeft)&&(lightRight>lightLeft)))
 {rainbow();} 
 else
 {
 delay(1000);
 }
 if(((lightLeft>lightRight)&&(lightCenter>lightRight)))
 {
 melody(); 
 
 }
 else
 {
 delay(1000);
 }
}
void conversation()
{
 randomSeed(analogRead(1));
 int response = random(0, 15);
 sparki.clearLCD();
 sparki.print(response);
 sparki.updateLCD();
 delay(2000);
 if((response>=0) && (response <3))
 {
 sparki.clearLCD();
 sparki.drawString(10, 1, "When I grow up I want to be a social domestic bot");
 sparki.updateLCD();
 delay(3000);
 dance();
 }
 else if((response>=3) && (response <=8))
 {
 sparki.clearLCD();
sparki.drawString(10,2, "Preparing lazers for combat");
sparki.updateLCD();
delay(3000);
sparki.beep(740, 200);
sparki.clearLCD();
sparki.drawString(10,2, "Pew Pew");
sparki.updateLCD();
delay(1000);
sparki.beep(740,200);
delay(100);
sparki.beep(440,200);
sparki.moveRight(10);
sparki.moveLeft(10);
delay(2000);
 }
 
 else if((response>=9) && (response <=12))
 {
 sparki.clearLCD();
sparki.drawString(10,2, "I will be kind to you when we robots take over the world");
sparki.updateLCD();
delay(3000);
dance2();
 }
 
else if((response>=9) && (response <=12))
{
 sparki.clearLCD();
 sparki.drawString(10, 1, "Today I organised my data stream");
 sparki.updateLCD();
 delay(3000);
 sparki.beep(440, 600);
 delay(400);
 sparki.beep(540, 400);
 delay(400);
 sparki.beep(640, 200);
 delay(200);
 sparki.beep(740, 200);
 delay(200);
 sparki.beep(640, 400);
 delay(400);
 sparki.beep(540, 200);
 delay(400);
 sparki.beep(440, 200);
sparki.clearLCD();
sparki.drawString(10,1, "Perfect");
 sparki.updateLCD();
 delay(3000);
 }
 else 
 {
 sparki.clearLCD();
sparki.drawString(10,2, "Today I beamed Jim. He's my cousin in California!");
sparki.updateLCD();
delay(3000);
 sparki.moveRight(90);
 sparki.moveForward(5);
 sparki.RGB(RGB_BLUE);
 sparki.beep();
 sparki.moveBackward(5);
 sparki.moveRight(50);
 sparki.moveBackward(5);
 sparki.moveLeft(50);
 sparki.RGB(RGB_GREEN);
 sparki.beep();
 }
}
void robotSnap()
{
 sparki.clearLCD();
 sparki.drawString(20,2,"Lets play robot snap");
 sparki.updateLCD();
 delay(3000);
 for (int i = 0; i < 5; i=i+1)
 {
 randomSeed(analogRead(1));
 int responsea = random(1, 3);
 if (responsea==1)
 {
 sparki.clearLCD(); // wipe the LCD clear
 sparki.drawRect(5,5,30,10); //small empty rectangle in upper-left
 sparki.updateLCD(); // put the drawings on the screen
 delay(500);
 sparki.clearLCD();
 }
 if (responsea==2)
 {
 sparki.clearLCD(); // wipe the LCD clear
 sparki.drawCircle(10,10,5); //small empty circle in upper-left
 sparki.updateLCD(); // put the drawings on the screen
 delay(500);
 sparki.clearLCD();
 
 
 }
 if (responsea==3)
 {
 sparki.clearLCD(); // wipe the LCD clear
 sparki.drawCircleFilled(90,40,20); // big filled circle in lower-right
 sparki.updateLCD(); // put the drawings on the screen
 delay(500);
 sparki.clearLCD();
 
 }
 randomSeed(analogRead(1));
 int responseb = random(1, 3);
 if (responseb==1)
 {
 sparki.clearLCD(); // wipe the LCD clear
 sparki.drawRect(5,5,30,10); //small empty rectangle in upper-left
 sparki.updateLCD(); // put the drawings on the screen
 delay(500);
 sparki.clearLCD();
 
 }
 if (responseb==2)
 {
 sparki.clearLCD(); // wipe the LCD clear
 sparki.drawCircleFilled(90,40,20); //small empty circle in upper-left
 sparki.updateLCD(); // put the drawings on the screen
 delay(500);
 sparki.clearLCD();
 
 }
 if (responseb==3)
 {
 sparki.clearLCD(); // wipe the LCD clear
 sparki.drawCircle(10,10,5); 
 sparki.updateLCD(); // put the drawings on the screen
 delay(500);
 sparki.clearLCD();
 }
 if ((responsea==1) && (responseb==1))
 {
 delay(1000);
 if(sparki.lightCenter()>sparki.lightRight())
 
 {
 sparki.clearLCD();
 sparki.drawString(20,2,"Well done! You were first");
 sparki.updateLCD();
 delay(1000);
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(20,2,"Snap! I win!");
 sparki.updateLCD();
 delay(1000);
 }
 }
 if ((responsea==2) && (responseb==2))
 {
 delay(1000);
 if(sparki.lightCenter()>sparki.lightRight())
 {
 sparki.clearLCD();
 sparki.drawString(20,2,"Well done! You were first");
 sparki.updateLCD();
 delay(1000);
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(20,2,"Snap! I win!");
 sparki.updateLCD();
 delay(1000);
 }
 }
 if ((responsea==3) && (responseb==3))
 {
 delay(1000);
 if (sparki.lightCenter()>sparki.lightRight())
 {
 sparki.clearLCD();
 sparki.drawString(20,2,"Well done! You were first");
 sparki.updateLCD();
 delay(1000);
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(20,2,"Snap! I win!");
 sparki.updateLCD();
 delay(1000);
 }
 }
 }
 }
 void rockPaperScissors()
 {
 sparki.clearLCD();
sparki.drawString(20,2,"Let's play rock paper scissors?");
sparki.updateLCD();
delay(1000);
sparki.clearLCD();
sparki.drawString(20,2,"Choose RIGHT for yes and LEFT for no");
sparki.updateLCD();
delay(5000);
if (sparki.lightRight()<sparki.lightLeft())
 {
 randomSeed(analogRead(1));
 int response = random(1, 4);
 if (response==1)
 {sparki.clearLCD();
 sparki.drawString(10, 1, "Here is your randomly generated choice");
 sparki.updateLCD();
 delay(2000);
 sparki.clearLCD();
 randomSeed(analogRead(1));
 int response1a = random(1, 10);
 if (response1a<=4)
 {
 sparki.clearLCD();
 sparki.print("rock");
 sparki.updateLCD();
 delay(1000);
 }
 else if ((response1a>4) && (response1a<7))
 {
 sparki.clearLCD();
 sparki.print("paper");
 sparki.updateLCD();
 delay(1000);
 }
 else if (response1a<=7)
 {sparki.clearLCD();
 sparki.print("scissors");
 sparki.updateLCD();
 delay(1000);
 }
 
 sparki.clearLCD();
 sparki.drawString(10, 1, "This is my choice");
 sparki.updateLCD();
 delay(1000);
 randomSeed(analogRead(1));
 int response2b = random(1, 3);
 
 if (response2b==1)
 {
 sparki.clearLCD();
 sparki.print("rock");
 sparki.updateLCD();
 delay(1000);
 }
 else if (response2b==2)
 {
 sparki.clearLCD();
 sparki.print("paper");
 sparki.updateLCD();
 delay(1000);
 }
 else if (response2b==3)
 {sparki.clearLCD();
 sparki.print("scissors");
 sparki.updateLCD();
 delay(1000);
 }
 if ((response1a<=4) && (response2b==3))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Well done you won");
 sparki.updateLCD();
 sparki.beep();
 delay(2000);
 }
 else if ((response1a<=4) && (response2b==2))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Yey! I won!");
 sparki.updateLCD();
 delay(1000);
 }
 else if ((response1a<=4) && (response2b==1))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "It was a draw!");
 sparki.updateLCD();
 delay(2000);
 }
 if (((response1a>4) && (response1a<7) && (response2b==1)))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Well done you won");
 sparki.updateLCD();
 sparki.beep();
 delay(2000);
 }
 else if (((response1a>4) && (response1a<7) && (response2b==3)))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Yey! I won!");
 sparki.updateLCD();
 delay(1000);
 }
 else if (((response1a>4) && (response1a<7) && (response2b==2)))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "It was a draw!");
 sparki.updateLCD();
 delay(2000);
 }
 if ((response1a>=7) && (response2b==2))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Well done you won");
 sparki.updateLCD();
 sparki.beep();
 delay(2000);
 }
 else if ((response1a>=7) && (response2b==1))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "Yey! I won!");
 sparki.updateLCD();
 delay(1000);
 }
 else if ((response1a>=7) && (response2b==3))
 {
 sparki.clearLCD();
 sparki.drawString(20,2, "It was a draw!");
 sparki.updateLCD();
 delay(2000);
 }
}
 }
 }
 void loop()
 {}
 

Sparki the Timer Robot- More improvements

Hey everyone

13138730_10208751226474608_3515831574067619993_n

So as you can all see I’ve been working on turning Sparki into a timer robot. This has been working really well but I thought of even more improvements that I wanted to make:

  • more timer choices – instead of just 5 or 10 mins I wanted every option from 5-30 mins and an extra option for 60 mins
  • more efficiency in the actual code- instead of repeating code, use functions for parts of the coding that will be repeated
  • more instructions- to help the user know what to do
  • more beeps for alerts – every time the user needs to read the screen for an instruction or to alert the reader that an instruction has been carried out

This the code I came up with, let me know what you think:

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

void setup() {
 sparki.RGB(RGB_OFF);
 sparki.servo(60);
}

void rightAndLeftChoice()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "RIGHT = YES: LEFT = NO");
 sparki.updateLCD();
 delay (5000);
}
void alarmTimeReached()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm time reached");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Swipe your hand across my eyes to stop the alarm");
 sparki.updateLCD();
 delay (5000);
 sparki.ping();
 while (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);
 }
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm off");
 sparki.beep();
 delay(500);
 sparki.updateLCD();
delay(5000);
}

void fiveMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "5 minute alarm set");
 sparki.beep();
 sparki.updateLCD();
 delay (3000);
 for (int i = 0; i < 3; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void tenMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "10 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 6; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void fifteenMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "15 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 9; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void twentyMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "20 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 12; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void twentyfiveMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "25 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 15; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void thirtyMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "30 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 18; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
}
void sixtyMinuteAlarm()
{
 sparki.clearLCD();
 sparki.drawString(10,2, "60 minute alarm set");
 sparki.beep();
 delay(500);
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 36; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);

}
}
void loop()
{
 int left = sparki.lightLeft();
 int right = sparki.lightRight();
 sparki.beep(640, 400);
 delay(500);
 sparki.beep(340,400);
 delay(500);
 sparki.clearLCD();
 sparki.drawString(10,2, "Hello");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Would you like to set an alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Choose RIGHT sensor for yes");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Choose LEFT sensor for no");
 sparki.updateLCD();
 delay (5000);
 rightAndLeftChoice();
 if (right<left)
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 5 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();

 if (sparki.lightRight()<sparki.lightLeft())
 {
 fiveMinuteAlarm();
 alarmTimeReached();
 delay(100000);
 }
else
{
 sparki.clearLCD();
 sparki.drawString(10,2, "A 10 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 tenMinuteAlarm();
 alarmTimeReached();
 delay(100000);
 }
else
{
 sparki.clearLCD();
 sparki.drawString(10,2, "A 15 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 fifteenMinuteAlarm();
 alarmTimeReached();
 delay(100000);
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 20 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 twentyMinuteAlarm();
 alarmTimeReached();
 delay(100000);
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 25 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 twentyfiveMinuteAlarm();
 alarmTimeReached();
 delay(100000);
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 30 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 thirtyMinuteAlarm();
 alarmTimeReached();
 delay(100000);
 }
 else
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "A 60 minute alarm?");
 sparki.beep();
 sparki.updateLCD();
 delay (5000);
rightAndLeftChoice();
 if (sparki.lightRight()<sparki.lightLeft())
 {
 sixtyMinuteAlarm();
 alarmTimeReached();
 delay(100000);
 }
 }
 }
 }
 }
}
}
 }
 
 sparki.clearLCD();
 sparki.drawString(10,2, "Ok maybe later");
 sparki.updateLCD();
 delay (5000);
 sparki.beep(450,400);
 delay(500);
 sparki.beep(750,400);
delay(500);
delay (100000);
}





ArduinoDroid

Hi everyone

So I recently downloaded ArduinoDroid which is a free app available on the Google Play Store, and I have to say I think it’s great.

ArduinoDroid provides you with the Arduino IDE including libraries and examples and enables you to write code, compile and debug as well as upload code by selecting the right board from the list.

This is a really useful app for me and it means I can tweak code and write new programs while I’m on the move. I was impressed with how many features it has and it doesnt take up too much space on my internal memory. Here are some screenshots of some code I’ve been working on, unfortunately not Sparki related this time.

 

Sparki the Timer Robot – improvements

So in this code I aimed to build on my Sparki the Timer Robot code to allow the user to switch off both the five minute alarm and the ten minute alarm whilst it is sounding. This made it more user friendly and less annoying!

DSC_0395

Here is a link to the video:

Find below the code, let me know what you think 🙂

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

void setup() {
 sparki.RGB(RGB_OFF);
 sparki.servo(60);
}
void loop()
{
 sparki.beep(640, 400);
 delay(500);
 sparki.beep(340,400);
 delay(500);
 sparki.clearLCD();
 sparki.drawString(10,2, "Hello");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Would you like to set an alarm?");
 sparki.updateLCD();
 delay (5000);
 int left = sparki.lightLeft();
 int right = sparki.lightRight();
 if (left>=right)
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "Choose Right for five minutes");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Choose Left for ten minutes");
 sparki.updateLCD();
 delay (5000);
 sparki.clearLCD();
 sparki.drawString(10,2, "Right (5) or Left (10)");
 sparki.updateLCD();
 delay (5000);
 int left = sparki.lightLeft();
 int right = sparki.lightRight();
 if (left>=right)
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "5 minute alarm set");
 sparki.updateLCD();
 delay (3000);
 for (int i = 0; i < 3; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm time reached");
 sparki.updateLCD();
 delay (5000);
 sparki.ping();
 while (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);
 }
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm off");
 sparki.updateLCD();
 delay(100000);
}
 
 
 if (left<right)
 {
 sparki.clearLCD();
 sparki.drawString(10,2, "10 minute alarm set");
 sparki.updateLCD();
 delay (5000);
 for (int i = 0; i < 6; i=i+1)
 {
 delay (100000);
 sparki.moveLeft(30);
 sparki.moveRight(30);
 }
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm time reached");
 sparki.updateLCD();
 delay (5000);
 while (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);
 }
 sparki.clearLCD();
 sparki.drawString(10,2, "Alarm off");
 sparki.updateLCD();
 delay(100000);
}
 }
 
 sparki.clearLCD();
 sparki.drawString(10,2, "Ok maybe later");
 sparki.updateLCD();
 delay (5000);
 sparki.beep(450,400);
 delay(500);
 sparki.beep(750,400);
delay(500);
delay (100000);
}