Using hardware switch to allow switching between programmed tasks?

Hi,

To save everybody time the question is: If a latched switch is depressed can the arduino uno run significantly different code than if a switch is not depressed? The rest of this post is included in case this sounds ambiguous.

For a project I'm working on I am hoping to record ECG's, do some heart beat detection and then write the data to an SD card. I'm expecting this to use up most of the available RAM on the arduino uno. Am I right in the assumption that if I added a hardware switch that if it was in one position I could do the acquisition, heart beat detection and writing to an sd card and if it was in the other position it would be possible to read the data off the sd card and do other processing on it?

I think it should be possible but I wanted to check before buying a SD shield and hardware switch.

I was thinking something like this (obviously not real code):

If switch=position 1 { record signal beat detection algorithm write to card } else if switch = position 2 { read from sd card signal processing }

If a latched switch is depressed can the arduino uno run significantly different code than if a switch is not depressed?

No. It can run a different portion of the code, though.

You could test your concept without the SD card, and a piece of wire for a switch.

Set a pin to be an input with pullup enabled, then the input will be high. Make it go low by having a wire in GND and put the other end in your chosen pin. Then your pseudo code simply becomes:

If pin is high
{
maybe switch on led 13
serial print a note to say "hi"
}
else
{
switch off led 13
serial print a note to say "lo"
}

Thanks guys.

Thanks Jimbo, that really should've occurred to me by now; as always its so simple when you know how though :)

if a latched switch is depressed can the arduino uno run significantly different code than if a switch is not depressed?

As you can see above, the answer is yes.

For a project I'm working on I am hoping to record ECG's, do some heart beat detection and then write the data to an SD card. I'm expecting this to use up most of the available RAM on the arduino uno.

Were you hoping somehow to give up this RAM for the data processing piece? If so, this may be one of the rare occasions where dynamic memory allocation (malloc) makes sense on an Arduino.

Hi Bill,

Yes, that was my hopes. Primarily because the heart beat detection will be using quite a lot of RAM (relatively speaking) so I was hoping to run it separately so that I could then free the RAM up for other applications. Thanks for the tip about using malloc(), I hadn't actually considered that yet but will have to look into it in more detail.