HI!
I am currently working on a project where I have a switch-button that I want to make the arduino run one out of two programs, depending on what state the switch is in.
I was thinking that I could use an interrupt for both states that the switch could be in, and make the interrupts send me to a function where the program would be.
// **** Declaring functions ****
void modeManual(void);
void modeAuto(void);
//*******************
void setup(void)
{
// Setting up serial-communication
Serial.begin(9600);
Serial.print("Press a button!\n");
// Attaches an interrupt to the switch-states
attachInterrupt(4, modeManual, RISING);
attachInterrupt(5, modeAuto, RISING);
}
//*****************
void loop(void)
{
}
//************ FUNCTIONS *************
void modeManual(void) // This is the program for when the device is set to mode1
{
Serial.print("modeManual\n"); // Feedback, to see if it actually enters the function
}
void modeAuto(void) // This is the program for when the device is set to mode2
{
Serial.print("modeAuto\n"); // Feedback, to see if it actually enters the function
}
But as you can see at the pictures, things are not working as I planned..
ADDITIONAL INFORMATION:
The switch has three pins. I'm using one for 5V input, and the position of the switch decides where the current goes. I'm using a 10K Ohm ressistor on both positions, so that the pin that is not in position should give out a GND-signal
I even tried adding this in both functions, but still got the same result as shown in the picture above...
void modeManual(void) // This is the program for when the device is set to manual mode
{
// Trying to prevent de-bouncing
noInterrupts();
delay(1000);
interrupts();
Serial.print("modeManual\n");
}
Why interupts?
if switch closed do program A
else (it must be open if it's not closed!) do program B
You only need to connect one side of your switch for this.
ViggoTW:
I need to be able to switch to the other program anywhere in the first program, without resetting
This is typically done by just reading the switch inputs sequentially in your loop function and then calling functions you create to accomplish what you want each switch input to perform. Using interrupts typically are required only for very high speed switching requirements.
I need to be able to switch to the other program anywhere in the first program, without resetting
If you interrupt 1 routine to call another and then interrupt again you have entered the world of reentrant code - a very bad idea on an Arduino. The program stack will pile up and soon crash.
I would also recommend a polling solution as simpler and cleaner. You must code your two "programs" so that they periodically look at the switch. When the switch change is detected, the program routine returns in a normal fashion to the main loop. The main loop then calls the other program routine. And so forth.
I need to be able to switch to the other program anywhere in the first program, without resetting
All the comments about polling, vs. interrupts, are avoiding stating that the code called by loop() must be non-blocking. If you call delay(1000); anywhere in the code called by loop(), polling won't be responsive. You don't seem to understand that you do not have control over what the program does after the interrupt is processed. The code resuming executing right where it left off.
Forget interrupts. Write non-blocking code, so that every pass through loop(), the Arduino decides what to do on this pass - call function one or call function two - each of which decides if it is time to do something.