Sunday, April 23, 2023

Twitchboard v1.7 Code and Schematics

 







//  Jekyll-Labs Twitchy Attiny85
//  Built for Twitchboard v1.7
//

// Pin definitions
int fetpin = 0;
int trigpin = 3;
int potpin = A2;

// global variables
long duration = 200; // duration in miliseconds (200)
long PWM = 255; // Duty cycle 0-255 (255)
long intervallow = 2000; // low end of interval range in sec [2 sec]
long intervalmid = 10000; // middle of interval range in sec [10sec]
long intervalhigh = 90000; // high end of interval range in sec [90sec]
long intervalmax = 36000000; // interval in sec if pot maxed out [10hrs]

// initialize global variables
long timeunit = 0;
unsigned long last = 0;
unsigned long timeinterval = 0;
float exprob=0.50;

void setup() {
  randomSeed(A1);
  pinMode(fetpin, OUTPUT);
  digitalWrite(fetpin, LOW);
  pinMode(trigpin, INPUT);
  pinMode(potpin, INPUT);
}

void loop() {
// uncomment if pot controls duration  
// duration of signal will last from 0.5 - 10 sec 
//int durationpot = analogRead(potpin);
//long duration = map(durationpot,100,1023,500,10000); 
//if (duration <500) duration = 150;

// uncomment if pot controls PWM
// read middle pot for PWM of fet signal
//int PWMpot = analogRead(potpin);
//int PWM = map(PWMpot,0,1000,0,255);
//if (PWM>255) PWM = 255;

// uncomment if pot controls frequency
// mean frequency of random signals is 1 events per time unit
int freqpot = analogRead(potpin);
long timeunit = map(freqpot,512,1023,intervalmid,intervalhigh); // 2nd half dial range
if (freqpot>1000) timeunit = intervalmax; // far right dial 1hr
if (freqpot<512) timeunit = map(freqpot,0,512,intervallow,intervalmid); // 1st half dial range
if (freqpot<100) timeunit = 0; // bottom of dial continous on  

if (timeunit<=0){ // continuous on if left dial all counter clockwise
  analogWrite(fetpin,PWM);
  }
  
  else {
    // Check for trigger (X3 for debounce) and adjust
    // timeunit if trigger is positive.
    if (timeunit>0) {
      int trigger = digitalRead(trigpin);  
      if (trigger == HIGH) {
        delay(100);
        int trigger = digitalRead(trigpin);
        if (trigger == HIGH) {
          delay (100);
          int trigger = digitalRead(trigpin);
          if (trigger == HIGH) {
            timeunit = (duration * 2);
          }
        }
      }
    }
    
    // Calcuate time interval between events. timeunit depends on:
    // Potentiometer value if trig = 0 
    // Duration multiplier (shorter) if trig = 1
    unsigned long timeinterval = exprob * timeunit;    
    
    // Check if time interval has passed
    if ((millis() - last) > timeinterval) {
      last = millis(); // updates timer to last event
      analogWrite(fetpin,PWM);
      long duration = random(2000)+3000;  
      delay(duration);
      digitalWrite(fetpin,LOW);
      // the following equation runs once per event and 
      // generates a random float (exprob) that falls in an 
      // exponential distribution. It will be used to
      // determine the interval time for the next event.
      // Events that occur at exponentially distributed  
      // intervals form poisson processes.
      exprob =  (-2)*(log((100-random(100))/100.00));
    }
    else digitalWrite(fetpin,LOW);
  }
  delay(20);
}

 

No comments:

Post a Comment