Stuck at programming a simple device

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);
}

Noted your cross posting in the Nederlands forum. :grin:

OK, the first problem. Here goes:

That is not what interrupts are for!

As a beginner, it is incredibly unlikely that interrupts will be useful to you.

A common “newbie” misunderstanding as you demonstrate here, is that an interrupt is a mechanism for altering the flow of a program - to execute an alternate function. Nothing could be further from the truth! :astonished:

An interrupt is a mechanism for performing an action which can be executed in “no time at all” with an urgency that it must be performed immediately or else data - information - will be lost or some harm will occur. It then returns to the main task without disturbing that task in any way though the main task may well check at the appropriate point for a “flag” set by the interrupt.

Now these criteria are in a microprocessor time scale - microseconds. This must not be confused with a human time scale of tens or hundreds of milliseconds or indeed, a couple of seconds. A switch operation is in this latter category and a mechanical operation perhaps several milliseconds; the period of a 6000 RPM shaft rotation is ten milliseconds.

Unless it is a very complex procedure, you would expect the loop() to cycle many times per millisecond. If it does not, there is most likely an error in code planning; while the delay() function is provided for testing purposes, its action goes strictly against effective programming methods. The loop() will be successively testing a number of contingencies as to whether each requires action, only one of which may be whether a particular timing criteria has expired. Unless an action must be executed in the order of microseconds, it will be handled in the loop().

So what sort of actions do require such immediate attention? Well, generally those which result from the computer hardware itself, such as high speed transfer of data in UARTs(, USARTs) or disk controllers.

An alternate use of interrupts, for context switching in RTOSs, is rarely relevant to this category of microprocessors as it is more efficient to write cooperative code as described above.

TLDR: Do not use interrupts to read push-buttons!

Second problem: “delay()” is not used in actual “real world” programs.

Some resources to learn about using millis() (and micros()) for timing. Credit to groundFungusfor this list.

Beginner’s guide to millis()
The several things at a time tutorial.
Blink without delay.

You need to study those references a little to understand how to program. Sorry, it isn’t dead easy but unless you understand it, you will not be in a position to use any code we provide.

Thirdly, your repetitious code could be condensed using a “for” loop but as it is not functional anyway, we can let that pass.

Finally, posting code here in the first place. You need to go and read the forum instructions so that you can go back and modify your original posts (not re-post them) - using the “More → Modify” option below the right hand corner of your post - to mark up your code as such using the “</>” icon in the posting window. Just highlight each section of code (or output if you need to post that) from the IDE and click the icon.

In fact, the IDE has a “copy for forum” link to put these markings on a highlighted block for you so you then just paste it here in a posting window. But even before doing that, don’t forget to use the “Auto-Format” (Ctrl-T) option first to make it easy to read. If you do not post it as “code” it can easily be quite garbled and is always more difficult to read due to the font.

It is inappropriate to attach it as a “.ino” file unless it is clearly too long to include in the post proper. People can usually see the mistakes directly and do not want to have to actually load it in their own IDE. And even that would also assume they are using a PC and have the IDE running on that PC.

Also tidy up your blank space. Do use blank lines, but only single blanks between complete functional blocks.