Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <Servo.h>
- #include <Wire.h>
- #include <Adafruit_MotorShield.h>
- #include "utility/Adafruit_MS_PWMServoDriver.h"
- const int flights = 6;
- const int blights = 7;
- const int buzzer = 3 ;
- const int BTState = 2;
- const int servopin = 10;
- int i=0;
- int j=0;
- int state;
- int forwardpin = 4;
- int backwardpin = 5;
- Adafruit_MotorShield AFMS = Adafruit_MotorShield();
- Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
- Servo myservo;
- void setup() {
- AFMS.begin();
- pinMode(flights, OUTPUT);
- pinMode(blights, OUTPUT);
- pinMode(forwardpin, OUTPUT);
- pinMode(backwardpin, OUTPUT);
- myservo.attach(servopin);
- pinMode(BTState, INPUT);
- Serial.begin(9600);
- myMotor->setSpeed(150);
- myMotor->run(FORWARD);
- // turn on motor
- myMotor->run(RELEASE);
- }
- void loop() {
- //Stop car when connection lost or bluetooth disconnected
- if(digitalRead(BTState)==LOW) { state='S'; }
- //Save income data to variable 'state'
- if(Serial.available() > 0){
- state = Serial.read();
- }
- /***********************Forward****************************/
- //If state is equal with letter 'F', car will go forward!
- if (state == 'F')
- {
- myservo.write(90);
- Serial.print("Forward: ");
- digitalWrite(forwardpin, HIGH);
- }
- /***********************Backward****************************/
- //If state is equal with letter 'B', car will go backward
- else if (state == 'B')
- {
- myservo.write(90);
- Serial.print("Backwards: ");
- digitalWrite(backwardpin, HIGH);
- }
- /***************************Left*****************************/
- //If state is equal with letter 'L', wheels will turn left
- else if (state == 'L')
- {
- myservo.write(115);
- digitalWrite(forwardpin, LOW);
- digitalWrite(backwardpin, LOW);
- }
- /***************************Right*****************************/
- //If state is equal with letter 'R', wheels will turn right
- else if (state == 'R')
- {
- myservo.write(60);
- digitalWrite(forwardpin, LOW);
- digitalWrite(backwardpin, LOW);
- }
- /**********************Forward Left************************/
- //If state is equal with letter 'G', car will go forward left
- else if (state == 'G')
- {
- myservo.write(115);
- Serial.print("Forward Left: ");
- digitalWrite(forwardpin, HIGH);
- }
- /**********************Forward Right************************/
- //If state is equal with letter 'I', car will go forward right
- else if (state == 'I')
- {
- myservo.write(60);
- Serial.print("Forward Right: ");
- digitalWrite(forwardpin, HIGH);
- }
- /**********************Backward Left************************/
- //If state is equal with letter 'H', car will go backward left
- else if (state == 'H')
- {
- myservo.write(115);
- Serial.print("Backwards Left: ");
- digitalWrite(backwardpin, HIGH);
- }
- /**********************Backward Right************************/
- //If state is equal with letter 'J', car will go backward right
- else if (state == 'J')
- {
- myservo.write(65);
- Serial.print("Backwards Right: ");
- digitalWrite(backwardpin, HIGH);
- }
- /************************Lights Front*****************************/
- //If state is equal with letter 'W', turn leds on or of off
- else if (state == 'W')
- {
- digitalWrite(flights, HIGH);
- state='n';
- }
- else if (state == 'w')
- {
- digitalWrite(flights, LOW);
- state='n';
- }
- /************************Lights Back*****************************/
- //If state is equal with letter 'W', turn leds on or of off
- else if (state == 'U')
- {
- digitalWrite(blights, HIGH);
- state='n';
- }
- else if (state == 'u')
- {
- digitalWrite(blights, LOW);
- state='n';
- }
- /************************Stop*****************************/
- //If state is equal with letter 'S', stop the car
- else if (state == 'S')
- {
- digitalWrite(forwardpin, LOW);
- digitalWrite(backwardpin, LOW);
- }
- delay(50);
- }
RAW Paste Data