For loop decrement issue?

Hi everyone. I'm trying to get some code working that decrements from 500 to 1 inside the delay function after turning on and led. The code works fine when i'm incrementing but when decrementing it doesn't run or it runs very slow. I've never encountered something like this, is there anything i'm missing? Thanks in advanced. The idea is to gradually increase the frequency of the LED's until they're flickering so fast they appear to be on.

'for(delayTime = 500; delayTime >= 1; delayTime--){'
'led1();'
'delay(delayTime/scaler);'
'led2();'
'delay(delayTime/scaler);'
'Serial.println(delayTime);'
'delay(10); '

Show actual code, ALL of it, IN CODE TAGS, instead of whatever that snippet of quoted unformatted nonsense was...

Sorry - kind of new to this


#define nop() asm volatile("nop")
int onTime = 1; 
int scaler = 15;
int pin2;
int delayTime;
volatile int state;
void setup() {
  
Serial.begin(9600);  
DDRB |= B00111101;
PORTB |= B00000010;

}

void loop() {
  
allOn();
delay(1);
}


void allOn()
{

  for(delayTime = 500; delayTime >= 1; delayTime--){
led1();
delay(delayTime/scaler);
led2();
delay(delayTime/scaler);
  
  }
}

void led1()
{
    DDRB &= B00000000;
    DDRB |= B00000101;
    PORTB = B00000011;              
}


void led2()
{
    DDRB &= B00000000;
    DDRB |= B00001100;
    PORTB = B00000110;
  }

  

You debug things with problems by putting serial.Print() in the loop.
For instance:

serial.PRintln('beginning for loop");
  for(delayTime = 500; delayTime >= 1; delayTime--){
serial.PRint(delayTime);

led1();
serialPrint("delayTime/scaler", delayTime/scaler);

delay(delayTime/scaler);
led2();
delay(delayTime/scaler);
  
  }
`
Turn the IDE monitor on and see what values you are generating.

It does count down but EXTREMELY slow. It took this around 30 seconds. Whereas counting forward it goes at the normal speed.

begin for loop
for loop control variable: 500
delayTime/scaler: 500
for loop control variable: 499
delayTime/scaler: 499
for loop control variable: 498
delayTime/scaler: 498
for loop control variable: 497
delayTime/scaler: 497
for loop control variable: 496
delayTime/scaler: 496
for loop control variable: 495
delayTime/scaler: 495

your start delay is 33ms can you even see flickering at this frequency?

this is not the result of the code you posted, POST THE ACTUAL CODE

Maybe in the outer reach of your sight...

Thanks everyone for your response. I was thinking about the right thing, the wrong way :sweat_smile:

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.