Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Battery Monitor Script
- # Author: Rich
- # Date: 2013-07-09
- # Version: 1.1.3
- # Read voltage level of a single battery pack and report to software.
- # Adapted from LiPo monitor circuit.
- # Assumed monitor circuit connected to ADC0
- # Additional LCD Display code added
- # Assumed 16x2 I2C LCD Display
- # as http://www.robot-electronics.co.uk/acatalog/LCD_Displays.html
- # Additional code for automatic voltage level calculations for
- # Lead Acid battery discharge voltages.
- # Set variables
- # Change $multiplyer if using voltage divider
- # Factor is 5/255 for adc value conversion to volts
- # Voltage Monitor Details
- $multiplier = 4
- # Lead Acid Battery Voltage
- $vbat = 12
- # Default values. Vmin at 20%. Vcritical at 10%.
- # Level1 40%, Level2 60%, Level3 80%
- # Change if required. Note levels are on a curve.
- $vundervoltage = $vbat*0.88
- $vcritical = $vbat*0.94
- $vmin = $vbat*0.97
- $level1 = $vbat*0.99
- $level2 = $vbat*1.02
- $level3 = $vbat*1.04
- $vmax = $vbat*1.06
- $factor = 0.019607843
- :ReadCells
- # Get ADC values
- $vc1 = GetADC(ADC0)
- # Convert values to voltage to 2 decimal places
- $batterylevel = $vc1 * $factor
- $batterylevel = $batterylevel * $multiplier
- $batterylevel = Round($batterylevel,2)
- # Check for errors on circuit
- IF ($batterylevel > $vmax)
- Print("Battery Monitor Error")
- Print("Check Monitor Circuits")
- # I2CWrite code for displaying message on LCD
- # Check for errors on battery connection
- ELSEIF ($batterylevel <= 0)
- Print ("Battery Connection Error")
- Print("Check Battery Connection")
- # I2CWrite code for displaying message on LCD
- # Check for under voltage
- ELSEIF ($batterylevel <= $vundervoltage)
- Print ("Battery Under Voltage")
- Print("Check Or Replace Battery")
- # I2CWrite code for displaying message on LCD
- # Check if at critical levels
- ELSEIF ($batterylevel <= $vcritical)
- # Do sometihing if critical
- Print("Battery Critical")
- # I2CWrite code for displaying message on LCD
- # Shut down all power, switch to back up battery alarm?
- # Check if below recommended levels
- ELSEIF ($batterylevel < $vmin)
- # Do sometihing if voltage low
- # Battery 60% full, display 0 full blocks and 5 empty block
- Print("Battery Level Low")
- ELSE
- # Output voltages
- Print($batterylevel + "V")
- # Output to LCD display here
- IF ($batterylevel >= $vmax)
- # Battery Full, display 5 full blocks and 0 empty blocks
- ELSEIF ($batterylevel > $level3)
- # Battery 80% full, display 4 full blocks and 1 empty block
- ELSEIF ($batterylevel > $level2)
- # Battery 60% full, display 3 full blocks and 2 empty blocks
- ELSEIF ($batterylevel > $level1)
- # Battery 40% full, display 2 full blocks and 3 empty blocks
- ELSEIF ($batterylevel > $vmin)
- # Battery 20% full, display 1 full block and 4 empty blocks
- ENDIF
- ENDIF
- # Wait 5 seconds
- Sleep(5000)
- # Go back to the start
- Goto(ReadCells)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement