Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Read voltage level of 2S LiPo battery and report to software.
- # Set variables
- # Change $vundervoltage to the under voltage of your specific battery (leave at 3v if unsure)
- # Change $vcritical to your lowest voltage you wish for the battery to get before human intervention
- # Change $vmin to low level alert value
- # Change $vmax to battery full charge
- # Change $multiplyer if using voltage divider
- # Factor is 5/255 for adc value conversion to volts
- $vundervoltage = 3
- $vcritical = 3.5
- $vmin = 3.7
- $vmax = 4.5
- $multiplier = 2
- $factor = 0.019607843
- :ReadCells
- # Get ADC values
- $vc1 = GetADC(ADC0)
- $vc2 = GetADC(ADC1)
- # Convert values to voltage
- $cell2 = $vc1 * $factor
- $cell1 = $vc2 * $factor * $multiplier
- $cell1 = $cell1 - $cell2
- $batteryv = $cell1 + $cell2
- # Check for errors on circuit
- IF ($cell1 > $vmax or $cell2 > $vmax)
- Print("Battery Monitor Error")
- Print("Check Monitor Circuits")
- # Check for errors on battery connection
- ELSEIF ($cell1 <= 0 or $cell2 <= 0)
- Print ("Battery Connection Error")
- Print("Check Battery Connection")
- # Check for under voltage
- ELSEIF ($cell1 <= $vundervoltage or $cell2 <= $undervoltage)
- Print ("Battery Under Voltage")
- Print("Check Or Replace Battery")
- # Check if at critical levels
- # Cell 1
- ELSEIF ($cell1 <= $vcritical)
- # Do sometihing if critical
- Print("Cell 1 Critical")
- # Shut down all power, switch to back up battery alarm?
- # Cell 2
- ELSEIF ($cell2 <= $vcritical)
- # Do sometihing if critical
- Print("Cell 2 Critical")
- # Shut down all power, switch to back up battery alarm?
- # Check if below recommended levels
- # Cell 1
- ELSEIF ($cell1 < $vmin)
- # Do sometihing if voltage low
- Print("Cell 1 Low")
- # Cell 2
- ELSEIF ($cell2 < $vmin)
- # Do sometihing if voltage low
- Print("Cell 2 Low")
- ELSE
- # Output voltages
- Print("C1 " + Round($cell1,2) + "V")
- Print("C2 " + Round($cell2,2) + "V")
- Print("To " + Round($batteryv,2) + "V")
- ENDIF
- # Wait 5 seconds
- Sleep(5000)
- # Go back to the start
- Goto(ReadCells)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement