Advertisement
baldengineer

max analog Read example

Jul 26th, 2015
383
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.98 KB | None | 0 0
  1.  
  2. int maxReading=0;
  3. int maxIndex=0;
  4. int readings[8];
  5.  
  6. void goRead(int readIndex) {
  7.   // see what the analog value is
  8.   int currentReading = analogRead(A0);
  9.  
  10.   // is it bigger than our already known max value?
  11.   if (currentReading > maxReading) {
  12.     // okay, save the max value and the index that it happened
  13.     maxReading = currentReading;
  14.     maxIndex = readIndex;
  15.   }
  16.   // store our readings, for ... reasons?
  17.   readings[readIndex] = currentReading;
  18.  
  19. }
  20.  
  21. void setup() {
  22.   // put your setup code here, to run once:
  23.  
  24. }
  25.  
  26. void loop() {
  27.   //reset your maxes
  28.   maxReading = 0;
  29.   maxIndex=0;
  30.  
  31.   // whatever happens, first read
  32.   goRead(0);
  33.   // more stuff happens
  34.   goRead(1);
  35.   // more stuff happens
  36.   goRead(2);
  37.   // more stuff happens
  38.   goRead(3);
  39.   // more stuff happens
  40.   goRead(4);
  41.   // more stuff happens
  42.   goRead(5);
  43.   // more stuff happens
  44.   goRead(6);
  45.   // more stuff happens
  46.   goRead(7);
  47.  
  48.   // maxReading and maxIndex are your max values...
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement