Building robots and automation with embedded tools and projects.
Pod Casts
News
Reviews
How To
Projects
Resources
Contact

Home / Projects / Land
Hide and Seek Lego
Jim Garvin        26 Oct 2000

Contact: URL: http://www.techgeek.com
Skill Level: beginner

Project Outline
Is there any better toy then Lego? I don't know. Oh, I know what your are saying, Lego's are for children. Well not when you add a microprocessor! The Lego Mindstorm is a great way to get started But I wanted to show you why.

With this project I took the lego mindstorm basic kit and the juiced it up. I replaced the OS with LegOS From there I can program using C instead of the default drag and drop OS that the mindstorm kit comes with.

The biggest improvement you get by using LegOS is that you get and "if" statement. Now I can make more complex statements and use the microprocessor to my advantage.

My project was a "hide and seek" robot. Having a five year old running around the house made this a worth while project. I built the base using the tracks. For sensors, I used used two of the straws on a pivot point to push the momentary switches for whiskers. I used the light sensor and pointed it up. The finished assembly is seen the the photograph.

Now came the programing. The LegOS can be written and compiled on Linux. Yes Linux, the OS that should rule the world ! The basics of the program was to build a bump and turn robot that would do this for a certain amount of time measured by the lego's internal timer. In this case it was 45 Sec. Once the time ran out it would keep moving until it hit the next object. This way it would not stop in the middle of the room. It would stop next to a wall or under a table or something like that. Sure this isn't too had to find but for a five year old this rules. Once it was there it would wait until the light sensor was lit up with a flash light thus telling the robot it was found. It would back up and spin to acknowldge it was found the then start the process all over again.

Using some simple easy to find software. I wrote and compiled and down loaded the code the the robot and I was off and running. The only question was who it entertained more me, the five year old or the cats.



Latest Journal Entry
The Code    26 Oct 2000

// HideSeek.cpp
// Hide And Seek Program
// by Jim Garvin
// Last Modified 12 Nov. 1999

#include < conio.h >
#include < unistd.h >
#include < time.h >
#include < dsensor.h >
#include < dmotor.h >

#include "base.C"


/////////////////////////////////////////////////////////
//                      GLOBALS

const int TRUE = 1;
const int FALSE = 0;
const int FWD = 0;
const int REV = 1;
const int LEFT = 0;
const int RIGHT = 1;
const int MAXWAIT = 180; // wait before I reveal myself
const int HIDETIME = 45; // how long to I take to hide

long last_event_time;
long start_time;
Move loMove;  // motion class

////////////////////////////////////////////////////////
//                      Functions
wakeup_t sensor_press_wakeup(wakeup_t data);
int init();
int hide();
int process_light(int piRaw);
int found();
Move loMove;  // motion class

////////////////////////////////////////////////////////
//                      MAIN()
///////////////////////////////////////////////////////
int main(int argc, char *argv[]) 
{
  
  int  liStartTime = sys_time/1000;
  int liResults;

  cputs("H S 2.2");            // version number  
  delay(3000);
  loMove.forward();

  while(1) //infinit loop 
    {
    loMove.forward();

    wait_event(&sensor_press_wakeup,0);
  
    /////////////////////////////
    // check if time to hide
    if((sys_time/1000) > ( HIDETIME + liStartTime))
      {
	liResults = hide();
	if(liResults)  //found
	  {
	    found();  
	  }
	else  //not found
	  {
	    //do something
	  }// else not found

	// reset time
	liStartTime = sys_time/1000;
      }// hide time expired
    else
      {
	///////////////////////////
	// what sensor was hit? 
	if(SENSOR_1<0xf000)
	  {
	    loMove.back_up();
	    delay(750);
	    loMove.spin(LEFT,750);              
	  }//if SENSOR_1 
	else if(SENSOR_2 < 0xf000)
	  {
	    loMove.back_up();
	    delay(750);
	    loMove.spin(RIGHT,750);    
	  }// close else Sensor_1
      }// close else > time
    }// close while(1)
 return 0;
}// close main


/////////////////////////////////////////////////
// int found();
// what to do if found when hiding
// example: liResults = found();
int found()
{
  lcd_refresh();
  cputs("fnd  ");

  //back up
  loMove.back_up();
  delay(500);

  // spin
  loMove.spin(RIGHT,2000);
  
  return 0;
}// close found

//////////////////////////////////////////////////
// int process_light(int raw)
// gives the light value in a percent
// example: liResults = process_light(SENSOR_2);
int process_light(int piRaw)
{
  int liRetVal;

  int start = ((piRaw/1000) + 34)* 100;
  int percent = (start / 58);

  liRetVal =  percent;
 
  if(liRetVal > 100)
    {
      liRetVal = 100;
    }// if RetVal>100
  
  return liRetVal;
}// process_light


//////////////////////////////////////////////////
// int hide();
// stops where it is and hides for three min or till found
// example: liResutls = hide();
int hide()
{
  int liReturnValue = 0;
  int liTime;
  int liDone;
  loMove.stop();
  cputs("hid");


  liTime = sys_time/1000; 
  liDone = FALSE;

  // No Light & less then wait time
  while(!liDone)
    {
      if(process_light(SENSOR_2) > 95 ) 
	{
	  liReturnValue = TRUE;
	  liDone = TRUE;
	}
      if((sys_time/1000) > (liTime + MAXWAIT))
	{
	  liReturnValue= FALSE;
	  liDone = TRUE;
	}//

    }// close while
  
return liReturnValue;
}// clos hide


/////////////////////////////////////////////////
// int init();
// reset all needed vars
// example: init();
int init()
{
  last_event_time = sys_time;
 
  return 0;
}// close init

////////////////////////////////////////////
// wakeup_t sensor_press_wakeup(wakeup_t data) 
// checks which sensor is hit
// example: wakeup_t = sensor_press_wakeup();
wakeup_t sensor_press_wakeup(wakeup_t data) 
{
      lcd_refresh();
      return SENSOR_1<0xf000 || SENSOR_3<0xf000;     
}// close sensor_press_wakeup



Other Journal Entries

RELATED
Carpet Moth
Jim Garvin
PC Rover
Jim Garvin
PanoBot
Philippe Hurbain
Snuf
Jaap Havinga
Bi-Ped Project
Jaap Havinga.
Bug Bot
Dennis Clark
My Robot
Matt Minnis
Hide and Seek Lego
Jim Garvin
SIPOC
Jeff Clayton
Scorch
Jeff Clayton
Body Logger
Chris Box
R.U.B.E.N.
Rob Roy