Step 6The Program
In order to program your ATMega168 with the USB connection and Arduino board, you will need an ATMega168 chip that has the Arduino bootloader already on it. This was the simplest way, that I could find, to program the microcontroller. When I purchased my board, I picked up an extra ATMega168 with the bootloader from the same supplier. You may have to pay a bit more for the pre-programmed chip, but it was worth it to me since I did not want to mess with serial cable adapters etc.
I've attached the code as a .txt file and a .pde file. I didn't want to make this instructable long by posting all the code. I used the latest Arduino programing environment: arduino-0015. What I love about the Arduino boards is that there are tons of examples included with the environment, the program environment is free, and there are some many project and help pages out there. It's also super easy to build a breakout board to run your program on it's own. I tried to comment the code to the best of my understanding so I'll keep the descriptions to a minimum.
I used the "Fading LED" example by BARRAGAN to become familiar with the pulse width modulation (PWM) the ATMega168 is capable of. I have three "if" statements. The first faded in the lower dimness levels (0-75 out of 255) slower since the higher levels look the same. The second fades in the upper dimness levels faster. The whole fade in process takes 15 minutes. Once the LEDs have reached full brightness, the song loop will play until the alarm is shut off.
The original alarm was fairly annoying. It was just the typical alarm clock sound that everyone hates. I thought, why not use the speaker to make a pleasant song to wake up to? Since my girlfriend loves The Beatles and I knew Hey Jude has a fairly simple melody, I decide to use it. A square wave is generated and then PWM is used to play the notes of Hey Jude on the piezoelectric speaker. To program the song, I manipulated the "Melody" example from the Arduino environment examples. I found some simple sheet music and translated that into notes in the code. I had to increase the number of notes that are played to 41 and do the math to figure out a lower note than the 'c' that is provided. I then implanted that code into my main code.
To program the chip, you will first need to install the USB drivers provided with the Arduino environment. Then, select your board from the drop down menu and select the appropriate COM port. This whole process is described in detail here: http://arduino.cc/en/Guide/Windows
And that's about it! After programming the ATMega168, it can be taken out of the Arduino and popped into the prototyped circuit!
| « Previous Step | Download PDFView All Steps | Next Step » |
![]() |
Add Comment
|















































