daily pastebin goal
47%
SHARE
TWEET

ac arduino

a guest Oct 16th, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <Arduino.h>
  2.  
  3. #define SQRT2 sqrt(2)
  4. uint16_t adc_min, adc_max, adc_vpp, adc_now;
  5. float adc_rms, adc_valx;
  6.  
  7. void setup() {
  8.   delay(1000);
  9.   Serial.begin(57600);
  10.  
  11.   adc_min = 1024;
  12.   adc_max = 0;
  13. }
  14.  
  15. void loop() {
  16.  
  17.   uint32_t tn = millis();
  18.   adc_min = 1024;
  19.   adc_max = 0;
  20.   while (millis() - tn <= 50) {
  21.     adc_now = analogRead(0);
  22.     if (adc_now < adc_min) {
  23.       adc_min = adc_now;
  24.     }
  25.  
  26.     if (adc_now > adc_max) {
  27.       adc_max = adc_now;
  28.     }
  29.   }
  30.   // peak to peak value
  31.   adc_vpp = (adc_max - adc_min);
  32.   adc_rms = ((float)adc_vpp / 2.0) / SQRT2;
  33.   adc_valx = adc_rms * 5 / 1024;
  34.  
  35.   // Serial.println(millis() + " : " + String(adc_valx*63*2, 3) + " -- " +
  36.   //                String(adc_rms, 3) + " -- " + String(adc_vpp, DEC) + " -- "
  37.   //                +
  38.   //                String(adc_min, DEC) + " -- " + String(adc_max, DEC));
  39.   Serial.println(millis() + " : " + String(adc_valx * 63 * 2, 3));
  40. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top