Max The Robot
Max is a four- wheel-drive, semi-autonomous robot. He is capable of driving around automatically without bumping into objects and operating via remote control. Newly added features include: A mode where the robot can give an alert upon entry into a room and a wired speaker for music as it moves around.Visit https://matthewamisi.wixsite.com/mattarts/portfolio for detailed info
Visit https://matthewamisi.wixsite.com/mattarts/portfolio for more info
#include <EEPROM.h>
#include <IRLib.h>
#include <AFMotor.h>
int frontlights = A1;
int backlights = A2;
int buzzer = A3;
int pingPin = A4; //setup pingpin as A4
long microsecondsToInches(long microseconds) {
  // According to Parallax's datasheet for the PING))), there are
  // 73.746 microseconds per inch (i.e. sound travels at 1130 feet per
  // second).  This gives the distance travelled by the ping, outbound
  // and return, so we divide by 2 to get the distance of the obstacle.
  // See: http://www.parallax.com/dl/docs/prod/acc/28015-PING-v1.3.pdf
  return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds) {
  // The speed of sound is 340 m/s or 29 microseconds per centimeter.
  // The ping travels out and back, so to find the distance of the
  // object we take half of the distance travelled.
  return microseconds / 29 / 2;
}
//ir variables
int RECV_PIN = A0;
IRrecv My_Receiver(RECV_PIN);
IRdecode My_Decoder;
unsigned int Buffer[RAWBUF];
//Motor variables
AF_DCMotor motor4(4,MOTOR34_64KHZ);
AF_DCMotor motor3(1,MOTOR34_64KHZ);
AF_DCMotor motor2(3,MOTOR34_64KHZ);
AF_DCMotor motor1(2,MOTOR34_64KHZ);
enum Dir{
  stoop,
  forward,
  backward,
  right,
  left,
};
static volatile enum Dir dir=stoop;
//motor movement functions
void motor_forward() {
 motor1.run(FORWARD);
 motor2.run(FORWARD);
 motor3.run(BACKWARD);
 motor4.run(BACKWARD);
}
 void motor_backward() {
 motor1.run(BACKWARD);
 motor2.run(BACKWARD);
 motor3.run(FORWARD);
 motor4.run(FORWARD);
}
void motor_left() {
 motor1.run(BACKWARD);
 motor2.run(FORWARD);
 motor3.run(FORWARD);
 motor4.run(BACKWARD);
}
void motor_right() {
 motor1.run(FORWARD);
 motor2.run(BACKWARD);
 motor3.run(BACKWARD);
 motor4.run(FORWARD);
}
void motor_stop(){
 motor1.run(RELEASE);
 motor2.run(RELEASE);
 motor3.run(RELEASE);
 motor4.run(RELEASE);
 
}
void setup()
{
 Serial.begin(9600);
 pinMode(frontlights, OUTPUT);
 pinMode(buzzer, OUTPUT);
 pinMode(backlights, OUTPUT);
 Serial.begin(9600);
 My_Receiver.enableIRIn(); // Start the receiver
 My_Decoder.UseExtnBuf(Buffer);
 motor4.setSpeed(255);
 motor3.setSpeed(255);
 motor2.setSpeed(255); 
 motor1.setSpeed(255);
}
void loop() {
  // put your main code here, to run repeatedly:
  long duration, inches, cm;
  // The PING))) is triggered by a HIGH pulse of 2 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  pinMode(pingPin, OUTPUT);
  digitalWrite(pingPin, LOW);
  delayMicroseconds(2);
  digitalWrite(pingPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(pingPin, LOW);
  // The same pin is used to read the signal from the PING))): a HIGH
  // pulse whose duration is the time (in microseconds) from the sending
  // of the ping to the reception of its echo off of an object.
  pinMode(pingPin, INPUT);
  duration = pulseIn(pingPin, HIGH);
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  Serial.print(inches);
  Serial.print("in, ");
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  delay(100);
   
   //manual code here
   if (My_Receiver.GetResults(&My_Decoder)){
   //Restart the receiver so it can be capturing another code
   //while we are working on decoding this one.
   My_Receiver.resume(); 
   My_Decoder.decode();
    if (My_Decoder.value==0xFFC837)
    {
      //foward motor code here
      motor_forward();
      digitalWrite(backlights, LOW);
    }
    else if (My_Decoder.value==0xFF2AD5)
    {
      //backward motor code here
      motor_backward();
      digitalWrite(backlights, HIGH);
    }
    else if (My_Decoder.value==0xFF48B7)
    {
      //right turn motor code here
      motor_right();
      digitalWrite(backlights, LOW);
    }
    else if (My_Decoder.value==0xFF8A75)
    {
      //left turn motor code here
      motor_left();
      digitalWrite(backlights, LOW);
    }
    else if (My_Decoder.value==0xFF0AF5)
    {
      //release motor code here
      motor_stop();
      digitalWrite(backlights, HIGH);
    }
    /////////////////////////////////////////////////////////////////////////
    else if (My_Decoder.value==0xFFE817)
    {
      digitalWrite(buzzer, HIGH);
      delay(800);
      digitalWrite(buzzer, LOW);
    }
    /////////////////////////////////////////////////////////////////////////
   else if (My_Decoder.value==0xFF8877)
    {
      digitalWrite(frontlights, HIGH);
    }
    else if (My_Decoder.value==0xFFA857)
    {
      digitalWrite(frontlights, LOW);
    }
   }
    /////////////////////////////////////////////////////////////////////////
    //Use if you want to check once but use while to repeat actions
    if (My_Receiver.GetResults(&My_Decoder)){
if (My_Decoder.value==0xFF807F) //The while sets up a loop and does this over n over
{
    //automatic code here
     if (cm<6)
     {
      delay(2000);
      motor_backward();
      Serial.println("backward");
      delay(2000);
      motor_stop();
      delay(900);
      motor_right();
      Serial.println("right");
      delay(2000);
      motor_stop();
      delay(900);
    }
    if (cm>6)
     {
      motor_forward();
      Serial.println("forward");
     }
    
}
   }
}













Comments