Advertisement
Guest User

Untitled

a guest
Aug 13th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.84 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial bluetooth(2, 3); //RX, TX
  3.  
  4. #define START_CMD_CHAR '*'
  5. #define DIV_CMD_CHAR '|'
  6. #define END_CMD_CHAR '#'
  7. #define CMD_DIGITALWRITE 10
  8. #define CMD_ANALOGWRITE 11
  9. #define CMD_TEXT 12
  10. #define CMD_READ_ARDUDROID 13
  11. #define MAX_COMMAND 20 // max command number code. used for error checking.
  12. #define MIN_COMMAND 10 // minimum command number code. used for error checking.
  13. #define IN_STRING_LENGHT 40
  14. #define MAX_ANALOGWRITE 255
  15. #define PIN_HIGH 3
  16. #define PIN_LOW 2
  17. #define Pin_chanal0 5
  18. #define Pin_chanal1 6
  19. #define Pin_chanal2 7
  20. #define Pin_chanal3 8
  21. #define Pin_chanal4 9
  22.  
  23. String Text;
  24. char get_Star = ' ';
  25. int Read_command = 0;
  26. int pin_number_digital = 0;
  27. int pin_number_analog = 0;
  28. int pin_state = 0;
  29. int pin_value = 0;
  30. int pin_value_state = 0;
  31.  
  32. void setup() {
  33. Serial.begin(9600);
  34. while (!Serial) ;
  35. bluetooth.begin(9600);
  36. Serial.println("Speed-airflow-bluetooth");
  37.  
  38. }
  39. void loop() {
  40.  
  41. if (Serial.available()) { // Read serial
  42. bluetooth.write(Serial.read()); // bluetooth = Serial.read() data form blue send to
  43. }
  44.  
  45. if (bluetooth.available()) { // get blue to show in serialmonitor
  46. //Serial.write(bluetooth.read()); // print blue form serial
  47. //parse incoming command start flag output = *10|1|3# HIGH
  48. get_Star = bluetooth.read();
  49. }
  50.  
  51. if (get_Star != START_CMD_CHAR) {
  52. return;
  53. }
  54. else {
  55. // parse incoming command type 10|
  56. Read_command = bluetooth.parseInt(); // read the command
  57. Serial.println("Read_command:" + String(Read_command));
  58.  
  59. // 1) GET digitalWrite DATA FROM ARDUDROID *10|
  60. if (Read_command == CMD_DIGITALWRITE) {
  61. Serial.println("CMD_DIGITALWRITE");
  62. // digital
  63. //*10|1|3# HIGH
  64. //*10|1|2# LOW
  65.  
  66. // parse incoming pin# and value |1|
  67. pin_number_digital = bluetooth.parseInt(); // read the pin
  68. //Serial.println("Pin_number_digital:" + String(pin_number_digital));
  69.  
  70. // parse incoming pin# and value |3|#
  71. pin_state = bluetooth.parseInt(); // read the pin
  72. //Serial.println("Pin_state:" + String(pin_state));
  73.  
  74. if (pin_state == PIN_LOW) {
  75. //Serial.println("Pin_state LOW:" + String(pin_state));
  76. pin_value_state = LOW;
  77. }
  78. if (pin_state == PIN_HIGH) {
  79. //Serial.println("Pin_state HIGH:" + String(pin_state));
  80. pin_value_state = HIGH;
  81. }
  82. // send pin_number and pin_value to function
  83. //speed_run(pin_number_digital, pin_value_state);
  84. return;
  85. }
  86.  
  87. // 2) GET analogWrite DATA FROM ARDUDROID *11|
  88. if (Read_command == CMD_ANALOGWRITE) {
  89. Serial.println("CMD_ANALOGWRITE");
  90.  
  91. // analog
  92. //*11|2|93#
  93. //*11|2|92#
  94.  
  95. // parse incoming pin# and value |2|
  96. pin_number_analog = bluetooth.parseInt(); // read the pin
  97. Serial.println("Pin_number_analog:" + String(pin_number_analog));
  98.  
  99. // parse incoming pin# and value |93|#
  100. pin_value = bluetooth.parseInt(); // read the value
  101. Serial.println("Pin_value:" + String(pin_value));
  102.  
  103. //analogWrite( pin_num, pin_value );
  104. return; // Done. return to loop();
  105. }
  106.  
  107. // 3) GET TEXT COMMAND FROM ARDUDROID
  108. if (Read_command == CMD_TEXT) {
  109. Serial.println("CMD_TEXT");
  110.  
  111. // text
  112. //*12|ggggggg#
  113.  
  114. Text = ""; //clears variable for new input
  115. while (bluetooth.available()) {
  116. char text_bluetooth = bluetooth.read(); //gets one byte from serial buffer
  117. //Serial.println(text_bluetooth);
  118. delay(5);
  119. if (text_bluetooth != DIV_CMD_CHAR) { // div
  120. Text += text_bluetooth;
  121. delay(5);
  122. }
  123. else {
  124. if (text_bluetooth == END_CMD_CHAR) { // end
  125. break;
  126. }
  127. }
  128. }
  129. Serial.print("Text: " + Text);
  130. }
  131.  
  132. // 4) SEND DATA TO ARDUDROID
  133. if (Read_command == CMD_READ_ARDUDROID) {
  134. Serial.println("CMD_READ_ARDUDROID");
  135. //char send_to_android[] = "Place your text here." ;
  136. //bluetooth.print(send_to_android); // Example: Sending text
  137. return; // Done. return to loop();
  138. }
  139. }
  140. }
  141.  
  142. // 2) select the requested pin# for DigitalWrite action
  143. void speed_run(int pin_number, int pin_value) {
  144. switch (pin_number) {
  145.  
  146. default:
  147. //if nothing else matches, do the default
  148. //default is optional
  149. case 0:
  150. pinMode(Pin_chanal0, OUTPUT);
  151. digitalWrite(Pin_chanal0, pin_value);
  152. // add your code here
  153. break;
  154. case 1:
  155. pinMode(Pin_chanal1, OUTPUT);
  156. digitalWrite(Pin_chanal1, pin_value);
  157. // add your code here
  158. break;
  159. case 2:
  160. pinMode(Pin_chanal2, OUTPUT);
  161. digitalWrite(Pin_chanal2, pin_value);
  162. // add your code here
  163. break;
  164. case 3:
  165. pinMode(Pin_chanal3, OUTPUT);
  166. digitalWrite(Pin_chanal3, pin_value);
  167. // add your code here
  168. break;
  169. case 4:
  170. pinMode(Pin_chanal4, OUTPUT);
  171. digitalWrite(Pin_chanal4, pin_value);
  172. // add your code here
  173. break;
  174. }
  175. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement