Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

9. Project: Lamp > Program the Lamp

Program the Lamp

The Lamp sketch is surprisingly complicated. It must monitor a serial port and watch a zero-cross output, and make sure it starts the TRIAC at the right time. To do all this at the same time and keep the code readable, we’re using interrupts. An interrupt is exactly what its name suggests—a way for the currently running program to be interrupted. These sound esoteric, and they really do help code work well and stay readable, but they can introduce errors if you’re not used to thinking of your code being interrupted.

We attach the zero-cross line to digital pin 3, and attach an interrupt to it using attachInterrupt(). Every time this line goes from 5V to 0V, or “falls,” we’ll run a function. Similarly, we’re going to attach a function to the Timer 1 interrupt using the TimerOne library. This is an easy way to make sure that a function is called at a specific period, and it’s a bit more accurate than the


  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint