Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.74 KB | None | 0 0
  1. /* ========================================================================== */
  2. /* adc.ino */
  3. /* */
  4. /* Code for reading/averaging ADC channels */
  5. /* */
  6. /* */
  7. /* */
  8. /* ========================================================================== */
  9.  
  10. // Variables
  11.  
  12. unsigned long CheckADCChannels=0;
  13. #ifdef A0_MULTIPLIER
  14. unsigned int Channel0Readings[5];
  15. unsigned int Channel0Average;
  16. #endif
  17.  
  18. void SetupADC(void)
  19. {
  20. #ifdef A0_MULTIPLIER
  21. analogReference(INTERNAL);
  22. // Serial.println("Setup A0");
  23. pinMode(A0, INPUT);
  24. #endif
  25. }
  26.  
  27. void CheckADC(void)
  28. {
  29. if (millis() >= CheckADCChannels)
  30. {
  31. #ifdef A0_MULTIPLIER
  32. Channel0Average = ReadADC(A0, A0_MULTIPLIER, Channel0Readings);
  33. // Serial.print("Average=");Serial.println(Channel0Average);
  34. #endif
  35.  
  36. CheckADCChannels = millis() + 1000L;
  37. }
  38. }
  39.  
  40. unsigned int ReadADC(int Pin, float Multiplier, unsigned int *Readings)
  41. {
  42. int i;
  43. unsigned int Result;
  44.  
  45. for (i=0; i<4; i++)
  46. {
  47. Readings[i] = Readings[i+1];
  48. }
  49.  
  50. Readings[4] = analogRead(Pin);
  51. //Serial.print("A0=");Serial.println(Readings[4]);
  52.  
  53. Result = 0;
  54. for (i=0; i<5; i++)
  55. {
  56. Result += Readings[i];
  57.  
  58. }
  59. // float Result;
  60. Result = analogRead(Pin);
  61.  
  62. // return (float)Result * Multiplier / 5.0;
  63. return (((float)Result /1024) * 1.1) * Multiplier ;
  64.  
  65. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement