Advertisement
stanleyseow

LCD Shield buzzer morse code tinyGPSplus satellite reporting

Sep 30th, 2014
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.88 KB | None | 0 0
  1. /*
  2.  
  3. This code uses VK LCD Shield with a LCD and buzzer to generate the morse code numbers for the number
  4. of satellites being tracked ...
  5.  
  6. Most of the codes are from tinyGPSplus and morse code Arduino examples
  7.  
  8. I change to use PWM / analogWrite for a better sounding morse code tone ...
  9.  
  10. Stanley Seow
  11.  
  12.  
  13. */
  14.  
  15. #include <TinyGPS++BD.h>
  16. //#include <TinyGPS++.h>
  17. #include <SoftwareSerial.h>
  18. #include <LiquidCrystal.h>
  19. LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
  20.  
  21. // Morse Code timing
  22. #define dotPeriod 70
  23. #define dashPeriod (dotPeriod*3)
  24. #define relaxTime (dotPeriod)
  25. #define letterSpace (dotPeriod*2)
  26. #define wordSpace (dotPeriod*4)
  27. #define buzz 700
  28. #define tonePin 6
  29.  
  30.  
  31. // Buzzer = D6
  32. // Push button = A3
  33. // A3 < 50 - Right
  34. // A3 < 600 - Left
  35. // A3 < 1020 - Down
  36.  
  37. unsigned int counter = 0;
  38. static const int RXPin = 7, TXPin = 8;
  39. static const uint32_t GPSBaud = 9600;
  40.  
  41. // The TinyGPS++ object
  42. TinyGPSPlus gps;
  43.  
  44. // The serial connection to the GPS device
  45. SoftwareSerial ss(RXPin, TXPin);
  46.  
  47. void setup()
  48. {
  49.  
  50.   lcd.begin(16,2);
  51.   pinMode(tonePin,OUTPUT);
  52.   Serial.begin(115200);
  53.   ss.begin(GPSBaud);
  54.  
  55.   lcd.clear();
  56.   lcd.setCursor(0,0);
  57.   lcd.print("Sat Monitor");  
  58.   lcd.setCursor(0,1);
  59.   lcd.print("9W2SVT");
  60.  
  61.   playLetter(9);      
  62.   delay(letterSpace);
  63.   playLetter('W');      
  64.   delay(letterSpace);
  65.    playLetter(2);      
  66.   delay(letterSpace);
  67.   playLetter('S');      
  68.   delay(letterSpace);
  69.   playLetter('V');
  70.   delay(letterSpace);  
  71.   playLetter('T');
  72.   delay(letterSpace);
  73.  
  74.   delay(1000);
  75.  
  76. }
  77.  
  78. void loop()
  79. {
  80.   static const double LONDON_LAT = 51.508131, LONDON_LON = -0.128002;
  81.  
  82.   lcd.clear();
  83.   lcd.setCursor(0, 0);
  84.  
  85.   lcd.print(convertDegMin(gps.location.lat()),2);
  86.   lcd.print(" ");
  87.   lcd.print(gps.satellites.value());
  88.   lcd.print(" ");
  89.   lcd.print(gps.hdop.value());
  90.  
  91.  
  92.   lcd.setCursor(0, 1);
  93.   lcd.print(convertDegMin(gps.location.lng()),2);
  94.   lcd.print(" ");
  95.   lcd.print(gps.speed.kmph(),0);
  96.   lcd.print(" ");
  97.   lcd.print(gps.altitude.meters(),0);
  98.  
  99.  
  100.   // Extract GPS value and play morse code (2 digits );
  101.   byte n1 = gps.satellites.value()/10;
  102.   byte n2 = gps.satellites.value() % 10;
  103.  
  104.  
  105.   if ( counter % 30 == 0 ) {
  106.     if ( n1 != 0 ) {
  107.       playLetter(n1);
  108.       delay(letterSpace);
  109.     }
  110.  
  111.     playLetter(n2);
  112.     delay(letterSpace);
  113.   }
  114.  
  115.   counter++;
  116.  
  117.   smartDelay(1000);
  118.  
  119.   if (millis() > 5000 && gps.charsProcessed() < 10)
  120.     Serial.println(F("No GPS data received: check wiring"));
  121. }
  122.  
  123.  
  124. float convertDegMin(float decDeg) {
  125.  
  126.   float DegMin;
  127.  
  128.   int intDeg = decDeg;
  129.   decDeg -= intDeg;
  130.   decDeg *= 60;
  131.   DegMin = ( intDeg*100 ) + decDeg;
  132.  
  133.  return DegMin;
  134. }
  135.  
  136. // This custom version of delay() ensures that the gps object
  137. // is being "fed".
  138. static void smartDelay(unsigned long ms)
  139. {
  140.   unsigned long start = millis();
  141.   do
  142.   {
  143.     while (ss.available())
  144.       gps.encode(ss.read());
  145.   } while (millis() - start < ms);
  146. }
  147.  
  148. static void printFloat(float val, bool valid, int len, int prec)
  149. {
  150.   if (!valid)
  151.   {
  152.     while (len-- > 1)
  153.       Serial.print('*');
  154.     Serial.print(' ');
  155.   }
  156.   else
  157.   {
  158.     Serial.print(val, prec);
  159.     int vi = abs((int)val);
  160.     int flen = prec + (val < 0.0 ? 2 : 1); // . and -
  161.     flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
  162.     for (int i=flen; i<len; ++i)
  163.       Serial.print(' ');
  164.   }
  165.   smartDelay(0);
  166. }
  167.  
  168. static void printInt(unsigned long val, bool valid, int len)
  169. {
  170.   char sz[32] = "*****************";
  171.   if (valid)
  172.     sprintf(sz, "%ld", val);
  173.   sz[len] = 0;
  174.   for (int i=strlen(sz); i<len; ++i)
  175.     sz[i] = ' ';
  176.   if (len > 0)
  177.     sz[len-1] = ' ';
  178.   Serial.print(sz);
  179.   smartDelay(0);
  180. }
  181.  
  182. static void printDateTime(TinyGPSDate &d, TinyGPSTime &t)
  183. {
  184.   if (!d.isValid())
  185.   {
  186.     Serial.print(F("********** "));
  187.   }
  188.   else
  189.   {
  190.     char sz[32];
  191.     sprintf(sz, "%02d/%02d/%02d ", d.month(), d.day(), d.year());
  192.     Serial.print(sz);
  193.   }
  194.  
  195.   if (!t.isValid())
  196.   {
  197.     Serial.print(F("******** "));
  198.   }
  199.   else
  200.   {
  201.     char sz[32];
  202.     sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
  203.     Serial.print(sz);
  204.   }
  205.  
  206.   printInt(d.age(), d.isValid(), 5);
  207.   smartDelay(0);
  208. }
  209.  
  210. static void printStr(const char *str, int len)
  211. {
  212.   int slen = strlen(str);
  213.   for (int i=0; i<len; ++i)
  214.     Serial.print(i<slen ? str[i] : ' ');
  215.   smartDelay(0);
  216. }
  217.  
  218. void dit()
  219. {
  220.  analogWrite(tonePin,128);
  221.  delay(dotPeriod);
  222.  analogWrite(tonePin,0);
  223.  delay(relaxTime);
  224. }
  225.  
  226. void dah()
  227. {
  228.   analogWrite(tonePin,128);  
  229.   delay(dashPeriod);
  230.   digitalWrite(tonePin,LOW);
  231.   analogWrite(tonePin,0);
  232.   delay(relaxTime);
  233. }
  234.  
  235. void playLetter(char x)
  236. {
  237.   switch (x) {
  238.    
  239.      // dit tree
  240.     case 'E':
  241.       dit(); return;
  242.     case 'A':
  243.       dit(); dah(); return;
  244.      
  245.     case 'W':
  246.       dit(); dah(); dah();return;
  247.     case 'R':
  248.       dit(); dah(); dit();return;
  249.      
  250.     case 'J':
  251.        dit(); dah(); dah(); dah(); return;
  252.     case 'P':
  253.        dit(); dah(); dah(); dit(); return;    
  254.     case 'L':
  255.        dit(); dah(); dit(); dit();return;  
  256.    
  257.     case 'I':
  258.       dit(); dit(); return;      
  259.     case 'U':
  260.        dit(); dit(); dah(); return;
  261.     case 'S':
  262.        dit(); dit(); dit(); return;
  263.        
  264.     case 'F':
  265.        dit(); dit(); dah(); dit(); return;  
  266.     case 'V':
  267.       dit(); dit(); dit(); dah(); return;
  268.     case 'H':
  269.        dit(); dit(); dit(); dit(); return;
  270.        
  271. // dah tree      
  272.     case 'T':
  273.       dah(); return;
  274.     case 'M':
  275.       dah(); dah();return;
  276.     case 'O':
  277.        dah(); dah(); dah();return;
  278.     case 'G':
  279.        dah(); dah(); dit();return;  
  280.     case 'Q':
  281.        dah(); dah(); dit(); dah(); return;
  282.     case 'Z':
  283.         dah(); dah(); dit(); dit(); return;  
  284.  
  285.  
  286.     case 'N':
  287.       dah(); dit(); return;  
  288.     case 'K':
  289.        dah(); dit(); dah(); return;
  290.     case 'Y':
  291.        dah(); dit(); dah(); dah(); return;
  292.     case 'C':
  293.         dah(); dit(); dah(); dit(); return;          
  294.     case 'D':
  295.        dah(); dit(); dit(); return;  
  296.     case 'X':
  297.        dah(); dit(); dit(); dah(); return;  
  298.     case 'B':
  299.        dah(); dit(); dit(); dit(); return;  
  300.    
  301.     case 1:
  302.        dit(); dah(); dah(); dah(); dah(); return;  
  303.     case 2:
  304.        dit(); dit(); dah(); dah(); dah(); return;  
  305.     case 3:
  306.        dit(); dit(); dit(); dah(); dah(); return;  
  307.     case 4:
  308.        dit(); dit(); dit(); dit(); dah(); return;      
  309.     case 5:
  310.        dit(); dit(); dit(); dit(); dit(); return;  
  311.     case 6:
  312.        dah(); dit(); dit(); dit(); dit(); return;        
  313.     case 7:
  314.        dah(); dah(); dit(); dit(); dit(); return;  
  315.     case 8:
  316.        dah(); dah(); dah(); dit(); dit(); return;        
  317.     case 9:
  318.       dah(); dah(); dah(); dah(); dit(); return;              
  319.     case 0: // Zero
  320.       dah(); dah(); dah(); dah(); dah(); return;
  321.      
  322.     case ' ':
  323.       delay(wordSpace); return;
  324.   }
  325. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement