Advertisement
Guest User

Untitled

a guest
Jul 6th, 2015
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.00 KB | None | 0 0
  1. const unsigned long sampleTime = 100000UL;  // sample over 100ms, it is an exact number of cycles for both 50Hz and 60Hz mains
  2. const unsigned long numSamples = 250UL;     // choose the number of samples to divide sampleTime exactly, but low enough for the ADC to keep up
  3. const unsigned long sampleInterval = sampleTime/numSamples;  // the sampling interval, must be longer than then ADC conversion time
  4. const int adc_zero = 511;
  5.  
  6. float current = 0.0f;
  7. const int currentPin = 0;
  8.  
  9.  
  10. void setup() {
  11. }
  12.  
  13. void loop() {
  14.  unsigned long currentAcc = 0;
  15.  unsigned int count = 0;
  16.  unsigned long prevMicros = micros () - sampleInterval;
  17.  
  18.  while (count < numSamples) {
  19.    if (micros () - prevMicros >= sampleInterval) {
  20.      int adc_raw = analogRead (currentPin) - adc_zero;
  21.      currentAcc += (unsigned long) (adc_raw * adc_raw);
  22.      ++ count;
  23.      prevMicros += sampleInterval;
  24.    }
  25.  }
  26.  
  27.  float rms = sqrt((float)currentAcc/(float)numSamples) * (75.7576 / 1024.0);
  28.  
  29.  current += 0.2f * (rms - current);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement