EmperorPhoenix

BluetoothTexting

Jun 3rd, 2016
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.49 KB | None | 0 0
  1. //ASCII
  2. //SPACE-32
  3. //UNDERSCORE-95
  4.  
  5. // include the library code:
  6. #include <LiquidCrystal.h>
  7. #include <SoftwareSerial.h>
  8.  
  9. const int joy1X = 0;
  10. const int joy1Y = 2;
  11. const int joy2X = 4;
  12. const int joy2Y = 6;
  13.  
  14. const int buttonSpace = 46;
  15. const int buttonEnter = 47;
  16. const int buttonSend = 44;
  17.  
  18. // initialize the library with the numbers of the interface pins
  19. LiquidCrystal lcd(48, 49, 50, 51, 52, 53); //lcd(12, 11, 5, 4, 3, 2);
  20. SoftwareSerial SerialBT(11, 12, 9600);
  21.  
  22. int qwe;
  23. int gloo;
  24. bool monom;
  25. char uSc = ((char)95);
  26.  
  27. char mes[16];
  28. char rec[16];
  29.  
  30. bool isSending=false;
  31.  
  32. void setup() {
  33.   qwe = 0;
  34.   gloo = 0;
  35.   monom = false;
  36.   // set up the LCD's number of columns and rows:
  37.   lcd.begin(16, 2);
  38.   // Print a message to the LCD.
  39.  
  40.   SerialBT.begin(9600);
  41.   Serial.begin(38400);
  42.    
  43.   mes[0] = uSc;
  44.   for (int q = 1; q < 16; q++) {
  45.     mes[q] = (char)32;
  46.   }
  47.   lcd.setCursor(0, 0);
  48.   lcd.print("ENTER - send");
  49.   lcd.setCursor(0, 1);
  50.   lcd.print("SPACE - receive");
  51.   bool hasChosen=false;
  52.   do{
  53.     delay(10);
  54.     if(digitalRead(buttonEnter)==HIGH){
  55.       isSending=true;
  56.       hasChosen=true;
  57.       lcd.clear();
  58.       lcd.setCursor(0, 0);
  59.       lcd.print("Sending");
  60.     }
  61.     if(digitalRead(buttonSpace)==HIGH){
  62.       isSending=false;
  63.       hasChosen=true;
  64.       lcd.clear();
  65.       lcd.setCursor(0, 0);
  66.       lcd.print("Receiving");
  67.     }
  68.   }while(!hasChosen);
  69.   delay(2000);
  70.   lcd.clear();
  71. }
  72.  
  73.  
  74. void loop() {
  75.   if(isSending){
  76.     lcd.clear();
  77.     // set the cursor to column 0, line 1
  78.     // (note: line 1 is the second row, since counting begins with 0):
  79.     lcd.setCursor(0, 1);
  80.     // print the number of seconds since reset:
  81.     lcd.print(mes);
  82.     lcd.setCursor(8, 0);
  83.     lcd.print((int)analogRead(8));
  84.     lcd.setCursor(0, 0);
  85.     lcd.write(potType());
  86.     //lcd.print(nunu);
  87.     delay(200);
  88.  
  89.     gloo++;
  90.     if (gloo % 5 == 0) {
  91.       monom = true;
  92.     }
  93.  
  94.     if (digitalRead(buttonSend) == HIGH) { //SEND
  95.       int err = sendMes();
  96.       while (err == 13) {
  97.         Serial.println("FAILED TO INITIALIZE RECIEVER");
  98.         sendMes();
  99.       }
  100.       while (err == 14) {
  101.         Serial.println("FAILED CHECKSUM TEST");
  102.         sendMes();
  103.       }
  104.     }
  105.     if (digitalRead(buttonEnter) == HIGH){
  106.       mes[qwe] = potType();
  107.       mes[qwe + 1] = uSc;
  108.       qwe++;
  109.     }
  110.     if(digitalRead(buttonSpace)==HIGH){
  111.       mes[qwe]=' ';
  112.       mes[qwe+1]=uSc;
  113.       qwe++;
  114.     }
  115.   }else{ //Rec
  116.     lcd.clear();
  117.     int erro;
  118.     Serial.println("ATTEMPTING TO RECIEVE MESSAGE");
  119.     while(erro!=30){
  120.       erro = recMes();
  121.     }
  122.     lcd.setCursor(0,1);
  123.     lcd.print("ENTER TO RESTART");
  124.     while(digitalRead(47)==LOW);
  125.   }
  126. }
  127.  
  128. char potType(){
  129.   char readr;
  130.   int ono = analogRead(8);
  131.   int non = map(ono, 0, 1030, 90, 64);
  132.   readr=(char)non;
  133.   return(readr);
  134. }
  135.  
  136. int sendMes() {
  137.   digitalWrite(5, HIGH);
  138.   Serial.println("CHECKING CONNECTION");
  139.   SerialBT.flush();
  140.   Serial.println("SENDING MESSAGE");
  141.   for(int i=0; i<16; i++){
  142.     SerialBT.write((char)mes[i]);
  143.     delay(1000);
  144.   }
  145.  
  146.   delay(1000);
  147.   digitalWrite(5, LOW);
  148. }
  149.  
  150. int recMes() {
  151.   Serial.println("WAITING FOR MESSAGE");
  152.   lcd.setCursor(0,1);
  153.   lcd.print("ENTER to Recieve");
  154.   while(digitalRead(buttonSpace)==LOW);
  155.   lcd.clear();
  156.   String recm = SerialBT.readString();
  157.   lcd.setCursor(0,0);
  158.   lcd.print(recm);  
  159.   return(30);
  160. }
  161.  
  162. //Change delay setups
  163.  
  164. //CHANGE EVERYTHING
  165. //USE THE REAL SERIAL PORTS
  166. //FIX CONNECTING AUTOMATICALLY?
Advertisement
Add Comment
Please, Sign In to add comment