Sunteți pe pagina 1din 4

sparkfun / SIK-Guide-Code

Code Issues 4 Pull requests 3 Projects 0 Pulse

SIK_Circuit_5C-AutonomousRobot / SIK_Circuit_5C-AutonomousRobot.ino

/*
SparkFun Inventor’s Kit
Circuit 5C - Autonomous Robot
This robot will drive around on its own and react to obstacles by bac
This sketch was adapted from one of the activities in the SparkFun Gu
Check out the rest of the book at
https://www.sparkfun.com/products/14326
This sketch was written by SparkFun Electronics, with lots of help fr
This code is completely free for any use.
View circuit diagram and instructions at: https://learn.sparkfun.com/
Download drawings and code at: https://github.com/sparkfun/SIK-Guide-
*/

//the right motor will be controlled by the motor A pins on the motor
const int AIN1 = 13; //control pin 1 on the motor driver fo
const int AIN2 = 12; //control pin 2 on the motor driver f
const int PWMA = 11; //speed control pin on the motor driv

//the left motor will be controlled by the motor B pins on the motor
const int PWMB = 10; //speed control pin on the motor drive
const int BIN2 = 9; //control pin 2 on the motor driver for
const int BIN1 = 8; //control pin 1 on the motor driver for

//distance variables
const int trigPin = 6;
const int echoPin = 5;

int switchPin = 7; //switch to turn the robot on and off

float distance = 0; //variable to store the distance measu

//robot behaviour variables


int backupTime = 300; //amount of time that the robot will
int turnTime = 200; //amount that the robot will turn onc

/********************************************************************
void setup()
{
  pinMode(trigPin, OUTPUT); //this pin will send ultrasonic pul
  pinMode(echoPin, INPUT); //this pin will sense when the puls

  pinMode(switchPin, INPUT_PULLUP); //set this as a pullup to sense

  //set the motor contro pins as outputs


  pinMode(AIN1, OUTPUT);
  pinMode(AIN2, OUTPUT);
  pinMode(PWMA, OUTPUT);

  pinMode(BIN1, OUTPUT);
  pinMode(BIN2, OUTPUT);
  pinMode(PWMB, OUTPUT);

  Serial.begin(9600); //begin serial communicat


  Serial.print("To infinity and beyond!"); //test the serial connect
}

/********************************************************************
void loop()
{
  //DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
  distance = getDistance();

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" in"); // print the units

  if(digitalRead(switchPin) == LOW){ //if the on switch is flipped

    if(distance < 10){ //if an object is detected


      //back up and turn
      Serial.print(" ");
      Serial.print("BACK!");

      //stop for a moment


      rightMotor(0);
      leftMotor(0);
      delay(200);

      //back up
      rightMotor(-255);
      leftMotor(-255);
      delay(backupTime);

      //turn away from obsticle


      rightMotor(255);
      leftMotor(-255);
      delay(turnTime);

    }else{ //if no obstacle is detected drive


      Serial.print(" ");
      Serial.print("Moving...");

      rightMotor(255);
      leftMotor(255);
    }
  } else{ //if the switch is off then stop

      //stop the motors


      rightMotor(0);
      leftMotor(0);
  }

  delay(50); //wait 50 milliseconds between read


}

/********************************************************************
void rightMotor(int motorSpeed) //function for
{
  if (motorSpeed > 0) //if the motor
  {
    digitalWrite(AIN1, HIGH); //set pin 1 to
    digitalWrite(AIN2, LOW); //set pin 2 to
  }
  else if (motorSpeed < 0) //if the motor
  {
    digitalWrite(AIN1, LOW); //set pin 1 to
    digitalWrite(AIN2, HIGH); //set pin 2 to
  }
  else //if the motor
  {
    digitalWrite(AIN1, LOW); //set pin 1 to
    digitalWrite(AIN2, LOW); //set pin 2 to
  }
  analogWrite(PWMA, abs(motorSpeed)); //now that the
}

/********************************************************************
void leftMotor(int motorSpeed) //function for
{
  if (motorSpeed > 0) //if the motor
  {
    digitalWrite(BIN1, HIGH); //set pin 1 to
    digitalWrite(BIN2, LOW); //set pin 2 to
  }
  else if (motorSpeed < 0) //if the motor
  {
    digitalWrite(BIN1, LOW); //set pin 1 to
    digitalWrite(BIN2, HIGH); //set pin 2 to
  }
  else //if the motor
  {
    digitalWrite(BIN1, LOW); //set pin 1 to
    digitalWrite(BIN2, LOW); //set pin 2 to
  }
  analogWrite(PWMB, abs(motorSpeed)); //now that the
}

/********************************************************************
//RETURNS THE DISTANCE MEASURED BY THE HC-SR04 DISTANCE SENSOR
float getDistance()
{
  float echoTime; //variable to store the time it t
  float calcualtedDistance; //variable to store the distance

  //send out an ultrasonic pulse that's 10ms long


  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  echoTime = pulseIn(echoPin, HIGH); //use the pulsein command t


                                          //pulse to bounce back to t

  calcualtedDistance = echoTime / 148.0; //calculate the distance of

  return calcualtedDistance; //send back the distance th


}

Desktop version

S-ar putea să vă placă și