tags:

views:

302

answers:

3
+1  Q: 

About interrupts?

+1  A: 

Option 1 is the simplest, you just create a timer that will interrupt your program at regular intervals. You say you are programming in C but specify no platform, timers are platform dependent.

In Unixland, timers send signals to process upon completion. Specifically, a timer implementation for Linux is setitimer.

You then have to catch and handle the signals sent by the timer.

In the Win32 API you can use timers as well, but the concept is somewhat different.

Vinko Vrsalovic
+2  A: 

You could just make a multithreaded application and have some treads act like interrupts when thay awake.

Thread one: Traffic controle Thread two: Simulate traffic approching from east and requesting a change in ligthing, and randomly sleeping from 1 to 60 seconds before awakening again. Thread X use imagination.

In gcc this could be done easy if you have the pthread lib for one example.

eaanon01
Thanx for editing my spelling error Gamecat
eaanon01
A: 

Get hold of a copy of Microsoft® Win32® Programmer's Reference Help File: Win32.hlp

Seek SetTimer and related functions. This function registers on windows that you want a function to be called after a specific interval (from the moment you're calling setTimer it). My suggestion is that on that function you: - kill this timer and set another one just like it (i.e., same interval, same function); - increment a counter; - test the counter against constants;

I.e. Counter=10, change to yellow, =20, to green, =30 to red and reset counter.

If the idea is to have the traffic light react to something else, substitute the counter logic by that "something else".

The main() of your program is just your timer initialization and a while(1). The rest is done by the timer-called function.

jpinto3912