Skip to content

Instantly share code, notes, and snippets.

@chand1012
Last active March 18, 2019 18:53
Show Gist options
  • Save chand1012/15eb8bc7e3a786c02c72994073f32e06 to your computer and use it in GitHub Desktop.
Save chand1012/15eb8bc7e3a786c02c72994073f32e06 to your computer and use it in GitHub Desktop.
uses an ATTiny85 to drive a L293D driver
//L293D
//Motor A
#define motorPin1 5 // Pin 14 of L293
#define motorPin2 2 // Pin 10 of L293
//Motor B
#define motorPin3 3 // Pin 7 of L293
#define motorPin4 4 // Pin 2 of L293
//ping sensor
#define echoPin 0
#define trigPin 1
long duration, distance;
long randint;
void ping(){
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);
// Calculating the distance
distance= duration*0.034/2;
}
void forward(){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
void backward(){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
void left(){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, HIGH);
digitalWrite(motorPin3, HIGH);
digitalWrite(motorPin4, LOW);
}
void right(){
digitalWrite(motorPin1, HIGH);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, HIGH);
}
void stop(){
digitalWrite(motorPin1, LOW);
digitalWrite(motorPin2, LOW);
digitalWrite(motorPin3, LOW);
digitalWrite(motorPin4, LOW);
}
void setup(){
//Set pins as outputs
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(motorPin1, OUTPUT);
pinMode(motorPin2, OUTPUT);
pinMode(motorPin3, OUTPUT);
pinMode(motorPin4, OUTPUT);
randomSeed(analogRead(0));
}
void loop(){
ping();
randint = random(11);
if (distance<10){
stop();
delay(250);
backward();
delay(250);
if (randint>=5){
left();
delay(500);
} else {
right();
delay(500);
}
} else {
forward();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment