//Arduino Bluetooth Controlled Car//
// Before uploading the code you have to install the necessary library//
//AFMotor Library https://learn.adafruit.com/adafruit-motor-shield/library-install //
#include <AFMotor.h>
//initial motors pin
AF_DCMotor motor1(1, MOTOR12_1KHZ);
AF_DCMotor motor2(2, MOTOR12_1KHZ);
AF_DCMotor motor3(3, MOTOR34_1KHZ);
AF_DCMotor motor4(4, MOTOR34_1KHZ);
char command;
int frontlights=14;
int backlights=15;
int horn=16;
int extra=17;
int speedx=255;
void setup()
{
pinMode(frontlights,OUTPUT);
pinMode(backlights,OUTPUT);
pinMode(horn,OUTPUT);
pinMode(extra,OUTPUT);
digitalWrite(frontlights,LOW);
digitalWrite(backlights,LOW);
digitalWrite(horn,LOW);
digitalWrite(extra,LOW);
Serial.begin(9600); //Set the baud rate to your Bluetooth module.
Serial.print("Bluetooth Car Ready");
}
void loop(){
if(Serial.available() > 0){
command = Serial.read();
Stop(); //initialize with motors stoped
//Change pin mode only if new command is different from previous.
Serial.println(command);
switch(command){
case 'F': forward(); break;
case 'B': back(); break;
case 'L': left(); break;
case 'R': right(); break;
case 'G': fleft(); break;
case 'I': fright(); break;
case 'H': bleft(); break;
case 'J': bright(); break;
case 'W':
digitalWrite(frontlights,HIGH);
break;
case 'w':
digitalWrite(frontlights,LOW);
break;
case 'U':
digitalWrite(backlights,HIGH);
break;
case 'u':
digitalWrite(backlights,LOW);
break;
case 'V':
digitalWrite(horn,HIGH);
break;
case 'v':
digitalWrite(horn,LOW);
break;
case 'X':
digitalWrite(extra,HIGH);
break;
case 'x':
digitalWrite(extra,LOW);
break;
case '1': Serial.print("speed= 150");speedx=150; break;
case '2': Serial.print("speed= 160");speedx=160; break;
case '3': Serial.print("speed= 170");speedx=170; break;
case '4': Serial.print("speed= 180");speedx=180; break;
case '5': Serial.print("speed= 190");speedx=190; break;
case '6': Serial.print("speed= 200");speedx=200; break;
case '7': Serial.print("speed= 225");speedx=225; break;
case '8': Serial.print("speed= 235");speedx=235; break;
case '9': Serial.print("speed= 245");speedx=245; break;
case 'q': Serial.print("speed= 255");speedx=255; break;
}
}
}
void forward()
{
motor1.setSpeed(speedx); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(speedx); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(speedx);//Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(speedx);//Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void fright()
{
motor1.setSpeed(speedx-(speedx/2)); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(speedx-(speedx/2)); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(speedx);//Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(speedx);//Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void fleft()
{
motor1.setSpeed(speedx); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(speedx); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(speedx-(speedx/2));//Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(speedx-(speedx/2));//Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void back()
{
motor1.setSpeed(speedx); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(speedx); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(speedx); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(speedx); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
void bright()
{
motor1.setSpeed(speedx-(speedx/2)); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(speedx-(speedx/2)); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(speedx); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(speedx); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
void bleft()
{
motor1.setSpeed(speedx); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(speedx); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(speedx-(speedx/2)); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(speedx-(speedx/2)); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
void right()
{
motor1.setSpeed(speedx); //Define maximum velocity
motor1.run(BACKWARD); //rotate the motor anti-clockwise
motor2.setSpeed(speedx); //Define maximum velocity
motor2.run(BACKWARD); //rotate the motor anti-clockwise
motor3.setSpeed(speedx); //Define maximum velocity
motor3.run(FORWARD); //rotate the motor clockwise
motor4.setSpeed(speedx); //Define maximum velocity
motor4.run(FORWARD); //rotate the motor clockwise
}
void left()
{
motor1.setSpeed(speedx); //Define maximum velocity
motor1.run(FORWARD); //rotate the motor clockwise
motor2.setSpeed(speedx); //Define maximum velocity
motor2.run(FORWARD); //rotate the motor clockwise
motor3.setSpeed(speedx); //Define maximum velocity
motor3.run(BACKWARD); //rotate the motor anti-clockwise
motor4.setSpeed(speedx); //Define maximum velocity
motor4.run(BACKWARD); //rotate the motor anti-clockwise
}
void Stop()
{
motor1.setSpeed(0); //Define minimum velocity
motor1.run(RELEASE); //stop the motor when release the button
motor2.setSpeed(0); //Define minimum velocity
motor2.run(RELEASE); //rotate the motor clockwise
motor3.setSpeed(0); //Define minimum velocity
motor3.run(RELEASE); //stop the motor when release the button
motor4.setSpeed(0); //Define minimum velocity
motor4.run(RELEASE); //stop the motor when release the button
}
No comments:
Post a Comment