Advertisement
RichPyke

Battery Monitor EZ-Script

Jul 9th, 2013
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. # Battery Monitor Script
  2. # Author: Rich
  3. # Date: 2013-07-09
  4. # Version: 1.1.3
  5.  
  6. # Read voltage level of a single battery pack and report to software.
  7. # Adapted from LiPo monitor circuit.
  8. # Assumed monitor circuit connected to ADC0
  9.  
  10. # Additional LCD Display code added
  11. # Assumed 16x2 I2C LCD Display
  12. # as http://www.robot-electronics.co.uk/acatalog/LCD_Displays.html
  13.  
  14. # Additional code for automatic voltage level calculations for
  15. # Lead Acid battery discharge voltages.
  16.  
  17. # Set variables
  18. # Change $multiplyer if using voltage divider
  19. # Factor is 5/255 for adc value conversion to volts
  20.  
  21. # Voltage Monitor Details
  22. $multiplier = 4
  23. # Lead Acid Battery Voltage
  24. $vbat = 12
  25.  
  26. # Default values. Vmin at 20%. Vcritical at 10%.
  27. # Level1 40%, Level2 60%, Level3 80%
  28. # Change if required. Note levels are on a curve.
  29. $vundervoltage = $vbat*0.88
  30. $vcritical = $vbat*0.94
  31. $vmin = $vbat*0.97
  32. $level1 = $vbat*0.99
  33. $level2 = $vbat*1.02
  34. $level3 = $vbat*1.04
  35. $vmax = $vbat*1.06
  36. $factor = 0.019607843
  37.  
  38. :ReadCells
  39. # Get ADC values
  40. $vc1 = GetADC(ADC0)
  41.  
  42. # Convert values to voltage to 2 decimal places
  43. $batterylevel = $vc1 * $factor
  44. $batterylevel = $batterylevel * $multiplier
  45. $batterylevel = Round($batterylevel,2)
  46.  
  47. # Check for errors on circuit
  48. IF ($batterylevel > $vmax)
  49. Print("Battery Monitor Error")
  50. Print("Check Monitor Circuits")
  51. # I2CWrite code for displaying message on LCD
  52.  
  53. # Check for errors on battery connection
  54. ELSEIF ($batterylevel <= 0)
  55. Print ("Battery Connection Error")
  56. Print("Check Battery Connection")
  57. # I2CWrite code for displaying message on LCD
  58.  
  59. # Check for under voltage
  60. ELSEIF ($batterylevel <= $vundervoltage)
  61. Print ("Battery Under Voltage")
  62. Print("Check Or Replace Battery")
  63. # I2CWrite code for displaying message on LCD
  64.  
  65. # Check if at critical levels
  66. ELSEIF ($batterylevel <= $vcritical)
  67. # Do sometihing if critical
  68. Print("Battery Critical")
  69. # I2CWrite code for displaying message on LCD
  70. # Shut down all power, switch to back up battery alarm?
  71.  
  72. # Check if below recommended levels
  73. ELSEIF ($batterylevel < $vmin)
  74. # Do sometihing if voltage low
  75. # Battery 60% full, display 0 full blocks and 5 empty block
  76. Print("Battery Level Low")
  77.  
  78. ELSE
  79. # Output voltages
  80. Print($batterylevel + "V")
  81. # Output to LCD display here
  82. IF ($batterylevel >= $vmax)
  83. # Battery Full, display 5 full blocks and 0 empty blocks
  84. ELSEIF ($batterylevel > $level3)
  85. # Battery 80% full, display 4 full blocks and 1 empty block
  86. ELSEIF ($batterylevel > $level2)
  87. # Battery 60% full, display 3 full blocks and 2 empty blocks
  88. ELSEIF ($batterylevel > $level1)
  89. # Battery 40% full, display 2 full blocks and 3 empty blocks
  90. ELSEIF ($batterylevel > $vmin)
  91. # Battery 20% full, display 1 full block and 4 empty blocks
  92. ENDIF
  93. ENDIF
  94. # Wait 5 seconds
  95. Sleep(5000)
  96.  
  97. # Go back to the start
  98. Goto(ReadCells)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement