Setting a timer to count analogRead input for 3 seconds

Discussion in 'Embedded Systems and Microcontrollers' started by Rickpercy87, May 5, 2016.

  1. Rickpercy87

    Thread Starter New Member

    Nov 20, 2015
    16
    0
    I should mention this is for Arduino.

    Hello, I would like the analogRead function to count the input from a sensor over three seconds.
    Basically my analogue read it reading about ten values per second.
    I would like it to add the total number counted from the analogue input over three seconds and output and ALARM if a threshold is exceeded.

    Thank you:
    Here is the code I have so far:
    Code (C):
    1. const int analogPin = 0; // input from sensor
    2. const int threshold = 3; //threshold sensor warning alarm
    3. int ledPin = 13;
    4. int buzzerPin = 8;  //alarm
    5. void setup()
    6.  
    7. {
    8.   Serial.begin(9600);
    9.   pinMode (ledPin, OUTPUT);
    10.   pinMode (buzzerPin, OUTPUT);
    11.  
    12. }
    13.  
    14. void loop() {
    15.   int mn = 1023;     // mn only decreases
    16.   int mx = 0;        // mx only increases
    17.  
    18.   // Perform 750 reads. Update mn and mx for each one.
    19.   for (int i = 0; i < 750  ; ++i) {
    20.     int val = analogRead(analogPin);
    21.     mn = min(mn, val);
    22.     mx = max(mx, val);
    23.  
    24.   }
    25. {
    26.  
    27. if (threshold > mn){
    28.  
    29.  
    30. digitalWrite (ledPin, HIGH); // lights the LED
    31. delay (100); // time LED stays lit once a rigger is met
    32. tone (8, 1500, 100); // warning alarm once the threshold is met
    33. delay (100);
    34. tone (8, 1000, 100);
    35. delay(100);
    36. tone (8, 1500, 100);
    37. delay (100);
    38. tone (8, 1000, 100);
    39. delay(100);
    40. tone (8, 1500, 100);
    41. delay (100);
    42. tone (8, 1000, 100);
    43. delay(100);
    44. tone (8, 1500, 100);
    45. delay (100);
    46. tone (8, 1000, 100);
    47. delay(100);
    48. }
    49.  
    50. else {
    51. digitalWrite (ledPin, LOW); //LED is turned off if there is no movement
    52. analogWrite (buzzerPin, LOW);
    53. }
    54.  
    55.   // Send min, max and delta over Serial
    56.   Serial.print("m=");
    57.   Serial.print(mn);
    58.   Serial.print(" M=");
    59.   Serial.print(mx);
    60.   Serial.print(" D=");
    61.   Serial.print(mx-mn);
    62.   Serial.println();
    63. }}
    64.  
    Mod edit: Please use code tags to post code.
     
    Last edited by a moderator: May 5, 2016
  2. mcgyvr

    AAC Fanatic!

    Oct 15, 2009
    4,771
    971
    So you want to get 30 readings (10 per second) over 3 seconds?
    How accurate on the time and number of readings do you need it to be?
    Exactly how fast can you get 1 reading now?
     
  3. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    Wouldn't just be adding up readings over 3 seconds, or adding up 3x10 readings?

    So approach 1

    Code (Text):
    1.  
    2.   time_start=time(); //start time now
    3.   sum=0;
    4.   while (time() - time_start < 30 seconds) sum+=adc_reading();
    5.  
    or approach 2
    Code (Text):
    1.  
    2.   count=0;
    3.   sum=0;
    4.   while (count++ < 30) sum+=adc_reading();
    5.  
    Pick whatever you want.
     
Loading...