EmperorPhoenix

THE CODE IS NOT WORKING!!!

Jun 14th, 2016
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. //ASCII
  2. //SPACE-32
  3. //UNDERSCORE-95
  4.  
  5. // include the library code:
  6. #include <LiquidCrystal.h>
  7.  
  8. const int joy1X = 0;
  9. const int joy1Y = 2;
  10. const int joy2X = 4;
  11. const int joy2Y = 6;
  12.  
  13. const int buttonSpace = 46;
  14. const int buttonEnter = 47;
  15. const int buttonSend = 44;
  16.  
  17. const int BT_RX = 15;
  18. const int BT_TX = 14;
  19.  
  20. // initialize the library with the numbers of the interface pins
  21. LiquidCrystal lcd(48, 49, 50, 51, 52, 53); //lcd(12, 11, 5, 4, 3, 2);
  22.  
  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. int mesCharNum;
  33.  
  34. void setup() {
  35.   mesCharNum=0;
  36.   gloo = 0;
  37.   monom = false;
  38.   // set up the LCD's number of columns and rows:
  39.   lcd.begin(16, 2);
  40.   // Print a message to the LCD.
  41.  
  42.   Serial3.begin(9600);
  43.   Serial.begin(38400);
  44.    
  45.   mes[0] = uSc;
  46.   for (int q = 1; q < 16; q++) {
  47.     mes[q] = (char)32;
  48.   }
  49.   lcd.setCursor(0, 0);
  50.   lcd.print("ENTER - send");
  51.   lcd.setCursor(0, 1);
  52.   lcd.print("SPACE - receive");
  53.   bool hasChosen=false;
  54.   do{
  55.     delay(10);
  56.     if(digitalRead(buttonEnter)==HIGH){
  57.       isSending=true;
  58.       hasChosen=true;
  59.       lcd.clear();
  60.       lcd.setCursor(0, 0);
  61.       lcd.print("Sending");
  62.     }
  63.     if(digitalRead(buttonSpace)==HIGH){
  64.       isSending=false;
  65.       hasChosen=true;
  66.       lcd.clear();
  67.       lcd.setCursor(0, 0);
  68.       lcd.print("Receiving");
  69.     }
  70.   }while(!hasChosen);
  71.   delay(2000);
  72.   lcd.clear();
  73. }
  74.  
  75.  
  76. void loop() {
  77.   if(isSending){
  78.     lcd.clear();
  79.     // set the cursor to column 0, line 1
  80.     // (note: line 1 is the second row, since counting begins with 0):
  81.     lcd.setCursor(0, 1);
  82.     // print the number of seconds since reset:
  83.     lcd.print(mes);
  84.     lcd.setCursor(8, 0);
  85.     lcd.print((int)analogRead(8));
  86.     lcd.setCursor(0, 0);
  87.     lcd.write(potType());
  88.     //lcd.print(nunu);
  89.     delay(200);
  90.  
  91.     gloo++;
  92.     if (gloo % 5 == 0) {
  93.       monom = true;
  94.     }
  95.  
  96.     if (digitalRead(buttonSend) == HIGH) { //SEND
  97.       int err = sendMes();
  98.       while (err == 123) {
  99.         Serial.println("FAILED TO INITIALIZE RECIEVER");
  100.         err = sendMes();
  101.       }
  102.     }
  103.     if (digitalRead(buttonEnter) == HIGH){
  104.       mes[mesCharNum] = potType();
  105.       mes[mesCharNum + 1] = uSc;
  106.       mesCharNum++;
  107.     }
  108.     if(digitalRead(buttonSpace)==HIGH){
  109.       mes[mesCharNum]=' ';
  110.       mes[mesCharNum+1]=uSc;
  111.       mesCharNum++;
  112.     }
  113.    
  114.   }else{
  115.    
  116.     //Rec
  117.    
  118.     lcd.clear();
  119.     int erro;
  120.    
  121.     while(erro!=30){
  122.       erro = recMes();
  123.     }
  124.     lcd.setCursor(0,0);
  125.     lcd.print(rec);
  126.    
  127.     lcd.setCursor(0,1);
  128.     lcd.print("ENTER TO RESTART");
  129.     while(digitalRead(47)==LOW);
  130.   }
  131. }
  132.  
  133. char potType(){
  134.   char readr;
  135.   int ono = analogRead(8);
  136.   int non = map(ono, 0, 1030, 90, 64);
  137.   readr=(char)non;
  138.   return(readr);
  139. }
  140.  
  141. int sendMes() {
  142.   Serial.println("FORMATTING MESSAGE");
  143.   if(mesCharNum<16){
  144.     for(int i=mesCharNum-1; i<16; i++){
  145.       mes[i]=(char)32;
  146.     }
  147.   }
  148.  
  149.   Serial.println(mes);
  150.  
  151.   Serial.println("CHECKING CONNECTION");
  152.   Serial3.flush();
  153.   Serial3.write((char)35);
  154.   while(!(Serial3.available() > 0));
  155.   if((int)Serial3.read()==36){
  156.     Serial.println("CONNECTION SUCESSFUL");
  157.   }else{
  158.     Serial.println("CONNECTION FAILED - RETRYING");
  159.     return(123);
  160.   }
  161.  
  162.   Serial.print("SENDING MESSAGE: ");
  163.   Serial.println(mes);
  164.   for(int i=0; i<16; i++){
  165.     Serial.print("Sending char ");
  166.     Serial.println(i);
  167.     Serial3.write((char)mes[i]);
  168.     delay(1000);
  169.   }
  170.   Serial.print("SENT");
  171.   delay(1000);
  172. }
  173.  
  174. int recMes() {
  175.  
  176.   for(int i=0; i<16; i++){
  177.     rec[i]=0;
  178.   }
  179.   bool hasRec=false;
  180.  
  181.   while(!hasRec);
  182.   Serial.println("WAITING FOR MESSAGE");
  183.   if (Serial3.available() > 0) {
  184.     if((int)Serial3.read()==35){
  185.       Serial.println("CONNECTION SUCESSFUL - RECEIVING MESSAGE");
  186.  
  187.       for(int i=0; i<16; i++){
  188.         if (Serial3.available() > 0) {
  189.           rec[i] = Serial3.read();
  190.          
  191.           Serial.print("RECEIVED CHAR ");
  192.           Serial.print(i);
  193.           Serial.println(" OF 16");
  194.          
  195.           Serial.print("CHAR: ");
  196.           Serial.println((char)rec[i]);
  197.         }
  198.         else
  199.         {
  200.           i--;
  201.         }
  202.       }
  203.  
  204.       Serial.print("RECEIVED \"");
  205.       Serial.print(rec);
  206.       Serial.println("\"");
  207.     }
  208.   }else{
  209.     Serial.println("INCORRECT INIT VALUE - WAITING");
  210.   }
  211. }
Advertisement
Add Comment
Please, Sign In to add comment