This exercise introduced me to a new world of embedded programming. The first issue was that by hello world board from two weeks ago had had broken. So I made a new board; setting a personal record of just 30 mins!
First Up I made a small arduino script that made the LEDs flash in a sequence. I then added the code for a button press, with debounce. The intention was to turn off all the lights with this button, however this didn't have the desired effect because the button was only registered at a certain point in the code, not at any point. You could toggle the LEDs on and off, but only when the flashing sequence was complete.
The key problem with interupts in arduino is that the
delay function takes up the whole processor. I wanted to try and stop the LEDs mid flash. The wisdom online, is to use
millis() as a time keeper to maintain flashing rather then delays to split chunks of time.
The second difficulty was in getting an button push to register mid script. Arduino can create it's own interupt but I wasn't able to make this work. Instead I used object oriented programming to create three threads on the script - one for each LED. This was cool for me as it allowed the chip to have multiple strands of unique calculations ticking at once. The button was then placed in the main void, and was able to interupt the flashing..
Interupts are much easier in C. However this also meant learning how to code interupts into C. Coding the interupt itself was quite straight forward
ISR(PCINT0_vect). APCINT0 captures any signal on the PORTA. However I also had to activate the interupt pin, set interupt pins high with pull up resistors, and activate interupts within the program embedded thoughts. After a bit of trial and error I worked out a system that could work, and the general logic of setting individual pins using mask functions.
stop with three lights on!