Advertisement
Guest User

Untitled

a guest
Apr 26th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.39 KB | None | 0 0
  1. /*
  2.  * Created by Vasilakis Michalis // 12-12-2014 ver.2
  3.  * Project: Control RC Car via Bluetooth with Android Smartphone
  4.  * More information at www.ardumotive.com
  5.  */
  6.  
  7. //L293 Connection  
  8.   const int motorA1  = 5;  // Pin  2 of L293
  9.   const int motorA2  = 6;  // Pin  7 of L293
  10.   const int motorB1  = 10; // Pin 10 of L293
  11.   const int motorB2  = 9;  // Pin 14 of L293
  12. //Leds connected to Arduino UNO Pin 12
  13.   const int lights  = 12;
  14. //Buzzer / Speaker to Arduino UNO Pin 3
  15.   const int buzzer = 3 ;  
  16. //Bluetooth (HC-06 JY-MCU) State pin on pin 2 of Arduino
  17.   const int BTState = 2;
  18. //Calculate Battery Level
  19.   const float maxBattery = 8.0;// Change value to your max battery voltage level!
  20.   int perVolt;                 // Percentage variable
  21.   float voltage = 0.0;         // Read battery voltage
  22.   int level;
  23. // Use it to make a delay... without delay() function!
  24.   long previousMillis = -1000*10;// -1000*10=-10sec. to read the first value. If you use 0 then you will take the first value after 10sec.  
  25.   long interval = 1000*10;       // interval at which to read battery voltage, change it if you want! (10*1000=10sec)
  26.   unsigned long currentMillis;   //unsigned long currentMillis;
  27. //Useful Variables
  28.   int i=0;
  29.   int j=0;
  30.   int state;
  31.   int vSpeed=200;     // Default speed, from 0 to 255
  32.  
  33. void setup() {
  34.     // Set pins as outputs:
  35.     pinMode(motorA1, OUTPUT);
  36.     pinMode(motorA2, OUTPUT);
  37.     pinMode(motorB1, OUTPUT);
  38.     pinMode(motorB2, OUTPUT);
  39.     pinMode(lights, OUTPUT);
  40.     pinMode(BTState, INPUT);    
  41.     // Initialize serial communication at 9600 bits per second:
  42.     Serial.begin(9600);
  43. }
  44.  
  45. void loop() {
  46.   //Stop car when connection lost or bluetooth disconnected
  47. //     if(digitalRead(BTState)==LOW) { state='S'; }
  48.  
  49.   //Save income data to variable 'state'
  50.     if(Serial.available() > 0){    
  51.       state = Serial.read();  
  52.     }
  53.  
  54.   //Change speed if state is equal from 0 to 4. Values must be from 0 to 255 (PWM)
  55.     if (state == '0'){
  56.       vSpeed=0;}
  57.     else if (state == '1'){
  58.       vSpeed=100;}
  59.     else if (state == '2'){
  60.       vSpeed=180;}
  61.     else if (state == '3'){
  62.       vSpeed=200;}
  63.     else if (state == '4'){
  64.       vSpeed=255;}
  65.      
  66.   /***********************Forward****************************/
  67.   //If state is equal with letter 'F', car will go forward!
  68.     if (state == 'F') {
  69.         analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
  70.         analogWrite(motorB1, 0);      analogWrite(motorB2, 0);
  71.     }
  72.   /**********************Forward Left************************/
  73.   //If state is equal with letter 'G', car will go forward left
  74.     else if (state == 'G') {
  75.         analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);  
  76.         analogWrite(motorB1, 200);    analogWrite(motorB2, 0);
  77.     }
  78.   /**********************Forward Right************************/
  79.   //If state is equal with letter 'I', car will go forward right
  80.     else if (state == 'I') {
  81.         analogWrite(motorA1, vSpeed); analogWrite(motorA2, 0);
  82.         analogWrite(motorB1, 0);      analogWrite(motorB2, 200);
  83.     }
  84.   /***********************Backward****************************/
  85.   //If state is equal with letter 'B', car will go backward
  86.     else if (state == 'B') {
  87.         analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed);
  88.         analogWrite(motorB1, 0);   analogWrite(motorB2, 0);
  89.     }
  90.   /**********************Backward Left************************/
  91.   //If state is equal with letter 'H', car will go backward left
  92.     else if (state == 'H') {
  93.         analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed);
  94.         analogWrite(motorB1, 200); analogWrite(motorB2, 0);
  95.     }
  96.   /**********************Backward Right************************/
  97.   //If state is equal with letter 'J', car will go backward right
  98.     else if (state == 'J') {
  99.         analogWrite(motorA1, 0);   analogWrite(motorA2, vSpeed);
  100.         analogWrite(motorB1, 0);   analogWrite(motorB2, 200);
  101.     }
  102.   /***************************Left*****************************/
  103.   //If state is equal with letter 'L', wheels will turn left
  104.     else if (state == 'L') {
  105.         analogWrite(motorA1, 0);   analogWrite(motorA2, 0);
  106.         analogWrite(motorB1, 200); analogWrite(motorB2, 0);
  107.     }
  108.   /***************************Right*****************************/
  109.   //If state is equal with letter 'R', wheels will turn right
  110.     else if (state == 'R') {
  111.         analogWrite(motorA1, 0);   analogWrite(motorA2, 0);
  112.         analogWrite(motorB1, 0);   analogWrite(motorB2, 200);      
  113.     }
  114.   /************************Lights*****************************/
  115.   //If state is equal with letter 'W', turn leds on or of off
  116.     else if (state == 'W') {
  117.       if (i==0){  
  118.          digitalWrite(lights, HIGH);
  119.          i=1;
  120.       }
  121.       else if (i==1){
  122.          digitalWrite(lights, LOW);
  123.          i=0;
  124.       }
  125.       state='n';
  126.     }
  127.   /**********************Horn sound***************************/
  128.   //If state is equal with letter 'V', play (or stop) horn sound
  129.     else if (state == 'V'){
  130.       if (j==0){  
  131.          tone(buzzer, 1000);//Speaker on
  132.          j=1;
  133.       }
  134.       else if (j==1){
  135.          noTone(buzzer);    //Speaker off
  136.          j=0;
  137.       }
  138.       state='n';  
  139.     }
  140.   /************************Stop*****************************/
  141.   //If state is equal with letter 'S', stop the car
  142.     else if (state == 'S'){
  143.         analogWrite(motorA1, 0);  analogWrite(motorA2, 0);
  144.         analogWrite(motorB1, 0);  analogWrite(motorB2, 0);
  145.     }
  146.   /***********************Battery*****************************/
  147.   //Read battery voltage every 10sec.
  148.     currentMillis = millis();
  149.     if(currentMillis - (previousMillis) > (interval)) {
  150.        previousMillis = currentMillis;
  151.        //Read voltage from analog pin A0 and make calibration:
  152.        voltage = (analogRead(A0)*5.015 / 1024.0)*11.132;
  153.        //Calculate percentage...
  154.        perVolt = (voltage*100)/ maxBattery;
  155.        if      (perVolt<=75)               { level=0; }
  156.        else if (perVolt>75 && perVolt<=80) { level=1; }    //        Battery level
  157.        else if (perVolt>80 && perVolt<=85) { level=2; }    //Min ------------------------   Max
  158.        else if (perVolt>85 && perVolt<=90) { level=3; }    //    | 0 | 1 | 2 | 3 | 4 | 5 | >
  159.        else if (perVolt>90 && perVolt<=95) { level=4; }    //    ------------------------
  160.        else if (perVolt>95)                { level=5; }  
  161.        Serial.println(level);    
  162.     }
  163.    
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement