{"id":371,"date":"2018-12-12T17:42:52","date_gmt":"2018-12-12T17:42:52","guid":{"rendered":"http:\/\/blog.cup-of-joe.me\/?p=371"},"modified":"2018-12-12T19:41:13","modified_gmt":"2018-12-12T19:41:13","slug":"mast-micro-controller-code","status":"publish","type":"post","link":"http:\/\/blog.cup-of-joe.me\/?p=371","title":{"rendered":"Mast &#8211; Micro controller code"},"content":{"rendered":"<p>Below is the arduino code I use for the Pro Mini. \u00a0I&#8217;m going to post it in block quotes as well as a link to a txt file with the code. \u00a0I feel that it&#8217;s commented well so I&#8217;m not going to go into details here. \u00a0It uses the standard Wire, OneWire &amp; AVR\/Interrupt libraries as well as some found code for the DS18B20 reading. \u00a0I&#8217;m planning on loading all my code to Github in the future so it&#8217;s all in one place. \u00a0I just have to figure out how to do that appropriately. \u00a0(Since some of the libraries I use for sensors come from other sources, ie: Adafruit)<\/p>\n<blockquote><p>#include &lt;Wire.h&gt;<br \/>\n#include &lt;avr\/interrupt.h&gt;<br \/>\n#include &lt;OneWire.h&gt;<\/p>\n<p>\/\/ defining which I2C addres to use<br \/>\n#define SLAVE_ADDRESS 0x05<br \/>\n\/\/ setting he number used to request data from pro mini to send back to the pi<br \/>\nint number = 0;<\/p>\n<p>\/\/setting up pins for sensors<br \/>\nconst byte rainGauge = 2;<br \/>\nconst byte windSpeed = 3;<\/p>\n<p>\/\/ setting up variables for sensors<br \/>\nvolatile int rainCount = 0;<br \/>\nvolatile int windCount = 0;<br \/>\nvolatile int windCountSend = 0;<br \/>\nvolatile int windCountSendMax = 0;<br \/>\nlong rainDebouncingTime = 300; \/\/Debouncing Time in Milliseconds<br \/>\nvolatile unsigned long rainLastMicros;<br \/>\nlong windDebouncingTime = 100; \/\/Debouncing Time in Milliseconds<br \/>\nvolatile unsigned long windLastMicros;<br \/>\nvolatile int windDir = 0;<\/p>\n<p>int temp = 0;<br \/>\n\/\/ pin for DS18B20<br \/>\nOneWire ds(10); \/\/ on pin 10<\/p>\n<p>void setup() {<\/p>\n<p>\/\/ initialize i2c as slave<br \/>\nWire.begin(SLAVE_ADDRESS);<\/p>\n<p>\/\/ define callbacks for i2c communication<br \/>\nWire.onReceive(receiveData);<br \/>\nWire.onRequest(sendData);<\/p>\n<p>\/\/ setup pins for sensors and attach interrupts<br \/>\npinMode(rainGauge, INPUT);<br \/>\ndigitalWrite (rainGauge, HIGH); \/\/ internal pull-up resistor<br \/>\nattachInterrupt (0, debounceRain, FALLING); \/\/ attach interrupt handler<br \/>\npinMode(windSpeed, INPUT);<br \/>\ndigitalWrite (windSpeed, HIGH); \/\/ internal pull-up resistor<br \/>\nattachInterrupt (1, debounceWind, FALLING); \/\/ attach interrupt handler<br \/>\nSerial.begin(9600);<\/p>\n<p>}<\/p>\n<p>void loop() {<\/p>\n<p>\/\/***********************************<br \/>\n\/\/ Read DS18B20<br \/>\nint HighByte, LowByte, TReading, SignBit, Tc_100, Whole, Fract;<\/p>\n<p>byte i;<br \/>\nbyte present = 0;<br \/>\nbyte data[12];<br \/>\nbyte addr[8];<\/p>\n<p>if ( !ds.search(addr)) {<br \/>\nds.reset_search();<br \/>\nreturn;<br \/>\n}<\/p>\n<p>ds.reset();<br \/>\nds.select(addr);<br \/>\nds.write(0x44,1); \/\/ start conversion, with parasite power on at the end<\/p>\n<p>delay(1000); \/\/ maybe 750ms is enough, maybe not<br \/>\n\/\/ we might do a ds.depower() here, but the reset will take care of it.<\/p>\n<p>present = ds.reset();<br \/>\nds.select(addr);<br \/>\nds.write(0xBE); \/\/ Read Scratchpad<\/p>\n<p>for ( i = 0; i &lt; 9; i++) { \/\/ we need 9 bytes<br \/>\ndata[i] = ds.read();<\/p>\n<p>}<\/p>\n<p>LowByte = data[0];<br \/>\nHighByte = data[1];<br \/>\nTReading = (HighByte &lt;&lt; 8) + LowByte;<br \/>\nSignBit = TReading &amp; 0x8000; \/\/ test most sig bit<br \/>\nif (SignBit) \/\/ negative<br \/>\n{<br \/>\nTReading = (TReading ^ 0xffff) + 1; \/\/ 2&#8217;s comp<br \/>\n}<br \/>\nTc_100 = (6 * TReading) + TReading \/ 4; \/\/ multiply by (100 * 0.0625) or 6.25<\/p>\n<p>temp = Tc_100;<\/p>\n<p>\/\/************************************<\/p>\n<p>\/\/ get wind speed<br \/>\nwindCount = 0;<br \/>\ndelay(10000);<br \/>\nwindCountSend = windCount;<br \/>\nif (windCountSend &gt; windCountSendMax){<br \/>\nwindCountSendMax = windCountSend;<br \/>\n}<\/p>\n<p>}<\/p>\n<p>\/\/ callback for received data<br \/>\nvoid receiveData(int byteCount){<\/p>\n<p>while(Wire.available()) {<br \/>\nnumber = Wire.read();<br \/>\n\/\/ setup to reset rain count. R-Pi sends this at midnight to reset<br \/>\nif (number == 8){<br \/>\nrainCount = 0;<br \/>\n}<\/p>\n<p>}<br \/>\n}<\/p>\n<p>\/\/ callback for sending data<br \/>\nvoid sendData(){<br \/>\nbyte sendData[2];<br \/>\nint sendVal = 0;<br \/>\nif (number == 1){<br \/>\nsendVal = rainCount;<\/p>\n<p>}<br \/>\n\/\/ wind speed<br \/>\nelse if (number == 2){<br \/>\nsendVal = windCountSend;<\/p>\n<p>}<br \/>\n\/\/ wind direction<br \/>\nelse if (number == 3){<br \/>\nsendVal = analogRead(1);<br \/>\n}<br \/>\n\/\/ wind gust speed<br \/>\nelse if (number == 4){<br \/>\nsendVal = windCountSendMax;<br \/>\nwindCountSendMax = 0;<\/p>\n<p>}<br \/>\n\/\/ temperature<br \/>\nelse if (number == 5){<br \/>\nsendVal = temp;<br \/>\n}<br \/>\n\/\/ humidity<br \/>\nelse if (number == 6){<br \/>\nsendVal = analogRead(2);<br \/>\n}<\/p>\n<p>else {<br \/>\nsendVal = number;<br \/>\n}<br \/>\n\/\/ sending requested data to the Pi<br \/>\nsendData[0] = (byte) sendVal &amp; 0xFF;<br \/>\n\/\/ Serial.println(sendData0, HEX);<br \/>\nsendData[1] = (byte) (sendVal &gt;&gt; 8) &amp; 0xFF;<br \/>\n\/\/ Serial.println(sendData1, HEX);<br \/>\nWire.write(sendData, 2);<br \/>\n\/\/Wire.write(sendData0);<br \/>\n\/\/Wire.write(sendData1);<br \/>\n}<\/p>\n<p>\/\/ debouncing of interrupt sensors so only count once per action<br \/>\nvoid debounceRain() {<br \/>\nif((long)(micros() &#8211; rainLastMicros) &gt;= rainDebouncingTime * 1000) {<br \/>\nrainFunc();<br \/>\nrainLastMicros = micros();<br \/>\n}<br \/>\n}<\/p>\n<p>void debounceWind() {<br \/>\nif((long)(micros() &#8211; windLastMicros) &gt;= windDebouncingTime * 1000) {<br \/>\nwindFunc();<br \/>\nwindLastMicros = micros();<br \/>\n}<br \/>\n}<\/p>\n<p>\/\/ incrementing the variables used for counting sensors<br \/>\nvoid rainFunc() {<br \/>\nrainCount++;<br \/>\nSerial.print(rainCount);<br \/>\n}<\/p>\n<p>void windFunc() {<br \/>\nwindCount++;<br \/>\nSerial.println(windCount);<br \/>\n}<\/p><\/blockquote>\n<p><a href=\"http:\/\/blog.cup-of-joe.me\/wp-content\/uploads\/2018\/12\/pro_mini_weather_station_new.txt\">Link to file<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Below is the arduino code I use for the Pro Mini. \u00a0I&#8217;m going to post it in block quotes as well as a link to a txt file with the code. \u00a0I feel that it&#8217;s commented well so I&#8217;m not going to go into details here. \u00a0It uses the standard Wire, OneWire &amp; AVR\/Interrupt libraries [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[3,7],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts\/371"}],"collection":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=371"}],"version-history":[{"count":3,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts\/371\/revisions"}],"predecessor-version":[{"id":375,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=\/wp\/v2\/posts\/371\/revisions\/375"}],"wp:attachment":[{"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=371"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=371"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.cup-of-joe.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=371"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}