I need to program a toggle switch in arduino to be able to toggle between two modes. As a start I played around toggling LEDs and it worked out fine but I am having issues using it to switch between different sections of my program. Within the program a user should have a choice to go into either learn mode or trigger mode when using the device.
Here is what I have as a basic program:
I thought that I should be able to replace LEDs with my respective functions, but it does not seem to be working. I've tried a few other things, but they only end in one mode stopping and restarting. I would appreciate any suggestions (except replacing this with a push button
)
Here is what I have as a basic program:
Rich (BB code):
byte switchPin = 2; //pin to which switch is connected
byte ledPin = 13;
volatile boolean trigger = LOW; //interrupt attached to the pin
void setup()
{
attachInterrupt(0, interrupttrigger, LOW); //digital pin 2 is interrupt 0
pinMode(ledPin,OUTPUT);
}
void loop()
{
delay(20); //wait to debounce the switch
if (trigger = !trigger) //check if interrupt was triggered
{
LED(); //go execute the function
//use this to execute one function in one state
}
else
{
analogWrite(ledPin, 0); //turn LED off
//execute another function in another state
}
}
void LED()
{
delay(200);
while(trigger == LOW)
{
digitalWrite(ledPin, HIGH);
}
}
void interrupttrigger()
{
if(trigger == LOW) //reset switch
{
trigger = HIGH;
}
else
{
trigger = LOW;
}
}