HELP ! Unable to control dimming speed of ac bulb with potentiometer or serial i

After examining multiples codes, i created this one (it's a sample ) code for my custom need.

I am trying to dim/ fade incandescent AC bulb with Triac.
It also have zerocrossing detection .

The circuite workes fine ( tested with other codes from instructables or electronoobs ).

MY Problem :

The below code starts normally ( that is with the initial dimmness value ) but when i turn the potentiometer knob, the bulbs glows at full brightness and stays at it.
Potentiometer does nothing then.
I tried with serial input too.

Serial monitor shows correct (expected ) values of dim variable but that value doesn't reflect in the interrupt function..

const int d1 = 3;  // D3 PWM OUT
int dim = 5000;    // Off time in microseconds to keep triac off after zerocross . 1/2 wave takes 10ms or 10000microseconds at 50hz
                   // Using 5000 as preset value
void setup()
{
  pinMode(d1, OUTPUT);
  Serial.begin(9600);
  attachInterrupt(0, zero, RISING);   // INT0 for zerocrozzing (from optocoupler)
}


void loop() {
  int dim = map(analogRead(A0),0,1024,7000,10);     // Map the potentiometer input into readings from 0 to 7000
  Serial.println(dim);
  delay(500);                                       // Update brightness level every half second
}


void zero() {                 // fired after zerocrossing detection
  delayMicroseconds(dim);     // Using 7000 Microseconds turns ac bulbs brightness completely dim (unnoticable) so keeping to 7000 only.
  digitalWrite(d1, HIGH);
  delayMicroseconds(5);       // 5 microseconds time for triac to turn on properly.
  digitalWrite(d1, LOW);
}

I am new to Arduino and this is also my first project.

Edit : i want to controll 3 bulbs independently . (As decorative lighting) however, i can help myself with that i guess.

You have declared the dim variable twice. The global dim variable is not the same as the dim variable declared in the loop() function. Don't declare the dim variable in the loop function, just use it.

Schematic ?

vaj4088:
You have declared the dim variable twice. The global dim variable is not the same as the dim variable declared in the loop() function. Don't declare the dim variable in the loop function, just use it.

Schematic ?

I saw that too but wonder why turning the pot has any effect on the brightness since the OP claims it works "initially."

Technically, you should declare 'dim' as 'volatile' since it is used both in the ISR and in loop().

volatile int dim = 5000;

You should probably declare it as an 'unsigned' int since delayMicroseconds() take an unsigned long int argument.

volatile unsigned dim = 5000;

Delaying more than 2000 microseconds in an ISR will definitely cause you to lose ticks in the millis() timer. As long as you don't use millis() for anything it may not be a problem.

I agree with johnwasser about volatile. unsigned long would be nice but should be handled by code in this case.

The ISR should be quick but you may have little choice in this case.

vaj4088:
Schematic ?

Thanks for answers.

The variable scope must be the cause of the problem.
I dont have any experience in C so.

I was following the post in electronoobs.
So I have this exact same schematic.

johnwasser:
Delaying more than 2000 microseconds in an ISR will definitely cause you to lose ticks in the millis() timer. As long as you don't use millis() for anything it may not be a problem.

If I place 3 functions in the ISR function will they behave asynchronously, can it be possible, because I will be needing to controll 3 triacs independently

vaj4088:
The ISR should be quick but you may have little choice in this case.

The code here uses hardware timers to control timing for generation of the triac's gate pulse. This means the ISRs are nice and short.

As a further improvement, I'd replace the digitalWrite() calls with Direct Port Manipulation.

gfvalvo:
The code here uses hardware timers to control timing for generation of the triac's gate pulse. This means the ISRs are nice and short.

As a further improvement, I'd replace the digitalWrite() calls with Direct Port Manipulation.

Well, thanks.

For the code in arduino, I didnt understand much about the inturrupt codes ( they were low level and needs to be referenced with the datasheet ) . Already said I am a begineer in arduino development.

About the direct port manipulation, those were good . Especially when I will control 3 diacs.
However still running on 16MHz, a few cycles doesnt matter.

johnwasser:
Technically, you should declare 'dim' as 'volatile' since it is used both in the ISR and in loop().

volatile int dim = 5000;

You should probably declare it as an 'unsigned' int since delayMicroseconds() take an unsigned long int argument.

volatile unsigned dim = 5000;

Your fix worked like a charm.

I have updated the code a bit so that to give it breathing effect amd speed is controlled with pot now.

Can anyone help me a little about achieving 3 phase independent ac control with single chip (using arduino nano here)