/* sample2.c, random IC program */
/*  */
/* function: waits for a switch to be pressed and released, then */
/*           drives forward (turning two motors on at some power, */
/*           so this is a tank-style robot with a motor or each side) */
/*           , then stops when the button is pressed again.   */
/*           uses a funciton for abstraction                  */

 
/*  plug a digital switch into port 7 to turn on/off */

#define powerswitch 7

#define leftmotor 0
#define rightmotor 1

float maxspeed = 90.0;   /* tweak according to battery level, etc... */

void main()
{

  printf("Hi, push my button.\n");
  
  while (digital(powerswitch)==0)
    ;                                 /* empty while... waits here
                                         until powerswitch==1 */

  printf("Let's go!\n");

  while (digital(powerswitch)==1)
     ;                                /* waits until switch is released
                                         to start moving so there aren't
                                         any false positives in the
                                         below check of powerswitch */

  while (digital(powerswitch)==0)     /* loop until switch pressed again */
    {
      go_forward((int) maxspeed);     /* call sub go_forward, 
                                         cast maxspeed into an int */
    }
  
  off(leftmotor);
  off(rightmotor);
  
  printf("\n\nGoodbye.\n");
  
}


void go_forward(int tspeed)       
{
  motor(leftmotor,tspeed);
  motor(rightmotor,tspeed);
}





