Facebook
From Walloping Zebra, 3 Years ago, written in C++.
This paste is a reply to How Do You Gauge the Risks and Rewards That Are As from Soiled Ostrich - view diff
Embed
Download Paste or View Raw
Hits: 106
  1. #include <SoftwareSerial.h>
  2. SoftwareSerial s(13,15);
  3.  
  4. #include <Wire.h>
  5.  
  6.  
  7. int livello, min, max;
  8.  
  9.  
  10.  
  11. void setup()
  12.  
  13. {
  14.  
  15.   Serial.begin(9600);
  16.   s.begin(2400);
  17.  
  18.  }
  19.  
  20. void loop()
  21. {
  22.  
  23.  
  24.   if (s.available() > 0)
  25.   {
  26.     static char rxBuffer[32];   // la direttiva static fa in modo che la variabile locale mantiene in memoria il valore
  27.     static int index =0;
  28.    
  29.     char ch = s.read();                 // Leggiamo un carattere per volta (non bloccante come parseInt())
  30.     rxBuffer[index++] = ch;     // Questa istruzione aggiunge ch all'array e contemporaneamente incrementa index di 1
  31.  
  32.     if (ch == '\n' )
  33.     {
  34.       // Trasmissione completa -> reset di index (ed eventualmente dell'array se necessario)
  35.       index = 0;
  36.      
  37.       // conversione del char array to int con l'istruzione C sscanf
  38.       sscanf(rxBuffer, "%d;%d;%d", &livello, &min, &max);
  39.      
  40.       Serial.print("Hai inviato il numero ");
  41.       Serial.println(livello);
  42.  
  43.       livello = map(livello, min, max, 0, 500);
  44.       Serial.print("Risultato del map  ");
  45.       Serial.println(livello);
  46.     }
  47.      
  48.  
  49. }
  50.