Advertisement
Roniere

Serial Arduino Receptor

Jan 3rd, 2019
207
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //Carrega a biblioteca LiquidCrystal
  2. #include <LiquidCrystal.h>
  3.  
  4. // Variáveis de definição das GPIOs
  5. int outZero = 2;
  6. int outOne  = 3;
  7. int outTwo  = 11;
  8. int inZero  = 12;
  9.  
  10. // Varíavel utilizada na transmissão serial
  11. int countSerial = 0;
  12.  
  13. //Define os pinos que serão utilizados para ligação ao display
  14. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  15.  
  16. void setup() {
  17.   // Código de configuração do Arduino
  18.  
  19.   pinMode(outZero,OUTPUT);
  20.   pinMode(outOne,OUTPUT);
  21.   pinMode(outTwo,OUTPUT);
  22.  
  23.   //Define o número de colunas e linhas do LCD
  24.   lcd.begin(16, 2);
  25.  
  26.   // Define o baudrate da comunicação serial no Arduino
  27.   Serial.begin(9600);
  28.  
  29. }
  30.  
  31. // Função que realiza a leitura dos dados recebidos pela porta serial
  32. String readStringSerial(){
  33.   String content = "";
  34.   char caracter;
  35.  
  36.   // Enquanto receber algo pela serial
  37.   while(Serial.available()) {
  38.     // Lê byte da serial
  39.     caracter = Serial.read();
  40.     // Ignora caractere de quebra de linha
  41.     if (caracter != '\n'){
  42.       // Concatena valores
  43.       content.concat(caracter);
  44.     }
  45.     // Aguarda buffer serial ler próximo caractere
  46.     delay(10);
  47.   }    
  48.   return content;
  49. }
  50.  
  51. void loop(){
  52.   // Se houver dados disponíveis na porta serial executa os comandos
  53.   if (Serial.available() > 0){
  54.  
  55.      String dpy;
  56.      String ser;
  57.      String output;
  58.      String receivedBuffer = readStringSerial();
  59.      String ptl = receivedBuffer.substring(0,3);
  60.      int key;
  61.      int lengthLcd;
  62.  
  63.      // Interpreta o cabeçalho do pacote de dados e define se eles são da "Entrada Digital", "Saída Digital", "LCD" ou "Serial"
  64.      if(ptl == "INP"){
  65.         key = 0;
  66.      }
  67.      else{
  68.        if(ptl == "OUT"){
  69.           key = 1;
  70.        }
  71.        else{
  72.          if(ptl == "LCD"){
  73.             key = 2;
  74.          }
  75.          else{
  76.            if(ptl == "SER"){
  77.               key = 3;
  78.            }
  79.            else{
  80.               key = -1;
  81.            }
  82.          }
  83.        }
  84.      }
  85.  
  86.      // De acordo com cabeçalho recebido se manipula a carga útil do pacote de dados
  87.      switch(key){
  88.         case 0:
  89.            if(digitalRead(inZero) == 1){
  90.                Serial.print("INPUT HIGH");
  91.            }  
  92.            else{
  93.                Serial.print("INPUT LOW");
  94.            }
  95.            break;
  96.         case 1:
  97.            if(receivedBuffer.substring(3,5) == "00"){
  98.               digitalWrite(outZero,LOW);
  99.               Serial.print("OUTPUT 1 LOW ");
  100.            }
  101.            if(receivedBuffer.substring(3,5) == "01"){
  102.               digitalWrite(outZero,HIGH);
  103.               Serial.print("OUTPUT 1 HIGH");
  104.            }
  105.            if(receivedBuffer.substring(3,5) == "10"){
  106.               digitalWrite(outOne,LOW);
  107.               Serial.print("OUTPUT 2 LOW ");
  108.            }
  109.            if(receivedBuffer.substring(3,5) == "11"){
  110.               digitalWrite(outOne,HIGH);
  111.               Serial.print("OUTPUT 2 HIGH");
  112.            }
  113.            if(receivedBuffer.substring(3,5) == "20"){
  114.               digitalWrite(outTwo,LOW);
  115.               Serial.print("OUTPUT 3 LOW ");
  116.            }
  117.            if(receivedBuffer.substring(3,5) == "21"){
  118.               digitalWrite(outTwo,HIGH);
  119.               Serial.print("OUTPUT 3 HIGH");
  120.            }
  121.            break;
  122.          
  123.         case 2:
  124.           lengthLcd = receivedBuffer.substring(3,6).toInt();
  125.           dpy = receivedBuffer.substring(6,lengthLcd + 6);
  126.  
  127.           lcd.clear();
  128.           lcd.setCursor(0, 0);
  129.           lcd.print(dpy);
  130.           delay(500);
  131.  
  132.           Serial.print(lengthLcd);
  133.          
  134.           if(lengthLcd > 16){
  135.               for (int positionCounter = 0; positionCounter < lengthLcd; positionCounter++) {
  136.                 // scroll one position right:
  137.                 lcd.scrollDisplayLeft();
  138.                 // wait a bit:
  139.                 delay(500);
  140.               }
  141.           }
  142.          
  143.           lcd.clear();
  144.           lcd.print(dpy);
  145.          
  146.           break;
  147.          
  148.         case 3:
  149.           ser = receivedBuffer.substring(3);
  150.           Serial.print(ser);
  151.           break;
  152.          
  153.         default:
  154.           break;
  155.      }  
  156.   }    
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement