1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Arduino Coding..

Discussion in 'Arduino' started by PhilipWarence, Jan 26, 2015.

  1. PhilipWarence

    PhilipWarence New Member

    Joined:
    Jul 14, 2014
    Messages:
    26
    Likes:
    0
    I have a problem in coding this graph in arduino. As i display the serial monitor, it logs analog values up to 730units. the value is indicated in the attached graph..
    My objective is to count how many pulses(high) there have in a defined time.
    Can I ask for a help. Thanks in advance..
    Very much appreciated!
     

    Attached Files:

  2. MikeMl

    MikeMl Well-Known Member Most Helpful Member

    Joined:
    Mar 17, 2009
    Messages:
    11,139
    Likes:
    566
    Location:
    AZ 86334
    You just want to count them without outputting them to the serial monitor?
     
  3. PhilipWarence

    PhilipWarence New Member

    Joined:
    Jul 14, 2014
    Messages:
    26
    Likes:
    0
    Yes Sir, The value that'll be displayed on the monitor should be the amount of pulses it have in a defined time, for example 60seconds..
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. MikeMl

    MikeMl Well-Known Member Most Helpful Member

    Joined:
    Mar 17, 2009
    Messages:
    11,139
    Likes:
    566
    Location:
    AZ 86334

    The pulses seem to be coming from an AD input (analogRead())?
     
  6. PhilipWarence

    PhilipWarence New Member

    Joined:
    Jul 14, 2014
    Messages:
    26
    Likes:
    0
    Exactly Sir! It's from analog out of my cicuit and going to the arduino Analog pin.
     
  7. MikeMl

    MikeMl Well-Known Member Most Helpful Member

    Joined:
    Mar 17, 2009
    Messages:
    11,139
    Likes:
    566
    Location:
    AZ 86334
    This shows how to time a pulse that comes from a digital input. Modifying it so the pulse comes from the AD is simple. This shows how to use the millis() function to time things. You can use it to count things just as easily.


    Code (text):

    const byte Squelch = 2;                   // Receiver Squelch is connected to pin 2

    const byte Led = 13;                        // Uno's built-in LED, used to indicate for debugging

    long  time=0;                                   // declare time to be long

    ...                                                       // Wait for Rising Edge and return Duration of the Pulse
    { while(!(digitalRead(Squelch)))    //Wait for rising Edge      
     
      time = millis();                              //store time now  
      digitalWrite(Led,true);                 //Turn On Led
      while(digitalRead(Squelch))        //Wait for falling Edge    
     
      Dur=millis()-time;                       //Compute duration of On time
     
     
     
  8. PhilipWarence

    PhilipWarence New Member

    Joined:
    Jul 14, 2014
    Messages:
    26
    Likes:
    0
    Im having a problem in what point to set before the variable will increment because the analog reading is a series of 0's and exponentially goes up to series of around 730 and drop down to series on 0's again without a definite time when and how long the pulse is. The time each pulse last is different to each other..
     
  9. PhilipWarence

    PhilipWarence New Member

    Joined:
    Jul 14, 2014
    Messages:
    26
    Likes:
    0
    long A = 0;
    int x = 0;
    long pulse;
    long counter = 0;
    void setup()
    {
    pinMode(A0, INPUT);
    Serial.begin(9600);
    delay(1000);
    for(x = 0; x<=60000; x++){
    A = analogRead(A0);
    if(A >= 20){
    pulse = pulse + A;
    counter = counter + 1;
    }
    }
    Serial.println(counter);
    Serial.println(pulse);
    }

    void loop()
    {
    A = analogRead(A0);
    delay(50);

    Serial.print("Heart Rate = ");
    Serial.println(A);
    delay(1000);

    }
     
  10. PhilipWarence

    PhilipWarence New Member

    Joined:
    Jul 14, 2014
    Messages:
    26
    Likes:
    0
    But that is a wrong code :D
     
  11. flat5

    flat5 Member

    Joined:
    Oct 26, 2008
    Messages:
    866
    Likes:
    8
    Location:
    Amsterdam
    MikeMl, your post #6 might be just what I need in my Morse decoder thread.
    Thank you!
     
  12. be80be

    be80be Well-Known Member

    Joined:
    Aug 23, 2008
    Messages:
    4,869
    Likes:
    144
    Location:
    morristown,tn
    You can't count a analog pin your setting it with this A = analogRead(A0) back to ADC that's going to give you a value of 1 to 1024.
    What your seeing is the voltage go from 0 to around 3.5 volts to count if it goes up to 3.5 you need to do some thing like this.
    If A= >720
    counter = counter +1
     
    Last edited: Mar 27, 2015

Share This Page