Hi, everybody,
For study I am working on a prototype of a wrist-worn device. The device has a led that switches between green and red, a vibrating motor and 2 buttons. I'm not very handy with arduino so I hope someone might know a solution. I program a Seeeduino Lotus with Groove components.
What needs to be done:
- LED should stay on green and the vibrate motor should start after a few seconds.
- (Button 1) Then you can choose to 'snooze' the vibrating and the led stays green, after which it starts again to the beginning stage: a green led and after a while it vibrates again.
- (Button 2) Or you can choose to stop the vibrating and the led will turn red, after that it will also go back to the initial stage.
Now the problem is that an interrupt stops in the middle of my loop stops (in the middle of the vibration) and then continues there again. I would like the whole interrupt to be done and then the program will start again at the beginning of the loop.
Note: I looked at the 'flag' method but that didn't help me out, because I did not understand how to use it.
The code:
#include <ChainableLED.h>
int button1 = 3;
int button2 = 2;
int ledpin1 = 4;
int ledpin2 = 5;
int vibratingmotor = 6;
int numLeds = 1;
ChainableLED leds(ledpin1, ledpin2, numLeds);
void setup()
{
//leds.init();
pinMode(button1,INPUT);
pinMode(button2,INPUT);
pinMode(vibratingmotor,OUTPUT);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
attachInterrupt(digitalPinToInterrupt(3),InterruptFunction1, HIGH);
attachInterrupt(digitalPinToInterrupt(2),InterruptFunction2, HIGH);
}
void loop()
{
leds.setColorRGB(0, 0, 255, 0);
delay (10000);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
delay (250);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
delay (250);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
delay (250);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
delay (250);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
delay (250);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
delay (250);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
delay (250);
digitalWrite(vibratingmotor, HIGH);
delay (500);
digitalWrite(vibratingmotor, LOW);
}
void InterruptFunction1 (){
digitalWrite(vibratingmotor, LOW);
leds.setColorRGB(0, 255, 0, 0);
delay(10000);
}
void InterruptFunction2 (){
digitalWrite(vibratingmotor, LOW);
leds.setColorRGB(0, 0, 255, 0);
delay (10000);
}