DAT programName byte "PingTemperatureDemo141206a", 0 CON {{ By Duane Degn December 6, 2014 This program demonstrates the abiltity of the object "EddiePing" to compensate for changing temperatures while measuring distances with a Ping sensor. This demo program increments the temperature value up and down to illustrate how to make set the new temperature value. Ideally one would want a way to monitor the ambient temperature so the value sent to the "AdjustForTemperature" matched the temperature of the environment of the Ping sensors. See the comments in the method "AdjustForTemperature" about the units one may use when calling the method. See the section "Temperature Effects on Speed of Sound" in the comments of the child object. This program assumes two Ping sensors are in use. If a differnt number of Ping sensors are used, change the value of the constant "PING_SENSORS_IN_USE" to the appropriate value and make changes to the "PING_PIN" constants. }} CON _clkmode = xtal1 + pll16x _xinfreq = 5_000_000 PING_PIN_0 = 0 PING_PIN_1 = 1 '' If additional Ping sensors are used, list the '' I/O pins in the "pingPins" array in the DAT '' section. PING_SENSORS_IN_USE = 2 '' This value needs to be adjusted if the number '' of Ping sensors is changed. MAX_PING_INDEX = PING_SENSORS_IN_USE - 1 DEBUG_BAUD = 115_200 DEFAULT_INTER_PING_DELAY = 50 ' in milliseconds '' The minimum delay should probably be about '' 20ms in order for the previous echos to die '' down. The delay should be long enough to allow '' for any delays caused by serial output. If the '' delay is too short a waitcnt statement may '' cause the program to freeze. '' In this demo program, the delay could probably '' be removed without causing a problem since the '' calls to the serial object will likely produce '' delays long enough to allow any echos to die '' down. '' Using a delay between Ping reading is likely '' most important when using multiple Ping '' sensors at the same time. LOWEST_TEMPERATURE = -10_0 '' tenths of degrees Celsius (-10.0 C) HIGHEST_TEMPERATURE = 40_0 '' tenths of degrees Celsius (40.0 C) INCREMENT_TEMPERATURE = 5 '' (0.5 C) VAR long range[PING_SENSORS_IN_USE] long pingTimer, pingInterval long speedOfSound OBJ Ping : "EddiePing141206a.spin" Pst : "Parallax Serial Terminal" PUB Start | changeInTemperature, temperature Pst.Start(DEBUG_BAUD) 'waitcnt(clkfreq * 2 + cnt) ' uncomment this line to add delay to start of program Pst.Clear Pst.Home Pst.Str(String(11, 13, "This program ", 34)) Pst.Str(@programName) Pst.Str(String(34, " demonstates the temperature compensation feature of the object ", 34)) Pst.Str(Ping.GetObjectName) Pst.Str(String(34, ".", 11, 13)) changeInTemperature := INCREMENT_TEMPERATURE temperature := LOWEST_TEMPERATURE Ping.Init pingInterval := DEFAULT_INTER_PING_DELAY * (clkfreq / 1000) pingTimer := cnt repeat Pst.ClearBelow Pst.Home Pst.Str(String(11, 13, "This program ", 34)) Pst.Str(@programName) Pst.Str(String(34, " demonstates the temperature compensation feature of the object ", 34)) Pst.Str(Ping.GetObjectName) Pst.Str(String(34, ".", 11, 13)) Pst.Str(string(11, 13, "The temperature used to compute the speed of sound is now set to ")) Pst.Dec(temperature / 10) Pst.Char(".") Pst.Dec(||temperature // 10) Pst.Str(string(" C")) Ping.AdjustForTemperature(temperature, Ping#DEGREES_TENTH_C) speedOfSound := Ping.GetSpeedOfSound(Ping#CM_UNITS) Pst.Str(string(11, 13, "The speed of sound value used to compute distances is ")) Pst.Dec(speedOfSound) Pst.Str(string(" centimeters per second.")) repeat result from 0 to MAX_PING_INDEX range[result] := Ping.Millimeters(pingPins[result]) Pst.Str(string(11, 13, "Sensor # ")) Pst.Dec(result) Pst.Str(string(" = ")) Pst.Dec(range[result] / 10) Pst.Char(".") Pst.Dec(||range[result] // 10) Pst.Str(string(" cm")) waitcnt(pingTimer += pingInterval) Pst.ClearEnd Pst.NewLine temperature += changeInTemperature if temperature =< LOWEST_TEMPERATURE or temperature => HIGHEST_TEMPERATURE -changeInTemperature DAT pingPins byte PING_PIN_0, PING_PIN_1 '' list I/O pins if additional sensors are used {{ Terms of Use: MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. }}