Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.49 KB | None | 0 0
  1. // number of analog samples to take per reading
  2. #define NUM_SAMPLES 10
  3.  
  4. int sum1 = 0;
  5. int sum2 = 0;  
  6. int sum3 = 0;  
  7. unsigned char sample_count = 0; // current sample number
  8. float voltage1 = 0.0;            // calculated voltage
  9. float voltage2 = 0.0;
  10. float voltage3 = 0.0;
  11.  
  12. void setup()
  13. {
  14.     analogReference(INTERNAL);
  15.      pinMode(A5, INPUT);
  16.      pinMode(A6, INPUT);
  17.      pinMode(A7, INPUT);
  18.     Serial.begin(9600);
  19. }
  20.  
  21. void loop()
  22. {
  23.     // take a number of analog samples and add them up
  24.     while (sample_count < NUM_SAMPLES) {
  25.         sum1 += analogRead(A5);
  26.         sum2 += analogRead(A6);
  27.         sum3 += analogRead(A7);
  28.         sample_count++;
  29.         delay(10);
  30.     }
  31.     // calculate the voltage
  32.     // use 5.0 for a 5.0V ADC reference voltage
  33.     // 5.015V is the calibrated reference voltage
  34.     voltage1 = ((float)sum1 / (float)NUM_SAMPLES * 5.015) / 1024.0;
  35.     voltage2 = ((float)sum2 / (float)NUM_SAMPLES * 5.015) / 1024.0;
  36.     voltage3 = ((float)sum3 / (float)NUM_SAMPLES * 5.015) / 1024.0;
  37.     // send voltage for display on Serial Monitor
  38.     // voltage multiplied by 11 when using voltage divider that
  39.     // divides by 11. 11.132 is the calibrated voltage divide
  40.     // value
  41.     Serial.print(voltage1 * 11.132);
  42.     Serial.println (" V ");
  43.     Serial.print(voltage2 * 11.132);
  44.     Serial.println (" V ");
  45.     Serial.print(voltage3 * 11.132);
  46.     Serial.println (" V ");
  47.     sample_count = 0;
  48.     sum1 = 0;
  49.     sum2 = 0;
  50.     sum3 = 0;
  51.     delay(1000);
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement