I have made a 230VAC-circut for LED-dimming. I want to dim the light from 0-100%, and then stay at 100%. When i turn my UNO R3 microcontroller off, i want it to dim from 0-100% again when i turn it on. So, my first step will be to somehow break the for-loop when it counts down to number 2 (full light), and then stay there! I have tried a IF-statment but it wont work. Any suggestions will be highly appriciated!
Here is the code:
int AC_LOAD = 2; // Output to Opto Triac pin
int dimming = 126; // Dimming level (2-126) 2 = ON, 126 = OFF
void setup()
{
Serial.begin(9600);
pinMode(AC_LOAD, OUTPUT);// Set AC Load pin as output
attachInterrupt(1, zero_crosss_int, RISING);
}
void zero_crosss_int()
{
int dimtime = (75*dimming);
delayMicroseconds(dimtime); // Wait till firing the TRIAC
digitalWrite(AC_LOAD, HIGH); // Fire the TRIAC
delayMicroseconds(10); // triac On propogation delay
digitalWrite(AC_LOAD, LOW); // No longer trigger the TRIAC (the next zero crossing will swith it off) TRIAC
}
void loop()
{
for (int i=126; i >= 2; i--){
dimming=i;
Serial.println(dimming);
delay(10);
}
}
The Arduino loop() function is called over and over, all the time.
So this
void loop()
{
for (int i=126; i >= 2; i--){
dimming=i;
Serial.println(dimming);
delay(10);
}
}
will just keep dimming 126 to 2, over and over, all the time.
You have to keep that from happening after it has done once the dimming.
Many ways to do that. Here's a bit of a simple way, see if your finger can trace how it might work:
unsigned char needToDim = 1; // (true) we need to dim
void loop()
{
if (needToDim) {
for (int i=126; i >= 2; i--){
dimming=i;
Serial.println(dimming);
delay(10);
}
needToDim = 0; // (false) all done, so don't anymore
}
}
It's kinda brain dead, as all it does is suppress subsequent processing of the for loop. In a real program, needToDim might serve other purposes at the same time; at a minimum some other program logic might set it back to true for dimming again or for control of un-dimming.
BTW you could just
Serial.println(i);
and eliminate the dimming variable. The for loop index is a perfectly good variable. You could even name it dimValue or whatever.