Arduino Car
Arduino Car: Control little car using sensors or bluetooth:
// Assign Pins for Left Motor
int motorleftPin1 = 3; // pin 2 on L293D
int motorleftPin2 = 4; // pin 7 on L293D
int enableleftPin = 5; // pin 1 on L293D
//assign pins for right motor
int motorrightPin1 = 8; // pin 10 on L293D
int motorrightPin2 = 9; // pin 15 on L293D
int enablerightPin = 10; // pin 9 on L293D
int incomingByte = 0;
int velocity = 100;
//set up ultrasound pins on HC-SR04 Ultrasound Sensor and define variables
int echoPin=6;
int trigPin=7;//set up ultrasound pins on HC-SR04
int led = 13;//assign LED pin
int threshold = 25; //assign threshold distance
int duration, cm;
void setup() {
// set the pins you're using as outputs:
pinMode(motorleftPin1, OUTPUT);
pinMode(motorleftPin2, OUTPUT);
pinMode(enableleftPin, OUTPUT); // or analogWrite(enableleftPin, speed 0-255); to adjust speed
pinMode(motorrightPin1, OUTPUT);
pinMode(motorrightPin2, OUTPUT);
pinMode(enablerightPin, OUTPUT); // or analogWrite(enablerightPin, speed 0-255); to adjust speed
// set enablePins high so that motor can turn on; this can also be used as a speed control using the analogWrite command
// set speed
analogWrite(enableleftPin, velocity);
analogWrite(enablerightPin, velocity);
Serial.begin (9600); //open serial communications
}
void loop() {
if (Serial.available() > 0) //check for serial data
{
incomingByte = Serial.read(); // read the incoming byte:
Serial.print("I received: ");
Serial.println(incomingByte);
delay(10); // delay 10 milliseconds to allow serial update time
// if byte is equal to "105" or "i", go forward
if (incomingByte == 105){
forward();
delay(500);
}
// if byte is equal to "106" or "j", go left
else if (incomingByte == 106){
turnleft();
delay(500);
}
// if byte is equal to+ speed upt
else if (incomingByte == 43){
velocity= velocity + 10;
analogWrite(enableleftPin, velocity);
analogWrite(enablerightPin, velocity);
delay(500);
}
// if byte is equal to - , slow down
else if (incomingByte == 45){
velocity= velocity - 10;
analogWrite(enableleftPin, velocity);
analogWrite(enablerightPin, velocity);
delay(500);
}
// if byte is equal to
else if (incomingByte == 108){
turnright();
delay(500);
}
// if byte is equal to "107" or "k", go reverse
else if (incomingByte == 107){
backward();
delay(500);
}
// if byte= "/", stop both motors
else if (incomingByte== 47) {
halt();
delay(1000);
}
else {
delay(25);
}
}
}
void forward() { //define going forwards function
digitalWrite(motorleftPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorleftPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorrightPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorrightPin2, HIGH); // set pin 7 on L293D high
}
void backward() { //define going backwards function
digitalWrite(motorleftPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorleftPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorrightPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorrightPin2, LOW); // set pin 7 on L293D low
}
void turnleft() { //define going backwards function
digitalWrite(motorleftPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorleftPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorrightPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorrightPin2, LOW); // set pin 7 on L293D low
}
void turnright() { //define going backwards function
digitalWrite(motorleftPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorleftPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorrightPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorrightPin2, HIGH); // set pin 7 on L293D low
}
void halt() { //define going backwards function
digitalWrite(motorleftPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorleftPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorrightPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorrightPin2, LOW); // set pin 7 on L293D low
}
void getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite(trigPin, LOW);
duration= pulseIn(echoPin, HIGH);
cm= duration /29 /2;
delay(100);
}
- Magician Chassis ( https://www.sparkfun.com/products/12866 )
- L298N Motor Driver http://www.gearbest.com/development-boards/pp_22490.html Specs: http://www.geeetech.com/wiki/index.php/L298N_Motor_Driver_Board
- HC-SR04 http://www.gearbest.com/development-boards/pp_58067.html
- Bluetooth receiver Module: http://www.gearbest.com/development-boards/pp_22526.html
- directions: http://www.instructables.com/id/Arduino-AND-Bluetooth-HC-05-Connecting-easily/
// Assign Pins for Left Motor
int motorleftPin1 = 3; // pin 2 on L293D
int motorleftPin2 = 4; // pin 7 on L293D
int enableleftPin = 5; // pin 1 on L293D
//assign pins for right motor
int motorrightPin1 = 8; // pin 10 on L293D
int motorrightPin2 = 9; // pin 15 on L293D
int enablerightPin = 10; // pin 9 on L293D
int incomingByte = 0;
int velocity = 100;
//set up ultrasound pins on HC-SR04 Ultrasound Sensor and define variables
int echoPin=6;
int trigPin=7;//set up ultrasound pins on HC-SR04
int led = 13;//assign LED pin
int threshold = 25; //assign threshold distance
int duration, cm;
void setup() {
// set the pins you're using as outputs:
pinMode(motorleftPin1, OUTPUT);
pinMode(motorleftPin2, OUTPUT);
pinMode(enableleftPin, OUTPUT); // or analogWrite(enableleftPin, speed 0-255); to adjust speed
pinMode(motorrightPin1, OUTPUT);
pinMode(motorrightPin2, OUTPUT);
pinMode(enablerightPin, OUTPUT); // or analogWrite(enablerightPin, speed 0-255); to adjust speed
// set enablePins high so that motor can turn on; this can also be used as a speed control using the analogWrite command
// set speed
analogWrite(enableleftPin, velocity);
analogWrite(enablerightPin, velocity);
Serial.begin (9600); //open serial communications
}
void loop() {
if (Serial.available() > 0) //check for serial data
{
incomingByte = Serial.read(); // read the incoming byte:
Serial.print("I received: ");
Serial.println(incomingByte);
delay(10); // delay 10 milliseconds to allow serial update time
// if byte is equal to "105" or "i", go forward
if (incomingByte == 105){
forward();
delay(500);
}
// if byte is equal to "106" or "j", go left
else if (incomingByte == 106){
turnleft();
delay(500);
}
// if byte is equal to+ speed upt
else if (incomingByte == 43){
velocity= velocity + 10;
analogWrite(enableleftPin, velocity);
analogWrite(enablerightPin, velocity);
delay(500);
}
// if byte is equal to - , slow down
else if (incomingByte == 45){
velocity= velocity - 10;
analogWrite(enableleftPin, velocity);
analogWrite(enablerightPin, velocity);
delay(500);
}
// if byte is equal to
else if (incomingByte == 108){
turnright();
delay(500);
}
// if byte is equal to "107" or "k", go reverse
else if (incomingByte == 107){
backward();
delay(500);
}
// if byte= "/", stop both motors
else if (incomingByte== 47) {
halt();
delay(1000);
}
else {
delay(25);
}
}
}
void forward() { //define going forwards function
digitalWrite(motorleftPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorleftPin2, HIGH); // set pin 7 on L293D high
digitalWrite(motorrightPin1, LOW); // set pin 2 on L293D low
digitalWrite(motorrightPin2, HIGH); // set pin 7 on L293D high
}
void backward() { //define going backwards function
digitalWrite(motorleftPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorleftPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorrightPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorrightPin2, LOW); // set pin 7 on L293D low
}
void turnleft() { //define going backwards function
digitalWrite(motorleftPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorleftPin2, HIGH); // set pin 7 on L293D low
digitalWrite(motorrightPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorrightPin2, LOW); // set pin 7 on L293D low
}
void turnright() { //define going backwards function
digitalWrite(motorleftPin1, HIGH); // set pin 2 on L293D high
digitalWrite(motorleftPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorrightPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorrightPin2, HIGH); // set pin 7 on L293D low
}
void halt() { //define going backwards function
digitalWrite(motorleftPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorleftPin2, LOW); // set pin 7 on L293D low
digitalWrite(motorrightPin1, LOW); // set pin 2 on L293D high
digitalWrite(motorrightPin2, LOW); // set pin 7 on L293D low
}
void getDistance() {
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite (trigPin, HIGH);
delayMicroseconds (10);
digitalWrite(trigPin, LOW);
duration= pulseIn(echoPin, HIGH);
cm= duration /29 /2;
delay(100);
}