Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include "C_BatteryHandlerMEGA.h"
  2.  
  3. using namespace std;
  4.  
  5. class C_BatteryHandlerMEGA{
  6.  
  7.     C_BatteryHandlerMEGA::C_BatteryHandlerMEGA(int pin, int time_refresh = 1){
  8.         this->pin = pin;
  9.         this->time_refresh = time_refresh
  10.         this->handleBattery();
  11.     }
  12.  
  13.     void C_BatteryHandlerMEGA::handleBattery(){
  14.         digitalWrite(BATTERY_HANDLER_PIN, battery_state);
  15.     }
  16.  
  17.     bool C_BatteryHandlerMEGA::initMeasuring(){
  18.         float time = (100*pow(10, -9))*(this->cpt++);
  19.         if(time == this->time_refresh)){
  20.             this->takeMeasure();
  21.             this->handleBattery();
  22.         }
  23.     }
  24.  
  25.     void C_BatteryHandlerMEGA::takeMeasure(){
  26.         int valeur = analogRead(this->pin);
  27.  
  28.         this->tension = valeur * (5/1023.0);
  29.  
  30.         this->checkTensionThreshold();
  31.     }
  32.  
  33.     void C_BatteryHandlerMEGA::checkTensionThreshold(){
  34.         if(this->tension <= MIN_TENSION){
  35.             this->switch_battery = true;
  36.             this->battery_state = 1;
  37.         }
  38.     }
  39.  
  40.     int C_BatteryHandlerMEGA::getBatteryPercent() const {
  41.         return (int)(((this->tension-MIN_TENSION)/(MAX_TENSION-MIN_TENSION))*100);
  42.     }
  43. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement