const int turnOnMotorPin = 11;
const int controlMotorPin_1 = 12;
const int controlMotorPin_2 = 10;
const int turnMotor_1 = 9;
const int turnMotor_2 = 8;
const int echoSensor = 7;
const int triggerSensor = 6;
int motion = 1; //forward
int direction = 0; // straight
void setup() {
pinMode(controlMotorPin_1, OUTPUT);
pinMode(controlMotorPin_2, OUTPUT);
pinMode(turnOnMotorPin, OUTPUT);
pinMode(turnMotor_1, OUTPUT);
pinMode(turnMotor_2, OUTPUT);
pinMode(echoSensor,INPUT);
pinMode(triggerSensor,OUTPUT);
digitalWrite(turnOnMotorPin, HIGH);
delay(5000); // Some initial delay to place the car in the right location
}
void loop() {
long duration, inches;
digitalWrite(triggerSensor, LOW);
delayMicroseconds(2);
digitalWrite(triggerSensor, HIGH);
delayMicroseconds(10);
digitalWrite(triggerSensor, LOW);
pinMode(echoSensor, INPUT);
duration = pulseIn(echoSensor, HIGH);
inches = microsecondsToInches(duration);
int oldMotion = motion;
if(motion == 1 && inches > 15){
forward();
motion = 1;
}else
if(motion == 1 && inches <= 15){
reverse();
motion = 0;
}else if(motion == 0 && inches <= 30) {
reverse();
motion = 0;
}else if(motion == 0 && inches > 30){
forward();
motion = 1;
}else {
forward();
motion = 1;
}
if(oldMotion != motion){
direction = random(0, 2);
}
if(direction == 0){
goStragith();
}else if(direction == 1){
changeDirectionLeft();
}else {
changeDirectionRight();
}
delay(100);
}
void changeDirectionLeft(){
digitalWrite(turnMotor_1, HIGH);
digitalWrite(turnMotor_2, LOW);
}
void changeDirectionRight(){
digitalWrite(turnMotor_1, LOW);
digitalWrite(turnMotor_2, HIGH);
}
void goStragith(){
digitalWrite(turnMotor_1, LOW);
digitalWrite(turnMotor_2, LOW);
}
void stop(){
digitalWrite(controlMotorPin_1, LOW);
digitalWrite(controlMotorPin_2, LOW);
}
void forward(){
digitalWrite(controlMotorPin_1, HIGH);
digitalWrite(controlMotorPin_2, LOW);
}
void reverse(){
digitalWrite(controlMotorPin_1, LOW);
digitalWrite(controlMotorPin_2, HIGH);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
const int controlMotorPin_1 = 12;
const int controlMotorPin_2 = 10;
const int turnMotor_1 = 9;
const int turnMotor_2 = 8;
const int echoSensor = 7;
const int triggerSensor = 6;
int motion = 1; //forward
int direction = 0; // straight
void setup() {
pinMode(controlMotorPin_1, OUTPUT);
pinMode(controlMotorPin_2, OUTPUT);
pinMode(turnOnMotorPin, OUTPUT);
pinMode(turnMotor_1, OUTPUT);
pinMode(turnMotor_2, OUTPUT);
pinMode(echoSensor,INPUT);
pinMode(triggerSensor,OUTPUT);
digitalWrite(turnOnMotorPin, HIGH);
delay(5000); // Some initial delay to place the car in the right location
}
void loop() {
long duration, inches;
digitalWrite(triggerSensor, LOW);
delayMicroseconds(2);
digitalWrite(triggerSensor, HIGH);
delayMicroseconds(10);
digitalWrite(triggerSensor, LOW);
pinMode(echoSensor, INPUT);
duration = pulseIn(echoSensor, HIGH);
inches = microsecondsToInches(duration);
int oldMotion = motion;
if(motion == 1 && inches > 15){
forward();
motion = 1;
}else
if(motion == 1 && inches <= 15){
reverse();
motion = 0;
}else if(motion == 0 && inches <= 30) {
reverse();
motion = 0;
}else if(motion == 0 && inches > 30){
forward();
motion = 1;
}else {
forward();
motion = 1;
}
if(oldMotion != motion){
direction = random(0, 2);
}
if(direction == 0){
goStragith();
}else if(direction == 1){
changeDirectionLeft();
}else {
changeDirectionRight();
}
delay(100);
}
void changeDirectionLeft(){
digitalWrite(turnMotor_1, HIGH);
digitalWrite(turnMotor_2, LOW);
}
void changeDirectionRight(){
digitalWrite(turnMotor_1, LOW);
digitalWrite(turnMotor_2, HIGH);
}
void goStragith(){
digitalWrite(turnMotor_1, LOW);
digitalWrite(turnMotor_2, LOW);
}
void stop(){
digitalWrite(controlMotorPin_1, LOW);
digitalWrite(controlMotorPin_2, LOW);
}
void forward(){
digitalWrite(controlMotorPin_1, HIGH);
digitalWrite(controlMotorPin_2, LOW);
}
void reverse(){
digitalWrite(controlMotorPin_1, LOW);
digitalWrite(controlMotorPin_2, HIGH);
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
No comments:
Post a Comment