Advertisement
AbstractBeliefs

Untitled

Nov 6th, 2011
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.62 KB | None | 0 0
  1. #include <NewSoftSerial.h>
  2.  
  3. NewSoftSerial phmeter = NewSoftSerial(2, 3);
  4. char received[20];
  5.  
  6. void init_ph(){
  7.   phmeter.begin(38400);
  8.   lon();
  9.   e();
  10. }
  11.  
  12. void lon(){
  13.   phmeter.print("L1");
  14.   phmeter.print(13, BYTE);
  15. }
  16.  
  17. void loff(){
  18.   phmeter.print("L0");
  19.   phmeter.print(13, BYTE);
  20. }
  21.  
  22. void r(char *dest){
  23.   phmeter.print("R");
  24.   phmeter.print(13, BYTE);
  25.   recv(dest);
  26. }
  27.  
  28. void e(){
  29.   phmeter.print("E");
  30.   phmeter.print(13, BYTE);
  31. }
  32.  
  33.  
  34. void i(char *dest){
  35.   phmeter.print("R");
  36.   phmeter.print(13, BYTE);
  37.   recv(dest);
  38. }
  39.  
  40. void recv(char *buf){
  41.   phmeter.flush(); // clear any crap
  42.   byte done = 0;
  43.   byte skipped = 0;
  44.   byte index = 0;
  45.  
  46.   delay(462); // Delay the magic number to let board respond
  47.   while (!done){
  48.     if (skipped > 50){  // If waited 462+50ms, since last byte, timeout.
  49.       buf[0] = 'N';
  50.       buf[1] = '\0';
  51.       done = 1;
  52.     }
  53.    
  54.     if (phmeter.available() < 1){  // if no data was available, we skip and wait some more
  55.       skipped++;
  56.       delay(1);
  57.     }
  58.    
  59.     else {  // Data available.
  60.       char c = phmeter.read();
  61.       if (c = 13) {  // If its <CR>, we are done.
  62.         buf[index] = '\0';
  63.         done = 1;
  64.         break;
  65.       }
  66.      
  67.       buf[index] = c;  // if not, lets add it to the capture buffer
  68.       index++; // And move to the next space
  69.     }
  70.   }
  71.   phmeter.flush(); // clear any crap
  72.   return;
  73. }
  74.  
  75. void setup(){
  76.   init_ph();
  77.   Serial.begin(19200);
  78. }
  79.  
  80. void loop(){
  81.   r(received);
  82.   Serial.print("This reading: ");
  83.   Serial.print(received);
  84.   Serial.print("\t at time: ");
  85.   Serial.println(millis(), DEC);
  86.  
  87.   delay(1000);
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement