Hi,
I would be very grateful for some guidance. I have two LEDs that flash red - wait period - green - wait period - red etc., with the 'wait period' determined by a 'faster button' to shorten the wait period and a 'slower button' to lengthen the wait period. These are on hardware interrupts. These flashes are a visual cue to help someone step, and with the buttons I can speed up or slow down their cadence.
I would like to buzz at the same time as flash using a vibration motor to provide an additional tactile cue. I am driving my motor using an Adafruit 2605 haptic breakout board and I2C.
The code below is the 'flashing only' version that works fine, with the addition of the 2605 libraries and my buzz(). This is not called from anywhere at present.
The nub of the problem is that MSTimer2 cannot accept a third argument, i.e. it can accept (Interval, flash) but not (Interval, flash, buzz).
A further problem is that using buzz() would introduce a delay into an ISR. I have not had success calling buzz() from flash(). I would be very grateful for some project help.
Kind regards,
Al.
// MsTimer2-1.1.0 - Version: Latest
#include <MsTimer2.h>
#include <Wire.h>
#include "Adafruit_DRV2605.h"
Adafruit_DRV2605 drv;
const byte Red = 5;
const byte Green = 6;
const byte fastBtn = 2;
const byte slowBtn = 3;
const int Quickest = 200;
const int Slowest = 3000;
volatile int Interval = 1000;
volatile int IntervalStep = 200;
void buzz() {
uint8_t effect = 1;
drv.setWaveform(0, effect);
drv.setWaveform(1, 0);
drv.go();
delay(50);
}
void flash() {
static boolean output = HIGH;
digitalWrite(Red, output);
digitalWrite(Green, !output);
output = !output;
}
void setup() {
pinMode(Red, OUTPUT);
pinMode(Green, OUTPUT);
pinMode(fastBtn, INPUT_PULLUP);
pinMode(slowBtn, INPUT_PULLUP);
attachInterrupt(0, fastBtn_ISR, CHANGE);
attachInterrupt(1, slowBtn_ISR, CHANGE);
MsTimer2::set(Interval, flash);
MsTimer2::start();
drv.begin();
drv.selectLibrary(1);
drv.setMode(DRV2605_MODE_INTTRIG);
}
void loop() {
}
void fastBtn_ISR() {
if (digitalRead(fastBtn) == HIGH)
{
if (Interval + IntervalStep <= Slowest)
{
Interval = Interval + IntervalStep;
MsTimer2::set(Interval, flash);
MsTimer2::start();
}
}
}
void slowBtn_ISR() {
if (digitalRead(slowBtn) == HIGH)
{
if (Interval - IntervalStep >= Quickest)
{
Interval = Interval - IntervalStep;
MsTimer2::set(Interval, flash);
MsTimer2::start();
}
}
}