-1

I have a for loop made by my teacher that dims (or brightens) an LED connected to a PIC18F2455. I have to make the code do both, first brighten, then dim it, but my C skills aren't good enough for this. How can I make this happen?

int count=500;
while(1){
    for (int i=0; i<count;i++){
        LED = 0;
        for(int j=0;j<i;j++);
            LED = 1;
        for (int j=0;j<count-i;j++);
    }
}
Mike
  • 4,041
  • 6
  • 20
  • 37
Underi
  • 1
  • 1
  • 1
    There are two ways of "dimming" a LED: Control the voltage to it, or make it blink in different frequencies (the quicker the blink, the brighter it will seem). Which of these methods can you use? – Some programmer dude Nov 28 '18 at 12:54
  • 3rd for loop executes? – shas Nov 28 '18 at 12:59
  • 1
    where is main()? – Mike Nov 28 '18 at 13:15
  • Ah sorry. This code is inside the main function. – Underi Nov 28 '18 at 16:22
  • I'm supposed to use this for loop to dim and brighten the LED. With this setup it only increases the time it's off and decreases the time it's off, until it goes off completely and then starts the loop all over again. – Underi Nov 28 '18 at 16:24

1 Answers1

1

You can use PWM to set the brightness or You can set the dim and brighten with this way. The second way is you can use this with software like that.

You can play with the ms variable and you can set the brightness.

#define MAX_DELAY 1000;
int ms=0;

while(1) {
LED=0;
delayMs(ms);
LED1(1)
delayMs(MAX_DELAY-ms);  
}
erenbasturk
  • 412
  • 4
  • 10