Robot wireless mode select

Hi, I have built and programed a robot that follows me around, what i now need to do is build a wireless controller that i can use to toggle it between the modes "Follow Me" and "Stay Put". Could i build a simple wireless controller using a single pole double throw switch and these? 433Mhz RF Decoder Transmitter With Receiver Module Kit For ARM MCU Wireless Geekcreit for Arduino - products that work with official Arduino boards Sale - Banggood USA I can't use IR because that would interfere with the current programing which is this

#include <Wire.h>
const int pingPin = 11;
unsigned int duration, inches;
int LeftF= 5;
int RightF= 2;
int LeftB= 3;
int RightB= 4;

/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.

 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);             


  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

Go here for more info: Interpreting sensor input for robot. - Sensors - Arduino Forum

If it is something as simple as a toggle switch, then you may want to get one of these. It still might be a little over kill, but four buttons should allow you to add more features than just follow and stay. 315MHz Wireless remote + receiver

Thanks, this is just what i need, now does the transmitter keep transmitting the last button that was pressed or do i need to hold down the button to keep transmitting the signal? Also I'm already using the 5V pin on my arduino for the ultrasonic sensor so do i hook the receiver up in series or parallel with the ultra sonic sensor. Also one final question the out put pins D0, D1, D2, and D3 on the receiver go the the digital pins on the arduino not the analog pins, right?

Thanks, this is just what i need, now does the transmitter keep transmitting the last button that was pressed or do i need to hold down the button to keep transmitting the signal?

It should be, you press it and it sends a signal then once you let go, the signal stops sending.

Also I'm already using the 5V pin on my arduino for the ultrasonic sensor so do i hook the receiver up in series or parallel with the ultra sonic sensor.

I do not know the power requirements of the receiver, but the 5V pin should be enough to power both.

Also one final question the out put pins D0, D1, D2, and D3 on the receiver go the the digital pins on the arduino not the analog pins, right?

Either or, analog pins can work as digital pins, but digital pins cannot read analog signals. They can only produce PWM signals, which are digital representations of an analog signal.

Could you direct me towards one of those that keeps on transmitting the last command pressed? I need the signal to keep broadcasting so that as soon is i release the "Stay put" button the robot won't start following me till i press the "Follow me" button. I'm most likley going to modify my code like this:

#include <Wire.h>
const int pingPin = 11;
unsigned int duration, inches;
int LeftF= 1;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
int Stay = 5;
int Follow = 6

/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.
 
 Released into the public domain.
 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  pinMode(Stay,INPUT);
  pinMode(Follow,INPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);		             


  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5 && Follow == HIGH)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9 && Follow == HIGH)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12 && Follow == HIGH)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3 && Follow == HIGH)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
 if (Stay = HIGH)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

Why did you modify a library? They should be separate, one is a library, the other is a sketch. The sketch includes the library to use is functions. What you have doesn't even compile.

Get them sorted out.

Pressing onward, you already have a way to control whether the robot is to follow or stay, now you just need to toggle it.
Since you already know your going to be using the digital pins to read from the receiver, you can have it read one pin to make Stay equal HIGH, and the other will make Stay equal LOW. It doesn't get any simpler then that.

if (Stay = HIGH)// something is missing here.
{
digitalWrite(LeftB,LOW);
digitalWrite(RightB,LOW);
digitalWrite(RightF,LOW);
digitalWrite(LeftF,LOW);
}

The code does comple, I've tested it myself. Any way are you saying i should modify my code like this:

#include <Wire.h>
const int pingPin = 11;
unsigned int duration, inches;
int LeftF= 5;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
int Stay = 5;
int Follow = 6;
char s = 0;

/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.
 Released into the public domain.
 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);		             

if (Stay == HIGH)
 {
s = 1;
 }

if (Follow == HIGH)
 {
s = 0;
 }

 if (s == 1)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }


  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5 && s == 0)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3 && s == 0)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

It didn't compile before because you were missing a semicolon on int Follow = 6, but now it does.

You need to read the pin, not compare the pin number to HIGH or LOW. It should be,

if(digitalRead(Stay) == HIGH) 
{
  digitalWrite(LeftB,LOW);
  digitalWrite(RightB,LOW);
  digitalWrite(RightF,LOW);
  digitalWrite(LeftF,LOW);
}

if(digitalRead(Follow) == HIGH) 
{
  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

If i do it that way the robot will stop following me when i let go of the Follow button. however if i use this method:

if(digitalRead(Stay) == HIGH)
 {
s = 1;
 }

if(digitalRead(Follow) == HIGH)
 {
s = 0;
 }

 if (s == 1)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }


  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5 && s == 0)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3 && s == 0)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

the robot will stay in it's selected mode even when that mode's respective button is released. It's because s will remain ether 1 if Stay is LOW or 0 if Follow is LOW.

Ok. If you want, you can make it work with a single button. Press it once, it stays; press it again, it follows you. That is done with a latch like you have except it would be written as:

if(digitalRead(Stay) == HIGH) //could be changed to Command instead of Stay
{
s = !s; // if the pin is HIGH, set to one and if the pin is HIGH again set back to zero. This will repeat per press.
}

Like this?:

#include <Wire.h>
const int pingPin = 11;
unsigned int duration, inches;
int LeftF= 5;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
int Stay = 5;
char s = 0;

/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.
 Released into the public domain.
 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);		             

  if(digitalRead(Stay) == HIGH)
 {
    s = !s; 
 }

  if (s == 1)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }


  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5 && s == 0)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3 && s == 0)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

Yea. Thats the way to do it with one button, but the receiver may send out a constant signal. So if you don't block out the constant signal then it will change the state of 's' constantly.

So the better way is:

boolean state = digitalRead(Stay) ;
if( state != lastState ) [b]// lastState must be set at the top of your code as false[/b]
 {
   if( state == HIGH )
   {
      lastState = state
      s = !s; 
    }
 }

You can use the regular two button method, if it is easier for you. But that is what you would need for a single button.

Better?

#include <Wire.h>
const int pingPin = 11;
unsigned int duration, inches;
int LeftF= 5;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
int Stay = 5;
char s = 0;
int lastState = false;
/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.
 Released into the public domain.
 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);		             

 boolean state = digitalRead(Stay) ;
 if( state != lastState ) 
 {
   if( state == HIGH )
   {
      lastState = state;
      s = !s; 
    }
 }

  if (s == 1)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }


  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5 && s == 0)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3 && s == 0)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

You can make lastState boolean instead of int , but yea, better.

So this code will work the way i want it to:

#include <Wire.h>
const int pingPin = 11;
unsigned int duration, inches;
int LeftF= 5;
int RightF= 2;
int LeftB= 3;
int RightB= 4;
int Stay = 5;
char s = 0;
boolean lastState = false;
/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared
 Released into the public domain.
 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode(RightF,OUTPUT);
  pinMode(LeftF,OUTPUT);
  pinMode(LeftB,OUTPUT);
  pinMode(RightB,OUTPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);		             

 boolean state = digitalRead(Stay) ;
 if( state != lastState ) 
 {
   if( state == HIGH )
   {
      lastState = state;
      s = !s; 
    }
 }

  if (s == 1)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }


  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5 && s == 0)
  {
    digitalWrite(RightF,HIGH);
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftB,LOW);
  }


  if (InfraredBall.Direction == 5 && inches > 12 && s == 0)
  {
    digitalWrite(LeftF,HIGH);
    digitalWrite(RightF,HIGH);
    digitalWrite(RightB,LOW);
    digitalWrite(LeftB,LOW);
  }  

  if (InfraredBall.Direction == 5 && inches < 3 && s == 0)
  {
    digitalWrite(LeftB,HIGH);
    digitalWrite(RightB,HIGH);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }

  if (InfraredBall.Direction == 5 && inches <= 12 && inches >= 3)
  {
    digitalWrite(LeftB,LOW);
    digitalWrite(RightB,LOW);
    digitalWrite(RightF,LOW);
    digitalWrite(LeftF,LOW);
  }
}

Yea, it looks ok. It may need some fine tuning when you actually get it going and you could probably use the NewPing library instead of the one you have now. The new ping library is more precise, but its just a suggestion.

The distance doesn't need to be that precise, so the code i have now will work just fine. I may use that library later though if i try to add more features to it. Any way thanks for the help, and later i might post a video and a few photos of the bot just so you can see what you helped create.

Ok cool, can't wait to see it.
I'm curious, does it just follow you or anything in its range?

Actually it follows a IR beacon i have on my backpack, the IR seeker sensor orients the bot towards the beacon and the Ultrasonic sensor measures the distance.

Hey i tested the remote control and the robot stays when i press the button the first time, but it won't continue following me if i press the button a second time. Any suggestions?

#include <Wire.h>
const int pingPin = 11;
unsigned int duration, inches;
int MotorFBL = 3;
int MotorFBR = 4;
int MotorL = 5;
int MotorR = 6;
int Stay = 7;
char s = 0;
boolean lastState = false;
/*
  IRSeeker.ino - A library/class for the HiTechnic IRSeeker V2 infrared sensor.
 Released into the public domain.
 */

struct InfraredResult
{
  byte Direction;
  byte Strength;
};

class InfraredSeeker
{
public:
  static void Initialize();
  static boolean Test();
  static void ReadACRaw(byte* buffer);
  static void ReadDCRaw(byte* buffer);
  static InfraredResult ReadAC();
  static InfraredResult ReadDC();
  static int DirectionAngle(byte Direction);
private:
  static InfraredResult PopulateValues(byte* buffer);
  static void ReadValues(byte OffsetAddress, byte* buffer);
  static const int Address = 0x10 / 2; //Divide by two as 8bit-I2C address is provided
};

void InfraredSeeker::Initialize()
{
  Wire.begin();
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x00);
  Wire.endTransmission();
  while(Wire.available() > 0)
    Wire.read();
}

boolean InfraredSeeker::Test()
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(0x08);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 16);
  char Manufacturer_Model[16];
  while(Wire.available() < 16);
  for(byte i=0; i < 16; i++)
  {
    Manufacturer_Model[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
  return strncmp(Manufacturer_Model, "HiTechncNewIRDir", 16)==0;
}

void InfraredSeeker::ReadValues(byte OffsetAddress, byte* buffer)
{
  Wire.beginTransmission(InfraredSeeker::Address);
  Wire.write(OffsetAddress);
  Wire.endTransmission();
  Wire.requestFrom(InfraredSeeker::Address, 6);
  while(Wire.available() < 6);
  for(byte i = 0; i < 6; i++)
  {
    buffer[i] = Wire.read();
  }
  while(Wire.available() > 0)
    Wire.read();
}

void InfraredSeeker::ReadACRaw(byte* buffer)
{
  ReadValues(0x49, buffer);
}

void InfraredSeeker::ReadDCRaw(byte* buffer)
{
  ReadValues(0x42, buffer);
}

InfraredResult InfraredSeeker::PopulateValues(byte* buffer)
{
  InfraredResult Data;
  Data.Direction = buffer[0];
  if(buffer[0] != 0)
  {
    if(buffer[0] % 2 == 0)
    {
      Data.Strength = (buffer[buffer[0] / 2] + buffer[buffer[0] / 2 + 1]) / 2;
    }
    else
    {
      Data.Strength = buffer[buffer[0] / 2 + 1];
    }
  }
  else
  {
    Data.Strength = 0;
  }
  return Data;
}

InfraredResult InfraredSeeker::ReadAC()
{
  byte buffer[6];
  ReadACRaw(buffer);
  return PopulateValues(buffer);
}

InfraredResult InfraredSeeker::ReadDC()
{
  byte buffer[6];
  ReadDCRaw(buffer);
  return PopulateValues(buffer);
}

int DirectionAngle(byte Direction)
{
  return Direction * 30 - 150;
}

void setup()
{
  pinMode (MotorFBL,OUTPUT);
  pinMode (MotorFBR,OUTPUT);
  pinMode (MotorL,OUTPUT);
  pinMode (MotorR,OUTPUT);
  pinMode(Stay,INPUT);
  Serial.begin(9600);
  Serial.println("HiTechnic IRSeeker V2");
  Serial.println();
  Serial.println();
  Serial.println("Dir\tAngle\tStrength");
  Serial.println();
  InfraredSeeker::Initialize();
}

void loop()
{   
  InfraredResult InfraredBall = InfraredSeeker::ReadAC();
  Serial.print(InfraredBall.Direction);
  Serial.print("\t");
  Serial.print(DirectionAngle(InfraredBall.Direction));
  Serial.print("\t");
  Serial.print(InfraredBall.Strength);
  Serial.println();
  delay(200); //optional

  pinMode(pingPin, OUTPUT);          
  digitalWrite(pingPin, LOW);        
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);       
  delayMicroseconds(5);              
  digitalWrite(pingPin, LOW);        
  pinMode(pingPin, INPUT);           
  duration = pulseIn(pingPin, HIGH); 
  inches = duration / 74 / 2;        
  Serial.println(inches);            
  delay(200);		             

 boolean state = digitalRead(Stay) ;
 if( state != lastState ) 
 {
   if( state == HIGH )
   {
      lastState = state;
      s = !s; 
    }
 }

  if (s == 1)
  {
    analogWrite(MotorR,0);
    analogWrite(MotorL,0);
    digitalWrite(MotorFBL,LOW);
    digitalWrite(MotorFBR,LOW);
  }

if (s == 0)
{
  if (InfraredBall.Direction >= 1 && InfraredBall.Direction < 5)
  {
    analogWrite(MotorR,255);
    analogWrite(MotorL,255);
    digitalWrite(MotorFBL,HIGH);
    digitalWrite(MotorFBR,LOW);
  }

  if (InfraredBall.Direction > 5 && InfraredBall.Direction <= 9)
  {
    analogWrite(MotorR,255);
    analogWrite(MotorL,255);
    digitalWrite(MotorFBL,LOW);
    digitalWrite(MotorFBR,HIGH);
 }


  if (InfraredBall.Direction == 5 && inches > 30)
  {
    analogWrite(MotorR,255);
    analogWrite(MotorL,255);
    digitalWrite(MotorFBL,LOW);
    digitalWrite(MotorFBR,LOW);
  }

  if (InfraredBall.Direction == 5 && inches < 9)
  {
    analogWrite(MotorR,255);
    analogWrite(MotorL,255);
    digitalWrite(MotorFBL,HIGH);
    digitalWrite(MotorFBR,HIGH);
  }

  if (InfraredBall.Direction == 5 && inches <= 30 && inches >= 9)
  {
    analogWrite(MotorR,0);
    analogWrite(MotorL,0);
    digitalWrite(MotorFBL,LOW);
    digitalWrite(MotorFBR,LOW); 
  }
   if (InfraredBall.Direction == 0)
  {
    analogWrite(MotorR,0);
    analogWrite(MotorL,0);
    digitalWrite(MotorFBL,LOW);
    digitalWrite(MotorFBR,LOW);
  }
 }
}