/*
  Arduino | off-topic
  KLevi — 12/9/24 at 10:27 AM
  Interrupt flasher

  The interrupt pin is floating with an extra wire attached to pick up the 50Hz mains noise. Triggering on both falling and rising edge gives us a 10ms timestep
  ⁠coding-help⁠Timing LED challenge

  robherc KV5ROB — Today at 11:15 AM
  cute....that was my 2nd thought, but I wasn't awake enough to catch the 10ms steps along with remembering that you're in a 50Hz country
  [11:16 AM]
  (we're on 60Hz here)

  @robherc KV5ROB
  cute....that was my 2nd thought, but I wasn't awake enough to catch the 10ms steps along with remembering that you're in a 50Hz country

  KLevi — Today at 12:32 PM
  Yeah, I think its a unique approach. Now I want to rewrite one of the sketches so that all of the variables (Pin numbers, delays can be defined in a single line)

  @KLevi
  Yeah, I think its a unique approach. Now I want to rewrite one of the sketches so that all of the variables (Pin numbers, delays can be defined in a single line)

  robherc KV5ROB — Today at 12:33 PM
  use a uint64_t & bitfield them all in

  KLevi — Today at 12:35 PM
  Dang that would be hard
  void LEDs(uint8_t counter, uint8_t ledpin) { I was thinking of something like this but for all of the leds included (or not?) (edited)
  [12:37 PM]
  But also that it counts the number of variables given (and declares more leds as needed in a for loop?)

  @KLevi
  But also that it counts the number of variables given (and declares more leds as needed in a for loop?)

  robherc KV5ROB — Today at 1:01 PM
  being a C programmer, I'd make it accept 2 arguments:
  `void LEDs(uint8_t count, LED* leds)*
  [1:02 PM]
  hint, the array MIGHT not be a "standard datatype"

  @robherc KV5ROB
  hint, the array MIGHT not be a "standard datatype"

  KLevi — Today at 1:03 PM
  LED* leds? Is that the array that you are talking about?

  robherc KV5ROB — Today at 1:07 PM
  yep
  [1:09 PM]
  typedef struct led{
  uint8_t pin;
  uint16_t count;
  } LED;

  KLevi — Today at 1:26 PM
  Can you please explain how this work? Or shall I just google it when I get to my PC?

  @KLevi
  Can you please explain how this work? Or shall I just google it when I get to my PC?

  robherc KV5ROB — Today at 1:53 PM
  you make that struct ... then you make an array of type "LED" (that struct)

  1
  [1:54 PM]
  ...you load your pin #s and count #s into the array of LED structs; then when you pass count (how many structs to read) and the array to your function, it can read through all of them in a loop.
  [1:55 PM]
  ...You could even add a "last count" item to the struct, to store your state variables in the struct array too.

  1

  nis — Today at 2:03 PM
  ;) (edited)

*/

//Pins
#define RED 9
#define GREEN 8
#define BLUE 7

//Frequency of leds
#define rSet 16
#define gSet 750  //on-off time with 50% duty cycle
#define bSet 66   //45bpm = 60000 / 45 => 1333 => /2 => /10ms

bool rState, bState, gState = 0;

uint8_t rCounter, bCounter;
uint16_t gCounter;

ISR(PCINT1_vect) {

  rCounter++;
  gCounter++;
  bCounter++;

  LEDs();
}


void setup() {
  // put your setup code here, to run once:

  PCMSK1 |= bit(PCINT8);                   // A0
  PCIFR |= bit(PCIF2);                     // clear interrupts
  PCICR |= bit(digitalPinToPCICRbit(A0));  // enable pin change interrupts for Analog pins

  pinMode(RED, OUTPUT);
  pinMode(GREEN, OUTPUT);
  pinMode(BLUE, OUTPUT);
}

void loop() {
  // put your main code here, to run repeatedly:
}

void LEDs() {
  if (rCounter > rSet) {
    rState = !rState;
    digitalWrite(RED, rState);
    rCounter = 0;
  }

  if (bCounter > bSet) {
    bState = !bState;
    digitalWrite(BLUE, bState);
    bCounter = 0;
  }

  if (gCounter > gSet) {
    gState = !gState;
    gCounter = 0;
  }

  if (gState) {
    digitalWrite(GREEN, bState);
  } else {
    digitalWrite(GREEN, rState);
  }
}