Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //ASCII
- //SPACE-32
- //UNDERSCORE-95
- // include the library code:
- #include <LiquidCrystal.h>
- const int joy1X = 0;
- const int joy1Y = 2;
- const int joy2X = 4;
- const int joy2Y = 6;
- const int buttonSpace = 46;
- const int buttonEnter = 47;
- const int buttonSend = 44;
- const int BT_RX = 15;
- const int BT_TX = 14;
- // initialize the library with the numbers of the interface pins
- LiquidCrystal lcd(48, 49, 50, 51, 52, 53); //lcd(12, 11, 5, 4, 3, 2);
- int gloo;
- bool monom;
- char uSc = ((char)95);
- char mes[16];
- char rec[16];
- bool isSending=false;
- int mesCharNum;
- void setup() {
- mesCharNum=0;
- gloo = 0;
- monom = false;
- // set up the LCD's number of columns and rows:
- lcd.begin(16, 2);
- // Print a message to the LCD.
- Serial3.begin(9600);
- Serial.begin(38400);
- mes[0] = uSc;
- for (int q = 1; q < 16; q++) {
- mes[q] = (char)32;
- }
- lcd.setCursor(0, 0);
- lcd.print("ENTER - send");
- lcd.setCursor(0, 1);
- lcd.print("SPACE - receive");
- bool hasChosen=false;
- do{
- delay(10);
- if(digitalRead(buttonEnter)==HIGH){
- isSending=true;
- hasChosen=true;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Sending");
- }
- if(digitalRead(buttonSpace)==HIGH){
- isSending=false;
- hasChosen=true;
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Receiving");
- }
- }while(!hasChosen);
- delay(2000);
- lcd.clear();
- }
- void loop() {
- if(isSending){
- lcd.clear();
- // set the cursor to column 0, line 1
- // (note: line 1 is the second row, since counting begins with 0):
- lcd.setCursor(0, 1);
- // print the number of seconds since reset:
- lcd.print(mes);
- lcd.setCursor(8, 0);
- lcd.print((int)analogRead(8));
- lcd.setCursor(0, 0);
- lcd.write(potType());
- //lcd.print(nunu);
- delay(200);
- gloo++;
- if (gloo % 5 == 0) {
- monom = true;
- }
- if (digitalRead(buttonSend) == HIGH) { //SEND
- int err = sendMes();
- while (err == 123) {
- Serial.println("FAILED TO INITIALIZE RECIEVER");
- err = sendMes();
- }
- }
- if (digitalRead(buttonEnter) == HIGH){
- mes[mesCharNum] = potType();
- mes[mesCharNum + 1] = uSc;
- mesCharNum++;
- }
- if(digitalRead(buttonSpace)==HIGH){
- mes[mesCharNum]=' ';
- mes[mesCharNum+1]=uSc;
- mesCharNum++;
- }
- }else{
- //Rec
- lcd.clear();
- int erro;
- while(erro!=30){
- erro = recMes();
- }
- lcd.setCursor(0,0);
- lcd.print(rec);
- lcd.setCursor(0,1);
- lcd.print("ENTER TO RESTART");
- while(digitalRead(47)==LOW);
- }
- }
- char potType(){
- char readr;
- int ono = analogRead(8);
- int non = map(ono, 0, 1030, 90, 64);
- readr=(char)non;
- return(readr);
- }
- int sendMes() {
- Serial.println("FORMATTING MESSAGE");
- if(mesCharNum<16){
- for(int i=mesCharNum-1; i<16; i++){
- mes[i]=(char)32;
- }
- }
- Serial.println(mes);
- Serial.println("CHECKING CONNECTION");
- Serial3.flush();
- Serial3.write((char)35);
- while(!(Serial3.available() > 0));
- if((int)Serial3.read()==36){
- Serial.println("CONNECTION SUCESSFUL");
- }else{
- Serial.println("CONNECTION FAILED - RETRYING");
- return(123);
- }
- Serial.print("SENDING MESSAGE: ");
- Serial.println(mes);
- for(int i=0; i<16; i++){
- Serial.print("Sending char ");
- Serial.println(i);
- Serial3.write((char)mes[i]);
- delay(1000);
- }
- Serial.print("SENT");
- delay(1000);
- }
- int recMes() {
- for(int i=0; i<16; i++){
- rec[i]=0;
- }
- bool hasRec=false;
- while(!hasRec);
- Serial.println("WAITING FOR MESSAGE");
- if (Serial3.available() > 0) {
- if((int)Serial3.read()==35){
- Serial.println("CONNECTION SUCESSFUL - RECEIVING MESSAGE");
- for(int i=0; i<16; i++){
- if (Serial3.available() > 0) {
- rec[i] = Serial3.read();
- Serial.print("RECEIVED CHAR ");
- Serial.print(i);
- Serial.println(" OF 16");
- Serial.print("CHAR: ");
- Serial.println((char)rec[i]);
- }
- else
- {
- i--;
- }
- }
- Serial.print("RECEIVED \"");
- Serial.print(rec);
- Serial.println("\"");
- }
- }else{
- Serial.println("INCORRECT INIT VALUE - WAITING");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment