Rewriting the remote control

Hi everyone so I posted the standard Sparki remote control code yesterday and today (as promised) I am posting the code that I wrote for the remote control.

I wanted to use the remote control to flit between modes for Sparki so I took the ideas from my entertainment bot code and some of my other favourite aspects of Sparki and put the simplest versions of these codes onto the remote control. This means buttons can make Sparki light follow, line follow, wander around autonomously, follow hands and play different tunes.

I was quite impressed with myself when I wrote this but I know I could do something much better if I were to work on it again, especially with my new Sparki mods!

Anyway here is the code:

/*******************************************
  IR Remote
  
  Sparki has a sensor that lets it receives
  commands from the included remote control.
  Try moving it around!
  
  http://arcbotics.com/infrared-remote-receiver/
********************************************/
#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();

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

  // buzzer
  case 74: {sparki.beep(); 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: 
  {
          int lineCentre = sparki.lineCenter();
          int edgeRight = sparki.edgeRight();
          int threshold = 400;
          for (int lineCentre = 0; lineCentre < 3; lineCentre++)
          {
          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 28: 
  {
     sparki.clearLCD(); 
     delay(1000);
     sparki.drawString(40, 4, "HELLO");
     sparki.updateLCD(); // put the drawings on the screen
     delay(1000);
  }
  break;
  
  case 90:
  {
             int left = sparki.lightLeft();
             int right = sparki.lightRight();
             int center = sparki.lightCenter(); // measure the center light sensor
             if( (center < left) && (center < right) ) // if the center light is the strongest
             {  
              sparki.moveLeft(30);
              sparki.beep(640, 400);
             }
          else
          {
            sparki.moveForward(3);
              delay(1000);
            
  }}
}       
  ;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;
  }
  }}

                  
                                        
            

Let me know what you think 🙂

P.S. obviously no video…that would be huge!!! here is a picture though 🙂

DSC_0152

Advertisements

2 thoughts on “Rewriting the remote control

  1. Wow, talk about a leap. The remote control can be programmed to make Sparki do other modes? Really awesome to input commands. Like to see Sparki become transform on command. 🙂

    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