Home › Forums › Environmental Sensors › Turbidity Sensor Information/Questions › Reply To: Turbidity Sensor Information/Questions
2022-10-05 at 12:15 PM
#17371
I used the Mayfly 1.1 board and the sketch below. Thank you for helping me!
Arduino
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
/* ######################## # OVERVIEW # ######################## Example B: Changing the address of a sensor. This is a simple demonstration of the SDI-12 library for arduino. It discovers the address of the attached sensor and allows you to change it. ######################### # THE CIRCUIT # ######################### The circuit: You should not have more than one SDI-12 device attached for this example. See: https://raw.github.com/Kevin-M-Smith/SDI-12-Circuit-Diagrams/master/basic_setup_no_usb.png or https://raw.github.com/Kevin-M-Smith/SDI-12-Circuit-Diagrams/master/compat_setup_usb.png ########################### # COMPATIBILITY # ########################### This library requires the use of pin change interrupts (PCINT). Not all Arduino boards have the same pin capabilities. The known compatibile pins for common variants are shown below. Arduino Uno: All pins. Arduino Mega or Mega 2560: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69). Arduino Leonardo: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI) ######################### # RESOURCES # ######################### Written by Kevin M. Smith in 2013. Contact: SDI12@ethosengineering.org The SDI-12 specification is available at: http://www.sdi-12.org/ The library is available at: https://github.com/EnviroDIY/Arduino-SDI-12 */ #include <SDI12.h> #define SERIAL_BAUD 115200 // The baud rate for the output serial port #define DATA_PIN 7 // The pin of the SDI-12 data bus #define POWER_PIN 22 // The sensor power pin (or -1 if not switching power) // Define the SDI-12 bus SDI12 mySDI12(DATA_PIN); String myCommand = ""; // empty to start char oldAddress = '!'; // invalid address as placeholder // this checks for activity at a particular address // expects a char, '0'-'9', 'a'-'z', or 'A'-'Z' boolean checkActive(byte i){ // this checks for activity at a particular address Serial.print("Checking address "); Serial.print((char)i); Serial.print("..."); myCommand = ""; myCommand += (char) i; // sends basic 'acknowledge' command [address][!] myCommand += "!"; for(int j = 0; j < 3; j++){ // goes through three rapid contact attempts mySDI12.sendCommand(myCommand); delay(30); if(mySDI12.available()) { // If we here anything, assume we have an active sensor Serial.println("Occupied"); mySDI12.clearBuffer(); return true; } else { Serial.println("Vacant"); // otherwise it is vacant. mySDI12.clearBuffer(); } } return false; } void setup(){ Serial.begin(SERIAL_BAUD); while(!Serial); Serial.println("Opening SDI-12 bus..."); mySDI12.begin(); delay(500); // allow things to settle // Power the sensors; if(POWER_PIN > 0){ Serial.println("Powering up sensors..."); pinMode(POWER_PIN, OUTPUT); digitalWrite(POWER_PIN, HIGH); delay(200); } } void loop(){ boolean found = false; // have we identified the sensor yet? for(byte i = '0'; i <= '9'; i++){ // scan address space 0-9 if(found) break; if(checkActive(i)){ found = true; oldAddress = i; } } for(byte i = 'a'; i <= 'z'; i++){ // scan address space a-z if(found) break; if(checkActive(i)){ found = true; oldAddress = i; } } for(byte i = 'A'; i <= 'Z'; i++){ // scan address space A-Z if(found) break; if(checkActive(i)){ found = true; oldAddress = i; } } if(!found){ Serial.println("No sensor detected. Check physical connections."); // couldn't find a sensor. check connections.. } else{ Serial.print("Sensor active at address "); // found a sensor! Serial.print(oldAddress); Serial.println("."); Serial.println("Enter new address."); // prompt for a new address while(!Serial.available()); char newAdd= Serial.read(); // wait for valid response while( ((newAdd<'0') || (newAdd>'9')) && ((newAdd<'a') || (newAdd>'z')) && ((newAdd<'A') || (newAdd>'Z'))){ if(!(newAdd =='\n') || (newAdd =='\r') || (newAdd ==' ')) { Serial.println("Not a valid address. Please enter '0'-'9', 'a'-'A', or 'z'-'Z'."); } while(!Serial.available()); newAdd = Serial.read(); } /* the syntax of the change address command is: [currentAddress]A[newAddress]! */ Serial.println("Readdressing sensor."); myCommand = ""; myCommand += (char) oldAddress; myCommand += "A"; myCommand += (char) newAdd; myCommand += "!"; mySDI12.sendCommand(myCommand); /* wait for the response then throw it away by clearing the buffer with clearBuffer() */ delay(300); mySDI12.clearBuffer(); Serial.println("Success. Rescanning for verification."); |