Thursday, 23 October 2008

Pumpkin - Putting codes together

Grrr, I thought it would never happen. How difficult can it be to put a few codes together and make it work?

Well, my aim is to play a melody and turn on a LED as soon as you get at a certain distance to the ping sensor. I had already managed to make the LED when getting close to the sensor. Now I also need to make the piezo buzzer play music, or at least a few tones.

Although, there are a few examples on the Internet, none of them exactly did what I was looking for. Some just simply played a melody with no interaction and some played a note sent via the serial monitor. After a lot of testing, I was able to take the parts I need and put them together in the right order.

Though, the code still needs improvement, I came up with a working code! The secret was to write an additional function (scale(), for example) and to call it when it is needed within 'void loop()'. So, the 'loop' function would test all over again whether the ultra sound value is higher than 200. If that is the case, the 'scale' function would start.

The next step will be to go over the code and tidy everything up a bit, as well as to get red of unnecessary code. Moreover, I will need to find a melody that goes with Halloween.

No comments: