Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <NewSoftSerial.h>
- NewSoftSerial phmeter = NewSoftSerial(2, 3);
- char received[20];
- void init_ph(){
- phmeter.begin(38400);
- lon();
- e();
- }
- void lon(){
- phmeter.print("L1");
- phmeter.print(13, BYTE);
- }
- void loff(){
- phmeter.print("L0");
- phmeter.print(13, BYTE);
- }
- void r(char *dest){
- phmeter.print("R");
- phmeter.print(13, BYTE);
- recv(dest);
- }
- void e(){
- phmeter.print("E");
- phmeter.print(13, BYTE);
- }
- void i(char *dest){
- phmeter.print("R");
- phmeter.print(13, BYTE);
- recv(dest);
- }
- void recv(char *buf){
- phmeter.flush(); // clear any crap
- byte done = 0;
- byte skipped = 0;
- byte index = 0;
- delay(462); // Delay the magic number to let board respond
- while (!done){
- if (skipped > 50){ // If waited 462+50ms, since last byte, timeout.
- buf[0] = 'N';
- buf[1] = '\0';
- done = 1;
- }
- if (phmeter.available() < 1){ // if no data was available, we skip and wait some more
- skipped++;
- delay(1);
- }
- else { // Data available.
- char c = phmeter.read();
- if (c = 13) { // If its <CR>, we are done.
- buf[index] = '\0';
- done = 1;
- break;
- }
- buf[index] = c; // if not, lets add it to the capture buffer
- index++; // And move to the next space
- }
- }
- phmeter.flush(); // clear any crap
- return;
- }
- void setup(){
- init_ph();
- Serial.begin(19200);
- }
- void loop(){
- r(received);
- Serial.print("This reading: ");
- Serial.print(received);
- Serial.print("\t at time: ");
- Serial.println(millis(), DEC);
- delay(1000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement