Advertisement
alexanderik

Arduino Power Counter

Apr 12th, 2015
385
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.88 KB | None | 0 0
  1. #include <avr/sleep.h>
  2. #include <avr/wdt.h>
  3.  
  4. #ifndef cbi
  5. #define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
  6. #endif
  7. #ifndef sbi
  8. #define sbi(sfr, bit) (_SFR_BYTE(sfr) |= _BV(bit))
  9. #endif
  10.  
  11.  
  12. /*
  13. #define LANG_ELECTRICITY 'E'
  14. #define MESG_ELEC_CURRENT 'c'
  15. typedef struct {
  16.     char lang;
  17.     char mesg;
  18.     double power;
  19.     double elapsedkWh;
  20. } Packet_t;
  21.  
  22. */
  23.  
  24. #define SRAM __attribute__((section(".noinit")))
  25.  
  26. //Number of pulses, used to measure energy.
  27. long pulseCount = 0;  
  28. //Used to measure power.
  29. unsigned long pulseTime,lastTime;
  30.  
  31. //power and energy
  32. double power, elapsedkWh;
  33.  
  34. //Number of pulses per wh - found or set on the meter.
  35. int ppwh = 1; //1000 pulses/kwh = 1 pulse per wh
  36.  
  37. #define sleepmode 1
  38.  
  39. void setup()
  40. {
  41.   Serial.begin(115200);
  42.  
  43. // KWH interrupt attached to IRQ 1  = pin3
  44.   attachInterrupt(0, onPulse, RISING);
  45.  
  46.   cbi( SMCR,SE );      // sleep enable, power down mode
  47.   cbi( SMCR,SM0 );     // power down mode
  48.   sbi( SMCR,SM1 );     // power down mode
  49.   cbi( SMCR,SM2 );     // power down mode
  50. }
  51.  
  52.  
  53.  
  54. void loop()
  55. {
  56.   //-------------------------------------------------------------
  57.   // 1) Enter sleep mode
  58.   //-------------------------------------------------------------
  59. #if sleepmode
  60. /*
  61.  The 5 different modes are:
  62.      *     SLEEP_MODE_IDLE         -the least power savings
  63.      *     SLEEP_MODE_ADC
  64.      *     SLEEP_MODE_PWR_SAVE
  65.      *     SLEEP_MODE_STANDBY
  66.      *     SLEEP_MODE_PWR_DOWN     -the most power savings
  67.      *
  68.  */
  69.  
  70.  cbi(ADCSRA,ADEN);    // switch Analog to Digital converter OFF
  71.  
  72.   set_sleep_mode(SLEEP_MODE_PWR_DOWN);
  73. //  cli();
  74.   sleep_enable();
  75.   sleep_mode();
  76.   sleep_bod_disable();
  77.   sei();
  78.   sleep_cpu();
  79.  
  80. // The arduino is now sleeping...
  81.  
  82.  //-------------------------------------------------------------
  83.   // 2) Program will resume from here on interrupt
  84.   //-------------------------------------------------------------
  85.  
  86.   sleep_disable();
  87.  sbi(ADCSRA,ADEN);    // switch Analog to Digitalconverter ON
  88.  Serial.println('P');
  89. Serial.println(millis(),3);
  90.    delay(10);
  91. Serial.println(millis(),3);
  92.    delay(20);
  93. Serial.println(millis(),3);
  94.    delay(10);
  95. Serial.println(millis(),3);
  96.    delay(10);
  97. #endif
  98.  
  99. }
  100.  
  101.  
  102. // The interrupt routine
  103. void onPulse()
  104. {
  105.  
  106.  
  107. //used to measure time between pulses.
  108.   lastTime = pulseTime;
  109.   pulseTime= micros();
  110.  
  111. //pulseCounter
  112.   pulseCount++;
  113.  
  114. //Calculate power
  115.   power = (3600000000.0 / (pulseTime - lastTime))/ppwh;
  116.  
  117.   //Find kwh elapsed
  118.   elapsedkWh = (1.0*pulseCount/(ppwh*1000)); //multiply by 1000 to pulses per wh to kwh convert wh to kwh
  119.  
  120. //Print the values.
  121.   Serial.print("W:");
  122.   Serial.print(power,4);
  123.   Serial.print(" kWh:");
  124.   Serial.print(elapsedkWh,3);
  125.   Serial.print(" C:");
  126.   Serial.print(pulseCount,4);  
  127.   Serial.print(" LT:");
  128.   Serial.print(lastTime,4);
  129.   Serial.print(" PT:");
  130.   Serial.println(pulseTime,4);  
  131.  
  132.  
  133.  
  134.  
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement