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





3 thoughts on “Sparki the Timer Robot- More improvements

  1. Hi,
    Your code will be more awesome if you replace fiveMinuteAlarm(), tenMinuteAlarm() and all other xMinuteAlarm() functions with a single function that takes as an argument number of minutes. Something like Alarm(int NumberOfMinutes) πŸ˜‰

    Liked by 1 person

    • I’ve amended this with your idea thank you so much πŸ™‚ πŸ™‚ Will update on my blog probably in a few months when I get round to it :
      Here is part of the new loop i used:

      if (sparki.lightRight()<sparki.lightLeft())
      {
      for (int i = 0; i < 5; i=i+1)
      {
      sparki.RGB(RGB_OFF);
      sparki.clearLCD();
      sparki.print("Minutes Remaining ");
      sparki.print(5-i);
      sparki.updateLCD();
      sparki.RGB(RGB_GREEN);
      delay(60000);
      }
      alarmTimeReached();
      }
      else
      {
      sparki.clearLCD();
      sparki.drawString(10,1, "10 minute alarm?");
      sparki.beep();
      sparki.updateLCD();
      delay (5000);
      if (sparki.lightRight()<sparki.lightLeft())
      {
      for (int i = 0; i < 10; i=i+1)
      {
      sparki.RGB(RGB_OFF);
      sparki.clearLCD();
      sparki.print("Minutes Remaining ");
      sparki.print(10-i);
      sparki.updateLCD();
      sparki.RGB(RGB_BLUE);
      delay(60000);
      }
      alarmTimeReached();
      }

      Like

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