Documente Academic
Documente Profesional
Documente Cultură
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;
/********************************************************************
void setup()
{
pinMode(trigPin, OUTPUT); //this pin will send ultrasonic pul
pinMode(echoPin, INPUT); //this pin will sense when the puls
pinMode(BIN1, OUTPUT);
pinMode(BIN2, OUTPUT);
pinMode(PWMB, OUTPUT);
/********************************************************************
void loop()
{
//DETECT THE DISTANCE READ BY THE DISTANCE SENSOR
distance = getDistance();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" in"); // print the units
//back up
rightMotor(-255);
leftMotor(-255);
delay(backupTime);
rightMotor(255);
leftMotor(255);
}
} else{ //if the switch is off then stop
/********************************************************************
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
Desktop version