[Solved]Could you help me to understand the timer interrupt

MrChips

Joined Oct 2, 2009
34,889
But it is very important for me to know why you think that this problem can be solved by the state machine.
Read the title of your thread.

Could you help me to understand the timer interrupt

If you intend to use timer interrupts, how do you propose to use it?
 

MrChips

Joined Oct 2, 2009
34,889
A flowchart shows the flow of a process.

1597199182510.png

@hexreader has given you a simple solution in post #65 based on a simple flowchart along these concepts:

yellow off, red on -> delay for 1000ms -> red off, green on -> delay for 500ms -> green off, yellow on -> delay for 200ms ->

A state diagram and a flowchart are two different things.
A state diagram shows all possible states of an element and how to get to and from that state.

1597199100979.png
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
A state diagram and a flowchart are two different things.
A state diagram shows all possible states of an element and how to get to and from that state.

View attachment 214580
MrChips My question is still the same, how do you find out that the state diagram will be useful why I am not able to think so? Where am I making a mistake in understanding the usefulness of the state diagram

Did you see my state diagram which is in another thread post #50, does it looks correct?

What is the actual purpose of your given state diagram, what works it does?
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Timer interrupts every 100ms.
When timer interrupts, increment a software counter.
Reset counter at 17.

Counter
0
1
2
3
4
5 Green OFF, Yellow ON
6
7 Yellow OFF, Red ON
8
9
10
11
12
13
14
15
16
17 = 0 Red OFF, Green ON
Does it makes any sense

IMG_20200812_163748.jpg
 

MrChips

Joined Oct 2, 2009
34,889
No. You are attempting to draw a diagram based on what I wrote.
That is doing it backwards.
You draw the diagram first based on what YOU think ought to happen.

These are mistakes students make.
1) They add comments to their code after the code is written because their instructor says comments are required.
2) They draw a flowchart after the code is written otherwise they get low marks for the assignment.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
No. You are attempting to draw a diagram based on what I wrote.
That is doing it backwards.
You draw the diagram first based on what YOU think ought to happen.
Problems description

1. Red ON, wait 10000 counts, Red OFF

2. While Red ON, I want Green ON, wait 5000 counts, then Green OFF

3. While Red and Green ON, I want Yellow ON wait 1000 counts then Yellow OFF

4. Repeat process

IMG_20200812_191934.jpg

Hope it describes correct
 

MrChips

Joined Oct 2, 2009
34,889
You are not thinking this through intelligently.
How are you going to detect when the timer register is equal to 60536, for example?
 

MrChips

Joined Oct 2, 2009
34,889
No. That will not work.
The timer is incrementing faster than your computer is processing.
You will never be able to catch the timer register at an exact value.

Read the title of your thread.

Could you help me understand the timer interrupt

As I already pointed out, you need to get the timer to trigger an interrupt at a defined interval, preferably at not too fast rate, milliseconds is ok.

In my solution, the timer triggers an interrupt once every 100ms. Done. That is all you need to know in order to work out a solution.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
Ok so the response time of led is slow so we don't need timer interrupt

Is this diagram looks correct from the perspective of a state diagram?

IMG_20200813_094442.jpg
 

trebla

Joined Jun 29, 2019
599
LED response time is critical if you are reading LEDs pulses with some optical sensors in high speed. Othewise only human eye response time matters there.
 

jpanhalt

Joined Jan 18, 2008
11,087
Still don't see why you have the 4th state. There is no such thing a "0" time.

1597316201008.png

If you want a 4th state, try Red on, yellow on, green off after Red on and everything else off. Years ago, some signals had that to anticipate green coming on. The obvious happened happened when the anticipators and yellow-light runners met.

It is now illegal in the US. I suspect that is why the pedestrian countdown signs are difficult to see from a car unless you are right at the crosswalk.
 

jpanhalt

Joined Jan 18, 2008
11,087
My comment about traffic lights was meant as an "aside." It's a problem with multiple, related, perpetual threads without any real question to be addressed.

Since you think there is "0" time. How will you code for that? Does a "state" with no existence have a purpose?
 

jpanhalt

Joined Jan 18, 2008
11,087
OK. Since you seem to be in lockdown and bored stiff, consider this problem:

You have three lights (R,G, and Y) and each is individually turned on or off by an interrupt/switch. There are 8 states:
a) All off (1 state)
b) One on and two off (3 states)
c) Two on and one off (3 states)
d) All on (1 state)

What is the fewest number of steps you can devise to get all 8 states if ONLY ONE light can be changed at a time? Show each step.
 

Thread Starter

Djsarakar

Joined Jul 26, 2020
489
What is the fewest number of steps you can devise to get all 8 states if ONLY ONE light can be changed at a time? Show each step.
So LED takes time to turn on, off, you have not mention this delay time

Which sequence of RGB do you want when a switch is pressed or an interrupt occur?

IMG_20200813_180128.jpg
 

MrChips

Joined Oct 2, 2009
34,889
What do you mean by LED has response time?
Define response time.
What do you mean by LED takes time to turn on?
What does this have to do with interrupts?
 
Top