After a couple of hours googling and trying and failing it seems like I did something right
I havent tried it with a SSR, only a LED but it works like I want. I have set the Period to 2,5 sec and adjust the duty cycle with a potmeter. The code is probably awful so I hope someone can comment and give me some tips.... ![]()
The values I get from analogread is pretty unstable so to get full output when the pot is turned to max I included a if statement.
#include "TimerOne.h"
int sensorPin = 0;
int sensorValue =Â 0;
//int Cycle =Â 0;
int Val = 0;
int SSRval = 0;
void setup()
{
 pinMode(10, OUTPUT);
 Timer1.initialize(2500000);    // initialize timer1, and set a 2,5 second period
 Timer1.pwm(9, 0);         // setup pwm on pin 9, duty cycle = 0
 Timer1.attachInterrupt(callback); // attaches callback() as a timer overflow interrupt
 Serial.begin(9600);
}
void callback()
{
 digitalWrite(10, digitalRead(10) ^ 1);
Â
}
void loop()
{
 Val = analogRead(sensorPin);
  if (Val >= 1010) {
   SSRval = 1024;
  }
   Â
  else {
   SSRval = Val;
  }
 Â
                   Â
  Timer1.setPwmDuty(9, SSRval);
  Serial.println(SSRval);                   Â
 delay(1000);
Â
}