What is the function of this code


can help me to understand the code …
like how this code is work?




I’m not sure which part you want explained, so here’s the whole lot! :slight_smile:

The lines that start with void are functions that don’t return a value (their return type is void - nothing).

There are at least two functions in most Arduino programs. The first you see there is called setup and it is run/invoked once at the start of the program (when you supply power or press the reset button). The other function (loop) is called over and over and over in an endless loop.

Outside these functions, the Arduino can be interrupted by certain events. This might be buttons being pressed, or counters inside the circuitry. We can attach our own functions to these interrupts and do something in response to the event.

Now to the details:
In setup, the program starts serial communications at a baud rate of 9600. This allows you to see output that your code prints to serial in the serial monitor built into the Arduino IDE.
It then sets the mode of the pin numbered with the value of pin (which we can’t see in your screenshot) as an input pin meaning we intend to read the value of it rather than write a value to it.
It then attaches a function called count_pulse to pin 0 to be invoked/called when the state changes from low to high.

Looking at the interrupt function, I see that there’s a syntax error in the voidcount_pulse() line. A space is missing after void. It should be void count_pulse(). The function simply increments (adds one to) the value of the pulse variable.

The loop function sets pulse to 0 before enabling interrupts with the call to interrupts() and then delaying (sleeping) for 1000 milliseconds (one second). It then disables interrupts with a call to noInterrupts() before printing the number of pulses that were counted by the count_pulse function whilst it was sleeping. Then it does the same thing all over again. :slight_smile:


Pulse counter code

Hi ,
thank you very much the idea is much more clear now…
but just to be sure, the input of the Arduino is an analog with ones and zeros voltage values…

what the value I should put in serial. begin ()
9600 as it is or I change it to zero?

Thanks a lot



Leave it at 9600. It’s not an IO value, is the speed.



ok, got it .
Thanks a lot.



regarding Arduino connection we connected:

  • output of the comparator A0 pinout
  • Arduino (USB port connector) to the laptop
    is there any connection needed or is there any initiation step for the Arduino ?

cause when we connect the circuit … an error in Arduino’s code screen appeared saying that the board is null :confused:



You can start with just the Arduino connected to the computer with a USB cable. That’s all you need to program it. In the Arduino IDE, you have to select which USB port the Arduino is connected to and which model of Arduino you’re programming. Have you done this?



ya I have got it, thanks