tylernt:
Maybe your variable 'dimming' needs to be volatile?
volatile int dimming = 128;
Thanks for that I've tried it and its made no odds
Use serial.print to show what value of dimming is actually being set by your pot and confirm that the values are correct
Done that all ready readings look nice and smooth from 0 to 1023
I wrote some code that does this but it uses 2 timers and requires either an Arduino Leonardo or Arduino Micro, as they use the Atmega32u chip that has an additional hardware timer
Unfortunately my pcb is based around the ATmega328p so if I can get away using this I would prefer to.
One more thought -- analog inputs are susceptible to noise, especially if you are using a high impedance pot (more than 10K). I assume you have the two ends of the pot connected to VCC and ground? What value is the pot?
I am using a 10k pot and yes I have VCC and GND either side of the pot
I have also tried this code which I found on a Google search which I think divides the input from the pot into 8 ( correct me if im wrong)
#include <TimerOne.h> // Avaiable from http://www.arduino.cc/playground/Code/Timer1
volatile int i=0; // Variable to use as a counter
volatile boolean zero_cross=0; // Boolean to store a "switch" to tell us if we have crossed zero
int AC_pin = 5; // Output to Opto Triac
int POT_pin = A3; // Pot for testing the dimming
int LED = 11; // LED for testing
int dim = 0; // Dimming level (0-128) 0 = on, 128 = 0ff
int freqStep = 65; // This is the delay-per-brightness step in microseconds.
// It is calculated based on the frequency of your voltage supply (50Hz or 60Hz)
// and the number of brightness steps you want.
//
// The only tricky part is that the chopper circuit chops the AC wave twice per
// cycle, once on the positive half and once at the negative half. This meeans
// the chopping happens at 120Hz for a 60Hz supply or 100Hz for a 50Hz supply.
// To calculate freqStep you divide the length of one full half-wave of the power
// cycle (in microseconds) by the number of brightness steps.
//
// (1000000 uS / 120 Hz) / 128 brightness steps = 65 uS / brightness step
//
// 1000000 us / 120 Hz = 8333 uS, length of one half-wave.
void setup() { // Begin setup
pinMode(AC_pin, OUTPUT); // Set the Triac pin as output
pinMode(LED, OUTPUT); // Set the LED pin as output
attachInterrupt(0, zero_cross_detect, RISING); // Attach an Interupt to Pin 2 (interupt 0) for Zero Cross Detection
Timer1.initialize(freqStep); // Initialize TimerOne library for the freq we need
Timer1.attachInterrupt(dim_check, freqStep);
// Use the TimerOne Library to attach an interrupt
// to the function we use to check to see if it is
// the right time to fire the triac. This function
// will now run every freqStep in microseconds.
}
void zero_cross_detect() {
zero_cross = true; // set the boolean to true to tell our dimming function that a zero cross has occured
i=0;
digitalWrite(AC_pin, LOW);
}
// Turn on the TRIAC at the appropriate time
void dim_check() {
if(zero_cross == true) {
if(i>=dim) {
digitalWrite(AC_pin, HIGH); // turn on light
i=0; // reset time step counter
zero_cross=false; // reset zero cross detection
}
else {
i++; // increment time step counter
}
}
}
void loop() {
dim = analogRead(POT_pin) / 8; // read dimmer value from potentiometer
analogWrite(LED, dim); // write dimmer value to the LED, for debugging
}
But once again I just get mad strobe effect or just stays on continually
I also tried this code off arduino website which seems to work fine but not using a pot
// AC Control V1.1
//
// This arduino sketch is for use with the heater
// control circuit board which includes a zero
// crossing detect fucntion and an opto-isolated triac.
//
// AC Phase control is accomplished using the internal
// hardware timer1 in the arduino
//
// Timing Sequence
// * timer is set up but disabled
// * zero crossing detected on pin 2
// * timer starts counting from zero
// * comparator set to "delay to on" value
// * counter reaches comparator value
// * comparator ISR turns on triac gate
// * counter set to overflow - pulse width
// * counter reaches overflow
// * overflow ISR truns off triac gate
// * triac stops conducting at next zero cross
// The hardware timer runs at 16MHz. Using a
// divide by 256 on the counter each count is
// 16 microseconds. 1/2 wave of a 60Hz AC signal
// is about 520 counts (8,333 microseconds).
#include <avr/io.h>
#include <avr/interrupt.h>
#define DETECT 2 //zero cross detect
#define GATE 5 //triac gate
#define PULSE 4 //trigger pulse width (counts)
int i=483;
void setup(){
// set up pins
pinMode(DETECT, INPUT); //zero cross detect
digitalWrite(DETECT, HIGH); //enable pull-up resistor
pinMode(GATE, OUTPUT); //triac gate control
// set up Timer1
//(see ATMEGA 328 data sheet pg 134 for more details)
OCR1A = 100; //initialize the comparator
TIMSK1 = 0x03; //enable comparator A and overflow interrupts
TCCR1A = 0x00; //timer control registers set for
TCCR1B = 0x00; //normal operation, timer disabled
// set up zero crossing interrupt
attachInterrupt(0,zeroCrossingInterrupt, RISING);
//IRQ0 is pin 2. Call zeroCrossingInterrupt
//on rising signal
}
//Interrupt Service Routines
void zeroCrossingInterrupt(){ //zero cross detect
TCCR1B=0x04; //start timer with divide by 256 input
TCNT1 = 0; //reset timer - count from zero
}
ISR(TIMER1_COMPA_vect){ //comparator match
digitalWrite(GATE,HIGH); //set triac gate to high
TCNT1 = 65536-PULSE; //trigger pulse width
}
ISR(TIMER1_OVF_vect){ //timer1 overflow
digitalWrite(GATE,LOW); //turn off triac gate
TCCR1B = 0x00; //disable timer stopd unintended triggers
}
void loop(){ // sample code to exercise the circuit
i--;
OCR1A = i; //set the compare register brightness desired.
if (i<65){i=483;}
delay(15);
}