safwan092

ArduDroid-HC-05

Dec 19th, 2017
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.45 KB | None | 0 0
  1. /*
  2.  PROJECT: ArduDroid
  3.  PROGRAMMER: Hazim Bitar (techbitar at gmail dot com)
  4.  DATE: Oct 31, 2013
  5.  FILE: ardudroid.ino
  6.  LICENSE: Public domain
  7. */
  8.  
  9. #define START_CMD_CHAR '*'
  10. #define END_CMD_CHAR '#'
  11. #define DIV_CMD_CHAR '|'
  12. #define CMD_DIGITALWRITE 10
  13. #define CMD_ANALOGWRITE 11
  14. #define CMD_TEXT 12
  15. #define CMD_READ_ARDUDROID 13
  16. #define MAX_COMMAND 20  // max command number code. used for error checking.
  17. #define MIN_COMMAND 10  // minimum command number code. used for error checking.
  18. #define IN_STRING_LENGHT 40
  19. #define MAX_ANALOGWRITE 255
  20. #define PIN_HIGH 3
  21. #define PIN_LOW 2
  22.  
  23. String inText;
  24.  
  25. void setup() {
  26.   Serial.begin(9600);
  27.   Serial.println("ArduDroid 0.12 Alpha by TechBitar (2013)");
  28.   Serial.flush();
  29. }
  30.  
  31. void loop()
  32. {
  33.   Serial.flush();
  34.   int ard_command = 0;
  35.   int pin_num = 0;
  36.   int pin_value = 0;
  37.  
  38.   char get_char = ' ';  //read serial
  39.  
  40.   // wait for incoming data
  41.   if (Serial.available() < 1) return; // if serial empty, return to loop().
  42.  
  43.   // parse incoming command start flag
  44.   get_char = Serial.read();
  45.   if (get_char != START_CMD_CHAR) return; // if no command start flag, return to loop().
  46.  
  47.   // parse incoming command type
  48.   ard_command = Serial.parseInt(); // read the command
  49.  
  50.   // parse incoming pin# and value  
  51.   pin_num = Serial.parseInt(); // read the pin
  52.   pin_value = Serial.parseInt();  // read the value
  53.  
  54.   // 1) GET TEXT COMMAND FROM ARDUDROID
  55.   if (ard_command == CMD_TEXT){  
  56.     inText =""; //clears variable for new input  
  57.     while (Serial.available())  {
  58.       char c = Serial.read();  //gets one byte from serial buffer
  59.       delay(5);
  60.       if (c == END_CMD_CHAR) { // if we the complete string has been read
  61.         // add your code here
  62.         break;
  63.       }              
  64.       else {
  65.         if (c !=  DIV_CMD_CHAR) {
  66.           inText += c;
  67.           delay(5);
  68.         }
  69.       }
  70.     }
  71.   }
  72.  
  73.   // 2) GET digitalWrite DATA FROM ARDUDROID
  74.   if (ard_command == CMD_DIGITALWRITE){  
  75.     if (pin_value == PIN_LOW) pin_value = LOW;
  76.     else if (pin_value == PIN_HIGH) pin_value = HIGH;
  77.     else return; // error in pin value. return.
  78.     set_digitalwrite( pin_num,  pin_value);  // Uncomment this function if you wish to use
  79.     return;  // return from start of loop()
  80.   }
  81.  
  82.   // 3) GET analogWrite DATA FROM ARDUDROID
  83.   if (ard_command == CMD_ANALOGWRITE) {  
  84.     analogWrite(  pin_num, pin_value );
  85.     // add your code here
  86.     return;  // Done. return to loop();
  87.   }
  88.  
  89.   // 4) SEND DATA TO ARDUDROID
  90.   if (ard_command == CMD_READ_ARDUDROID) {
  91.     // char send_to_android[] = "Place your text here." ;
  92.     // Serial.println(send_to_android);   // Example: Sending text
  93.     Serial.print(" Analog 0 = ");
  94.     Serial.println(analogRead(A0));  // Example: Read and send Analog pin value to Arduino
  95.     return;  // Done. return to loop();
  96.   }
  97. }
  98.  
  99. // 2a) select the requested pin# for DigitalWrite action
  100. void set_digitalwrite(int pin_num, int pin_value)
  101. {
  102.   switch (pin_num) {
  103.   case 13:
  104.     pinMode(13, OUTPUT);
  105.     digitalWrite(13, pin_value);  
  106.     // add your code here      
  107.     break;
  108.   case 12:
  109.     pinMode(12, OUTPUT);
  110.     digitalWrite(12, pin_value);  
  111.     // add your code here      
  112.     break;
  113.   case 11:
  114.     pinMode(11, OUTPUT);
  115.     digitalWrite(11, pin_value);        
  116.     // add your code here
  117.     break;
  118.   case 10:
  119.     pinMode(10, OUTPUT);
  120.     digitalWrite(10, pin_value);        
  121.     // add your code here
  122.     break;
  123.   case 9:
  124.     pinMode(9, OUTPUT);
  125.     digitalWrite(9, pin_value);        
  126.     // add your code here
  127.     break;
  128.   case 8:
  129.     pinMode(8, OUTPUT);
  130.     digitalWrite(8, pin_value);        
  131.     // add your code here
  132.     break;
  133.   case 7:
  134.     pinMode(7, OUTPUT);
  135.     digitalWrite(7, pin_value);        
  136.     // add your code here
  137.     break;
  138.   case 6:
  139.     pinMode(6, OUTPUT);
  140.     digitalWrite(6, pin_value);        
  141.     // add your code here
  142.     break;
  143.   case 5:
  144.     pinMode(5, OUTPUT);
  145.     digitalWrite(5, pin_value);
  146.     // add your code here      
  147.     break;
  148.   case 4:
  149.     pinMode(4, OUTPUT);
  150.     digitalWrite(4, pin_value);        
  151.     // add your code here
  152.     break;
  153.   case 3:
  154.     pinMode(3, OUTPUT);
  155.     digitalWrite(3, pin_value);        
  156.     // add your code here
  157.     break;
  158.   case 2:
  159.     pinMode(2, OUTPUT);
  160.     digitalWrite(2, pin_value);
  161.     // add your code here      
  162.     break;      
  163.     // default:
  164.     // if nothing else matches, do the default
  165.     // default is optional
  166.   }
  167. }
Add Comment
Please, Sign In to add comment