Sunday, April 23, 2023

Twitchboard v1.7 Code and Schematics

 


** updated 10-5-23** 

Removed (by adding //)  the option for continuous on. I've burned out too many (2) door lock actuators by having the interval potentiometer go to zero. Now it will just fire same duration, but more frequently.





//  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 = 1000; // 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 controls="" duration="150;" fet="" for="" if="" int="" middle="" of="" pot="" pwm="map(PWMpot,0,1000,0,255);" pwmpot="analogRead(potpin);" read="" signal="" uncomment="">255) PWM = 255;
  
  // uncomment if pot controls frequency
  // mean frequency of random signals is 1 events per time unit
  int freqpot = analogRead(potpin);
  timeunit = map(freqpot,512,1023,intervalmid,intervalhigh); // 2nd half dial range
  if (freqpot>1000) timeunit = intervalmax; // far right dial 1hr
  if (freqpot<512 1st="" adjust="" and="" bottom="" check="" continous="" debounce="" dial="" for="" freqpot="" half="" if="" is="" of="" on="" positive.="" range="" timeunit="" trigger="">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 thisduration = (0.5*duration) +random(duration);  
    delay(thisduration);
    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