MaxXLive

Technikprojekt

Apr 26th, 2016
81
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #include <Servo.h>
  3. #include <Wire.h>
  4. #include <Adafruit_MotorShield.h>
  5. #include "utility/Adafruit_MS_PWMServoDriver.h"
  6.  
  7.  
  8.   const int flights  = 6;
  9.   const int blights  = 7;
  10.   const int buzzer = 3 ;  
  11.   const int BTState = 2;
  12.   const int servopin = 10;
  13.   int i=0;
  14.   int j=0;
  15.   int state;
  16.   int forwardpin = 4;
  17.   int backwardpin = 5;
  18.  
  19.   Adafruit_MotorShield AFMS = Adafruit_MotorShield();
  20.   Adafruit_DCMotor *myMotor = AFMS.getMotor(1);
  21.   Servo myservo;
  22.  
  23. void setup() {
  24.     AFMS.begin();
  25.     pinMode(flights, OUTPUT);
  26.     pinMode(blights, OUTPUT);
  27.     pinMode(forwardpin, OUTPUT);
  28.     pinMode(backwardpin, OUTPUT);
  29.     myservo.attach(servopin);
  30.      
  31.     pinMode(BTState, INPUT);    
  32.     Serial.begin(9600);
  33.  
  34.     myMotor->setSpeed(150);
  35.     myMotor->run(FORWARD);
  36.     // turn on motor
  37.     myMotor->run(RELEASE);
  38. }
  39.  
  40. void loop() {
  41.   //Stop car when connection lost or bluetooth disconnected
  42.      if(digitalRead(BTState)==LOW) { state='S'; }
  43.  
  44.   //Save income data to variable 'state'
  45.     if(Serial.available() > 0){    
  46.       state = Serial.read();  
  47.     }
  48.    
  49.      
  50.   /***********************Forward****************************/
  51.   //If state is equal with letter 'F', car will go forward!
  52.     if (state == 'F')
  53.     {
  54.       myservo.write(90);
  55.       Serial.print("Forward: ");
  56.       digitalWrite(forwardpin, HIGH);
  57.     }
  58.   /***********************Backward****************************/
  59.   //If state is equal with letter 'B', car will go backward
  60.     else if (state == 'B')
  61.     {
  62.       myservo.write(90);
  63.       Serial.print("Backwards: ");
  64.       digitalWrite(backwardpin, HIGH);
  65.     }
  66.  
  67.   /***************************Left*****************************/
  68.   //If state is equal with letter 'L', wheels will turn left
  69.     else if (state == 'L')
  70.     {
  71.       myservo.write(115);
  72.       digitalWrite(forwardpin, LOW);
  73.       digitalWrite(backwardpin, LOW);
  74.     }
  75.   /***************************Right*****************************/
  76.   //If state is equal with letter 'R', wheels will turn right
  77.     else if (state == 'R')
  78.     {
  79.       myservo.write(60);
  80.       digitalWrite(forwardpin, LOW);
  81.       digitalWrite(backwardpin, LOW);
  82.     }
  83.   /**********************Forward Left************************/
  84.   //If state is equal with letter 'G', car will go forward left
  85.     else if (state == 'G')
  86.     {
  87.       myservo.write(115);
  88.       Serial.print("Forward Left: ");
  89.       digitalWrite(forwardpin, HIGH);
  90.     }
  91.   /**********************Forward Right************************/
  92.   //If state is equal with letter 'I', car will go forward right
  93.     else if (state == 'I')
  94.     {
  95.       myservo.write(60);
  96.       Serial.print("Forward Right: ");
  97.       digitalWrite(forwardpin, HIGH);
  98.     }
  99.   /**********************Backward Left************************/
  100.   //If state is equal with letter 'H', car will go backward left
  101.     else if (state == 'H')
  102.     {
  103.       myservo.write(115);
  104.       Serial.print("Backwards Left: ");
  105.       digitalWrite(backwardpin, HIGH);
  106.     }
  107.   /**********************Backward Right************************/
  108.   //If state is equal with letter 'J', car will go backward right
  109.     else if (state == 'J')
  110.     {
  111.       myservo.write(65);
  112.       Serial.print("Backwards Right: ");
  113.       digitalWrite(backwardpin, HIGH);
  114.     }
  115.   /************************Lights Front*****************************/
  116.   //If state is equal with letter 'W', turn leds on or of off
  117.     else if (state == 'W')
  118.     {
  119.       digitalWrite(flights, HIGH);
  120.       state='n';
  121.     }
  122.     else if (state == 'w')
  123.     {
  124.       digitalWrite(flights, LOW);
  125.       state='n';
  126.     }
  127.     /************************Lights Back*****************************/
  128.   //If state is equal with letter 'W', turn leds on or of off
  129.     else if (state == 'U')
  130.     {
  131.       digitalWrite(blights, HIGH);
  132.       state='n';
  133.     }
  134.     else if (state == 'u')
  135.     {
  136.       digitalWrite(blights, LOW);
  137.       state='n';
  138.     }
  139.   /************************Stop*****************************/
  140.   //If state is equal with letter 'S', stop the car
  141.     else if (state == 'S')
  142.     {
  143.         digitalWrite(forwardpin, LOW);
  144.         digitalWrite(backwardpin, LOW);
  145.     }
  146.     delay(50);    
  147. }
RAW Paste Data