EmperorPhoenix

IRTexting

May 11th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.60 KB | None | 0 0
  1. //ASCII
  2. //SPACE-32
  3. //UNDERSCORE-95
  4.  
  5. // include the library code:
  6. #include <LiquidCrystal.h>
  7. #include <IRremote.h>
  8.  
  9.  
  10.  
  11. const int joy1X = 0;
  12. const int joy1Y = 2;
  13. const int joy2X = 4;
  14. const int joy2Y = 6;
  15.  
  16. const int buttonSpace = 46;
  17. const int buttonEnter = 47;
  18. const int buttonSend = 44;
  19.  
  20. const int IRRecPin = 45;
  21. const int IRSenPin = 43;
  22.  
  23. IRsend irsend;
  24. IRrecv irrecv(IRRecPin); // Receive on pin 45
  25. decode_results results;
  26.  
  27. // initialize the library with the numbers of the interface pins
  28. LiquidCrystal lcd(48, 49, 50, 51, 52, 53); //lcd(12, 11, 5, 4, 3, 2);
  29.  
  30. int qwe;
  31. int gloo;
  32. bool monom;
  33. char uSc = ((char)95);
  34.  
  35. char mes[16];
  36. char rec[16];
  37.  
  38. bool isSending=false;
  39.  
  40. void setup() {
  41.   qwe = 0;
  42.   gloo = 0;
  43.   monom = false;
  44.   // set up the LCD's number of columns and rows:
  45.   lcd.begin(16, 2);
  46.   // Print a message to the LCD.
  47.   Serial.begin(9600);
  48.   pinMode(7, INPUT);
  49.   pinMode(8, INPUT);
  50.   pinMode(9, INPUT);
  51.   mes[0] = uSc;
  52.   for (int q = 1; q < 16; q++) {
  53.     mes[q] = (char)32;
  54.   }
  55.   irrecv.enableIRIn(); // Start the receiver
  56.   lcd.setCursor(0, 0);
  57.   lcd.print("ENTER - send");
  58.   lcd.setCursor(0, 1);
  59.   lcd.print("SPACE - receive");
  60.   bool hasChosen=false;
  61.   do{
  62.     delay(10);
  63.     if(digitalRead(buttonEnter)==HIGH){
  64.       isSending=true;
  65.       hasChosen=true;
  66.       lcd.clear();
  67.       lcd.setCursor(0, 0);
  68.       lcd.print("Sending");
  69.     }
  70.     if(digitalRead(buttonSpace)==HIGH){
  71.       isSending=false;
  72.       hasChosen=true;
  73.       lcd.clear();
  74.       lcd.setCursor(0, 0);
  75.       lcd.print("Receiving");
  76.     }
  77.   }while(!hasChosen);
  78.   delay(2000);
  79.   lcd.clear();
  80. }
  81.  
  82.  
  83.  
  84.  
  85. void loop() {
  86.   if(isSending){
  87.     lcd.clear();
  88.     // set the cursor to column 0, line 1
  89.     // (note: line 1 is the second row, since counting begins with 0):
  90.     lcd.setCursor(0, 1);
  91.     // print the number of seconds since reset:
  92.     lcd.print(mes);
  93.     lcd.setCursor(8, 0);
  94.     lcd.print((int)analogRead(8));
  95.     lcd.setCursor(0, 0);
  96.     lcd.write(potType());
  97.     //lcd.print(nunu);
  98.     delay(200);
  99.  
  100.     gloo++;
  101.     if (gloo % 5 == 0) {
  102.       monom = true;
  103.     }
  104.  
  105.     if (digitalRead(buttonSend) == HIGH) { //SEND
  106.       int err = sendMes();
  107.       while (err == 13) {
  108.         Serial.println("FAILED TO INITIALIZE RECIEVER");
  109.         sendMes();
  110.       }
  111.       while (err == 14) {
  112.         Serial.println("FAILED CHECKSUM TEST");
  113.         sendMes();
  114.       }
  115.     }
  116.     if (digitalRead(buttonEnter) == HIGH){
  117.       mes[qwe] = potType();
  118.       mes[qwe + 1] = uSc;
  119.       qwe++;
  120.     }
  121.     if(digitalRead(buttonSpace)==HIGH){
  122.       mes[qwe]=' ';
  123.       mes[qwe+1]=uSc;
  124.       qwe++;
  125.     }
  126.   }else{ //Rec
  127.     lcd.clear();
  128.     lcd.setCursor(0,0);
  129.     lcd.print("WAITING");
  130.     Serial.println("WAITING FOR INIT CODE");
  131.     while (!irrecv.decode(&results));
  132.     Serial.println("CODE RECIEVED");
  133.     lcd.clear();
  134.     int erro;
  135.     Serial.println("ATTEMPTING TO RECIEVE MESSAGE");
  136.     while(erro!=30){
  137.       erro = recMes();
  138.     }
  139.     lcd.setCursor(1,0);
  140.     lcd.print("ENTER TO RESTART");
  141.     while(digitalRead(47)==LOW);
  142.   }
  143. }
  144.  
  145. char potType(){
  146.   char readr;
  147.   int ono = analogRead(8);
  148.   int non = map(ono, 0, 1030, 90, 64);
  149.   readr=(char)non;
  150.   return(readr);
  151. }
  152.  
  153. int sendMes() {
  154.  
  155.   lcd.clear();
  156.   lcd.setCursor(1,0);
  157.   lcd.print(mes);
  158.   lcd.setCursor(0,0);
  159.   lcd.print("TRANSMITTING");
  160.  
  161.   irsend.sendSony(0x0000, 12);
  162.   Serial.println("INITIALIZING TRANSMISSION");
  163.   irsend.sendSony(0x0000, 12);
  164.   lcd.print(".");
  165.   while (!irrecv.decode(&results));
  166.   if (irrecv.decode(&results)) {
  167.     Serial.println(results.value, HEX);
  168.     irrecv.resume(); // Continue receiving
  169.   }
  170.   lcd.print(".");
  171.   if (results.value != 0xFFFF) {
  172.     return (13);
  173.   }
  174.   Serial.println("INITIALIZATION SUCCESSFUL");
  175.   Serial.println("BEGINNING TRANSMISSION");
  176.   for (int i = 0; i < 16; i++) {
  177.     Serial.print("TRANSMITTING BYTE ");
  178.     Serial.println(i);
  179.     int msg = (int)(mes[i]);
  180.     Serial.println(msg);
  181.     irsend.sendSony(msg, 12);
  182.   }
  183.   Serial.println("MESSAGE TRANSMISSION COMPLETE");
  184.   Serial.println("SENDING CHECKSUM");
  185.   int chkSm;
  186.   for (int i = 0; i < 16; i++) {
  187.     chkSm += ((int)(mes[i]));
  188.   }
  189.   Serial.print("CHECKSUM: ");
  190.   Serial.println(chkSm);
  191.   irsend.sendSony(chkSm, 12);
  192.   while (!irrecv.decode(&results));
  193.   if (irrecv.decode(&results)) {
  194.     Serial.println(results.value, HEX);
  195.     irrecv.resume(); // Continue receiving
  196.   }
  197.   if (results.value != 0xFFFF) {
  198.     return (14);
  199.   }
  200.   Serial.println("SENDING COMPLETE");
  201.   return (0);
  202. }
  203.  
  204. int recMes() {
  205.   Serial.println("WAITING FOR INITIALIZATION CODE");
  206.   while (!irrecv.decode(&results));
  207.   if (irrecv.decode(&results)) {
  208.     Serial.println(results.value, HEX);
  209.     irrecv.resume(); // Continue receiving
  210.   }
  211.   if (results.value != 0x0000) {
  212.     return (12);
  213.   }
  214.   Serial.println("SENDING INITIALIZATION CONFIRMATION");
  215.   irsend.sendSony(0xFFFF, 12);
  216.   Serial.println("AWAITING MESSAGE");
  217.   for (int i = 0; i < 16; i++) {
  218.     while (!irrecv.decode(&results));
  219.     if (irrecv.decode(&results)) {
  220.       Serial.println(results.value, HEX);
  221.       irrecv.resume(); // Continue receiving
  222.     }
  223.     rec[i] = ((char)(results.value, HEX));
  224.   }
  225.   int chkSm;
  226.   for (int i = 0; i < 16; i++) {
  227.     chkSm += ((int)(rec[i]));
  228.   }
  229.   while (!irrecv.decode(&results));
  230.   if (irrecv.decode(&results)) {
  231.     Serial.println(results.value, HEX);
  232.     irrecv.resume(); // Continue receiving
  233.   }
  234.   if ((chkSm, HEX) != (results.value, HEX)) {
  235.     return (12);
  236.   }else{
  237.     lcd.clear();
  238.     lcd.setCursor(0,0);
  239.     lcd.print(rec);
  240.     return(30);
  241.   }
  242.  
  243. }
  244.  
  245. //FINISH ADDING CHECKSUMS
  246. //DISPLAY REC MESSAGE
Advertisement
Add Comment
Please, Sign In to add comment