/*
* DC Cooling Fan (12V) Speed Controller v1
* PWM speed Control with Digispark Attiny85 Development Board
* Author: T.K.Hareendran/2019
* Publisher: www.codrey.com
*/
// which analog pin to connect
#define THERMISTORPIN A1
int potPin = 1; // Potentiometer Input to Pin P2 (See, ADC1 is connected to digital pin 2)
int potValue = 0;
int pulse = 1; // Pulse Output from Pin P1 (See, there's an onboard LED connected to digital pin 1)
/* Hardware PWM Frequency @ P1 = 504Hz :: Digispark @ 16.5MHz */
long x = analogRead(THERMISTORPIN); /* for some randomized value at startup... */
long w = 0;
long s = 0xb5ad; /* pour valeur randomize */
long PWM_duration = 0;
long PWM_value = 0;
long min_PWM = 300; /* between 0 and 1000 ex 200 */
long max_PWM = 1000; /* between 0 and 1000 ex 600 */
long min_duration = 100; /* between 0 and 1500 */
long max_duration = 800; /* between 0 and 1500 */
void setup() {
pinMode(pulse, OUTPUT);
}
long random () {
x = x * x;
w = w + s;
x = x + w;
x = x / 8;
x = x & 0xff;
return (x);
}
void loop() {
/* pour PWM via potentiometre
potValue = analogRead(potPin);
analogWrite(pulse, potValue/4);
delay(10);
*/
/* keep PWM_value between min_PWM and 1000 for (min_PWM/10)% to 100% */
PWM_value = min_PWM + (random() * (max_PWM - min_PWM) / 256);
/* keep PWM_duration between 300 and 1500 ms */
PWM_duration = min_duration + (random() * (max_duration - min_duration) / 256);
/* loop for as much millis-seonds as PWM_duration */
for (int i_boucle = 0 ; i_boucle < PWM_duration ; i_boucle ++) {
digitalWrite(pulse, HIGH);
delayMicroseconds(PWM_value);
digitalWrite(pulse, LOW);
delayMicroseconds(1000 - PWM_value);
}
}