/*
 * Prodces a 100 millisecond pulse at intervals decided by potentiometer.
 * VCC must be 3V3 to make output pulse is 3V3.
 * 
 * RedGrittyBrick 2021
 */

#define LED_BUILTIN 1
#define PulsePin 0
#define PotPin A1

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  pinMode(PulsePin, OUTPUT); 
  pinMode(PotPin, INPUT);
}

int PotInterval = 100; // In milliseconds
int PotValue = 0; 
int PriorValue = 0;
unsigned long NextRead = 0; // Time to next read potentiometer

int PulseInterval = 1000; // millseconds rise to rise
const int PulseLength = 50; // milliseconds rise to fall
unsigned long NextPulse = 0; // Time to start next 3V3 pulse

void loop() {

    // Time to read the potentioneter?
  if ( millis() > NextRead ) {
     PotValue = analogRead(PotPin); // 0-1023
     //PulseInterval = 1000 + (PotValue * 3600); // 1 second to ~1 hour
     PulseInterval = 100 + (PotValue * 2); // 0.1 second to ~2 seconds
     NextRead += PotInterval;
     PriorValue = PotValue; 
  }

  // Time to start a pulse?
  if ( millis() > NextPulse ) {
    digitalWrite(PulsePin, HIGH);
  }

  // Time to end a pulse?
  if ( millis() > (NextPulse + PulseLength)) {
    digitalWrite(PulsePin, LOW);
    NextPulse += PulseInterval;
  }
  
}
ATTINY8520PU