Understanding TimerOne.h

Hello all,
I’m attempting to use an interrupt for the first time within a project, and I believe I’m going to utilize the TimerOne.h library. If I’m understanding that library correctly, am I only able to program outputs within the ISR on pins 9 and 10?

I was hoping to have 3 outputs triggered individually from the interrupt. Is there a means to do this?

Thank you for such a quick reply! I’m just hoping to pull three pins to ground individually, which will trigger an audio player. So maybe my current plan will work. I’ll give it a shot.

Not sure why interrupts seem so intimidating to me…

Have you tried the TimerOne interrupt example?

I’m not too worried about exact timing, no.

To give you a feel for what I’m doing (I don’t have a schematic drawn out yet, otherwise I’d share) - I have two ultrasonic sensors (HC-SR04s) wired into a nano. The setup is on a coin donation bank at a children’s museum, designed to look like R2D2.

One sensor is watching for a coin drop through a funnel, and when it senses a coin it triggers one audio track and starts an LED strip routine using FastLED.h. I currently have this side of the program working pretty reliably.

The second sensor is watching for people to walk by. When it sees a passerby within a pre-programmed distance window, it will alternate audio sounds between three different tracks, all on different triggers (intended to gain attention). When I add this sensor in with the previous, neither works.

My thinking was that the timing of things was essentially missing the coin drop, so I was going to attempt an interrupt to see if that could solve that problem.

Lot to take in without any diagram, I know.

Do the sounds play in a round robin fashion? That is, in some predetermined sequence. Or, is the selection random? Or, do all play at once?

Yeah, I definitely checked that out. I also found Rachel De Barros' YouTube video "How to Use Arduino Interrupts the Easy Way" very helpful, but that was where my confusion on the usage of pins 9 and 10 came from.

Yeah, that was my initial attempt. I found that it was very unreliable due to the large diameter of the funnel outlet (~3in) compared to the size of a coin. That seemed to miss the coin regularly. I ended up with the ultrasonic because I found it had a wider view range that supported the coin drop area. Very well might be overkill, but hey, it works.

That was my thought too! Which is why I was a little weirded out when it didn't work quite like I had envisioned. Perhaps there was a different issue within my code that I overlooked?

My plan is to use trigger 1 only when a coin is donated, then alternate via a counter between triggers 2, 3, and 4. The code I'm pasting below does not have that functionality built in yet; I've only been trying to get it to play trigger 2 reliably when someone walks past.

If you're curious, here's the code I've got running. I'd certainly be open to any critique.

/*********************************************************************************************
** Declare required libraries to include                                                    **
*********************************************************************************************/
#include <FastLED.h>                                  // Include FastLED libray             //
/*********************************************************************************************
** Declare all program constants                                                            **
*********************************************************************************************/
#define ledBrightness  255                            // Set LED brightness from 0 - 255    //
#define typeLED        WS2812B                        // Define LED chip type               //
#define colorOrder     BRG                            // Define LED color order             //

#define ledsHead       5                              // Set Arduino pin for RGB LED output //
#define numHeadLEDs    55                             // Define total number of LEDs        //
CRGB headLEDs[numHeadLEDs];                           // Create LED Object                  //

const int coinDropTrigger = 2,                        // Trigger pin input - coin drop      //
          coinDropEcho = 3,                           // Echo pin input - coin drop         //
          qwTrigger1 = 4;                             // QuikWave Trigger 1                 //
          
const int passerbyTrigger = 6,                        // Trigger pin input - walk past      //
          passerbyEcho = 7,                           // Echo pin input - walk past         //
          qwTrigger2 = 8,                             // QuikWave Trigger 2                 //
          qwTrigger3 = 9,                             // QuikWave Trigger 3                 //
          qwTrigger4 = 10;                            // QuikWave Trigger 4                 //

uint8_t   thisFade = 8,                               // Fade speed - Lower = Slower        //
          thisInc = 1,                                // Incremental value for rotating hues//
          thisSat = 255;                              // LED saturation from 0 - 255        //
int       thisHue = 64,
          huediff = 160;
/*********************************************************************************************
** Method Setup(). This is an Arduino IDE method which is called upon boot or restart.      **
** It is only called one time and then control goes to the main loop, which loops           **
** indefinately.                                                                            **
*********************************************************************************************/
void setup() {                                        // Arduino standard setup method      //
  FastLED.addLeds<typeLED, ledsHead,                  // Define LED properties - Head LEDs  //
    colorOrder>(headLEDs, numHeadLEDs).setCorrection(TypicalLEDStrip);
  FastLED.setBrightness(ledBrightness);               // Set brightness of LEDs             //

  pinMode(coinDropTrigger, OUTPUT);                   // Sets coinDropTrigger as an output  //
  pinMode(coinDropEcho, INPUT);                       // Sets coinDropEcho as an input      //
  pinMode(qwTrigger1, OUTPUT);                        // Sets qwTrigger1 as an output       //
  digitalWrite(qwTrigger1, HIGH);                     // Set and hold qwTrigger1 to HIGH    //

  pinMode(passerbyTrigger, OUTPUT);                   // Sets passerbyTrigger as an output  //
  pinMode(passerbyEcho, INPUT);                       // Sets passerbyEcho as an input      //
  pinMode(qwTrigger2, OUTPUT);                        // Sets qwTrigger2 as an output       //
  pinMode(qwTrigger3, OUTPUT);                        // Sets qwTrigger3 as an output       //
  pinMode(qwTrigger4, OUTPUT);                        // Sets qwTrigger4 as an output       //
  digitalWrite(qwTrigger2, HIGH);                     // Set and hold qwTrigger2 to HIGH    //
  digitalWrite(qwTrigger3, HIGH);                     // Set and hold qwTrigger3 to HIGH    //
  digitalWrite(qwTrigger4, HIGH);                     // Set and hold qwTrigger4 to HIGH    //

  clearLEDs();                                        // Sub-Routine to turn off all LEDs   //

  Serial.begin(9600);                                 // Initialize serial communications   //
} // of method setup()                                //------------------------------------//
/*********************************************************************************************
** This is the main program for the Arduino IDE, it is an infinite loop and keeps on        **
** repeating.                                                                               **
*********************************************************************************************/
void loop() {                                         // Arduino standard loop method       //
  long coinDuration,
       passerbyDuration;
  int coinDropOpenBeam = 194,
      passerbyOpenBeam = 3000;
    
  digitalWrite(coinDropTrigger, LOW);                 // Clear coinDropTrigger              //
  delayMicroseconds(2);

  digitalWrite(coinDropTrigger, HIGH);                // Set coinDropTrigger HIGH for 10mSec//
  delayMicroseconds(10);
  digitalWrite(coinDropTrigger, LOW);

  coinDuration = pulseIn(coinDropEcho, HIGH)          // Return sound wave travel time      //
    - coinDropOpenBeam;
//  passerbyDuration = pulseIn(passerbyEcho, HIGH)      // Return sound wave travel time      //
//    - passerbyOpenBeam;

  if (coinDuration >= 10) {
    digitalWrite(qwTrigger1, LOW);                    // Set qwTrigger1 to LOW, play audio  //
    delay(100);                                       // Short delay to trigger track 1     //
    digitalWrite(qwTrigger1, HIGH);                   // Reset qwTrigger1 to HIGH and hold  //
    for (int n = 0; n <= 900; n++) {                  // FOR loop to iterate LED pattern    //
      fadeToBlackBy(headLEDs, numHeadLEDs, thisFade); // Low values = slower fade.
      int pos = random16(numHeadLEDs);                // Pick an LED at random              //
      headLEDs[pos] += CHSV((thisHue +                // I use 12 bits for hue so that the hue increment isn't too quick.
          random16(huediff))/3 , thisSat,
          ledBrightness);
      thisHue = thisHue + thisInc;                    // Increment hue value
      if (thisHue >= 192) {
        thisHue = 128;
      }                                               // of reset thisHue IF statement      //
    Serial.println(n);                                // Show FOR loop iteration, debug use //
    FastLED.show();                                   // Update LED strip                   //
    }                                                 // of LED pattern FOR loop            //
  } else {                                            // of coinDuration IF statement       //
    // No coin donated
    clearLEDs();                                      // Call clearLEDs() sub-routine       //
    
    if (passerbyDuration >= 0 && passerbyDuration < 6000) {
      digitalWrite(qwTrigger2, LOW);
      delay(100);
      digitalWrite(qwTrigger2, HIGH);
    }
  }                                                   // of ELSE statement                  //

//  Serial.print("coinDuration = ");
//  Serial.print(coinDuration);
//  Serial.print("  passerbyDuration = ");
//  Serial.println(passerbyDuration);
} // of method loop()                                 //------------------------------------//
/*********************************************************************************************
** clearLEDs() sub-routine                                                                  **
** Clear all LED strips to a known state                                                    **
*********************************************************************************************/
void clearLEDs(){
  FastLED.clear();
  FastLED.show();
} // of clearLEDs() sub-routine                       //------------------------------------//