Start a loop with pushbutton - digitalRead vs. interrupt

Hi,

I need a simple program which waits for a button to be pressed and starts outputting a signal. That's what I came up with:


#define F_CPU 16000000 UL	// Set CPU frequency to 16 MHz
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

unsigned int i = 0;	// Interrupt count

ISR(INT0_vect)
{
	// If external interrupt INT0 gets triggered
	i++;
}

int main(void)
{
	DDRB |= 0B00100000;	// Pin 13 OUTPUT
	PORTB &= 0B11011111;	// Set Pin 13 LOW
	DDRD &= 0B11111011;	// Pin 2 INPUT
	EICRA |= 0B00000011;	// Rising edge interrupt on pin 2
	EIMSK |= 0B00000001;	// Enable interrupt on Pin 2
	sei();	// Set global interrupt mask

	while (1)
	{
		while (i)
		{
			PORTB |= 0B00100000;	// Set Pin 13 HIGH
			_delay_us(1000);
			PORTB &= 0B11011111;	// Set Pin 13 LOW
			_delay_us(34000);
		}
	}
}

The problem is, that the time between the button press and signal start is not constant and varies between 3.5 and 4 μs. Would it be better to watch the PIND register for a change on D2 of the Arduino instead of using an interrupt? Are there any other methods that would result in a constant time?

Thanks for your help.

If you are using an UNO, the finest resolution is 4us. You can move to a faster processor, like one of the teensies.

and then starting the signal is required to start at a repeated precision of 0,000004 seconds?

are you sure that you can press the button with a timely precision of just 0,01 seconds?

I highly doubt it.

either this precision is not nescessary or it is not a button but a full electronic IO-pin

The button will be replaced by a light switch (edit: photoelectric sensor) in the future. It'll be used to detect an object on a motorized rail passing by. As the light switch turns on, the signal has to be sent continuously. Since this has to be repeated several times, we would have to achieve a constant time between the light switch turning on and the start of the signal. 4 μs would be ok, but it's always changing a bit.

Hello :raised_hand:, you can try to use timer instead od delay because it will be more accurate.

Here you can find more information :information_source:

increment variable "i" by one each time the interrupt occurs

.
.
as long as variable i is not zero run this while-loop

.
.
The first interrupt occurs variable i will be 1
your while loop is running

The second interrupt occurs i will be 2
your while-loop just keeps on running

The third interrupt occurs i will be 3
your while-loop just keeps on running

where is the problem?

Do you restart this microcontroller over and over again?

Is this just a part of your code?

If you do an internet-search for the term light switch
you get such pictures

Is this the kind of "light switch" you are talking about?

Sorry, my bad. It's a photoelectric sensor. I should've googled up the English term for it.

You made the common mistake of guessing what is required to make an ISR work reliably on your platform. You need some things like protection around your critical section, 'volatile' keyword for shared variables, and probably more I can't remember right now. Please research it, apply those fixes, and re-post your revised sketch.

Go here for help:
https://gammon.com.au/interrupts

1 Like

Hi @afkisch,

it is still not quite clear what you want to achieve ...

  • Could you make a sketch/picture of your setup?
  • Why is the time between the trigger from the photoelectric sensor and the signal start so relevant?
  • What do you mean with "the signal has to be sent continuously"?
  • What is the time difference between two consecutive triggers of the photoelectric sensor?

I assume that a certain "jitter" is not avoidable this way as the interrupt is not "synchronizable" with the other functions that the controller performs (while()-loop, timer interrupts to handle micros() and millis() ...).

ec2021

I want to second that.

What you have described so far is a certain detail. This detail is not a self-purpose.
This detail serves to a final purpose that your whole project has.

If you describe the complete project more suggestions to achieve the final purpose can be made

I want to describe what might happen if you only post some details#
details versus overview:
We are talking about details. Please give an overview over your whole project.
in mimimum 70% of all cases knowing the whole thing offers completely different and much better working solutions.

This is like

here is an analogon that shall show what can happen if just ask for details:

questioner: "I want to do better cutting please help me sharpening. "
Expert: Sure I can help you. What kind of cutting-tool are you using?
(expert asks back for details the questioner left out slowing down getting a solution)

questioner: a scissor.
Expert: OK take this sharpening tool
questioner: Yea works great Next question How can I make it cut faster I need to finish faster.
expert: Motorised scissors.
questioner Yea works great though still not fast enough.

questioner: Ok can you give an overview about what you are cutting.
questioner: the green of a football-arena.
expert: Oha! take a big mowing tractor with a seven boom spindel-mower and GPS-steering

In the beginning the questioner always just told details.
The expert was assuming the questioner knows that his basic approach is well suited.
which turns out to be very bad suited
that's the reason why it is always a good idea to give an overview and to explain what shall happen in the end. The final purpose of a project.

best regards Stefan

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.