Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2018
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.37 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <Servo.h>
  3. #include <Adafruit_INA219.h>
  4.  
  5. #include <DallasTemperature.h>
  6.  
  7. #define ONE_WIRE_BUS 2
  8.  
  9. OneWire oneWire(ONE_WIRE_BUS);
  10. DallasTemperature sensors(&oneWire);
  11.  
  12. Adafruit_INA219 ina219;
  13. Servo myservo;
  14. int pos = 0;
  15. int counter = 0;
  16. int startedCounter = 0;
  17.  
  18. bool isTurningOff = false;
  19. bool hasStarted = false;
  20.  
  21. const int POWER_BUTTON = 7;
  22. const int POWER_OFF_PIN = 8;
  23.  
  24. void setup(void)
  25. {
  26.   Serial.begin(115200);
  27.   myservo.attach(9);
  28.   while (!Serial) {
  29.       // will pause Zero, Leonardo, etc until serial console opens
  30.       delay(1);
  31.   }
  32.  
  33.   sensors.begin();
  34.  
  35.   uint32_t currentFrequency;
  36.    
  37.   Serial.println("Hello!");
  38.  
  39.   pinMode(POWER_BUTTON, INPUT);
  40.   pinMode(POWER_OFF_PIN, OUTPUT);
  41.  
  42.   ina219.begin();
  43.  
  44.   Serial.println("Measuring voltage and current with INA219 ...");
  45. }
  46.  
  47. void loop(void)
  48. {
  49.  
  50.   if (startedCounter > 2000) {
  51.     startedCounter = 0;
  52.     hasStarted = true;
  53.   }
  54.  
  55.   if (Serial.available() > 0) {
  56.     processMessage();
  57.   } else if (counter > 2000) {
  58.     counter = 0;
  59.     float shuntvoltage = 0;
  60.     float busvoltage = 0;
  61.     float current_mA = 0;
  62.     float loadvoltage = 0;
  63.  
  64.     shuntvoltage = ina219.getShuntVoltage_mV();
  65.     busvoltage = ina219.getBusVoltage_V();
  66.     current_mA = ina219.getCurrent_mA();
  67.     loadvoltage = busvoltage + (shuntvoltage / 1000);
  68. //    power = busvoltage * (current_mA/1000);
  69.  
  70.  
  71.     // 1.2 x (ncellx - 1) = 6V = cut off voltage
  72.     Serial.print("B:");
  73.     Serial.print(loadvoltage); Serial.print(",");
  74.     Serial.print(current_mA);
  75.     Serial.println("");
  76.  
  77.     sensors.requestTemperatures();
  78.  
  79.     Serial.print("Temperature is: ");
  80.     Serial.println(sensors.getTempCByIndex(0));
  81.     Serial.println(sensors.getTempCByIndex(1));
  82.      
  83.   } else {
  84.  
  85.     if (hasStarted && !isTurningOff && digitalRead(POWER_BUTTON) == HIGH) {
  86.       isTurningOff = true;
  87.      
  88.       Serial.println("Turning it off");
  89. //      turnOff();
  90.     }
  91.    
  92.   }
  93.  
  94.   delay(1);
  95.   counter += 1;
  96.   startedCounter += 1;
  97. }
  98.  
  99. void turnOff() {
  100.   digitalWrite(POWER_OFF_PIN, HIGH);
  101. }
  102.  
  103. void processMessage() {
  104.   while (Serial.available() > 0) {
  105.  
  106.     char c = Serial.peek();
  107.     // ignore newlines
  108.     if (c == 'S') {
  109.       Serial.println("OK");
  110.       Serial.read();
  111.       int pos = Serial.parseInt();
  112.       myservo.write(pos);
  113.     } else {
  114.       Serial.read();
  115.     }
  116.   }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement