delay help needed

EDIT: As posted, I no longer need help. Thank you for those how tried to help.

I know, another delay question… :roll_eyes:

I have been trying for days to remove the delay and replace it with millis. I have looked through many tutorials and I just can’t figure out where to make the changes. I have the following, but it doesn’t scan correctly.

This is a Larson scanner code on a Mega, LEDs, Resistors…

PLEASE HELP!!! Thank you!!!

int scanPinArray[] = {36,37,38,39,40,41,42,43,44,45};
int scanLen = 9 ; // sizeof(scanPinArray) / sizeof(int);
int scanDir = 1;


void setup()
{ 
for (int scanPin = 0; scanPin < scanLen; scanPin++)pinMode(scanPinArray[scanPin], OUTPUT);
} 

void pulse_width_modulation(int pos)
{
 if(scanDir==1)
 { 
   scanDir = 0;
   for (int scanLed=scanLen; scanLed>0; --scanLed)fadeIn(scanLed);
 }
 else{ 
   scanDir = 1; 
   for (int scanLed=0; scanLed<scanLen; ++scanLed)fadeIn(scanLed);
 } 
} 

void fadeIn(int pos)
{ 
 for(int bri = 150; bri < 256; ++bri)
 { if (millis() > scanTimerMillis1){
    if(scanDir==1 && pos>0)analogWrite(scanPinArray[pos-1], 255-bri);
    if(scanDir==0 && pos<scanLen)analogWrite(scanPinArray[pos+1], 255-bri);   
    analogWrite(scanPinArray[pos], bri);
    scanTimerMillis1 = millis() + 700; }
 //   delayMicroseconds(700); 
   
 }  
}

void loop() {
 static uint8_t pos=0;
 while(pos<scanLen)
 {
     pulse_width_modulation(pos);
     ++pos;
 }
 while(pos>0) 
 {
     --pos;
     pulse_width_modulation(pos);
 }
}

Several things wrong with this:- 1) NO CODE TAGS !!! - read the "how to use this forum sticky post". 2) You have not said what the hardware is nor have you posted a schematic - read the "how to use this forum sticky post". 3) You do not say what this code is supposed to do and what it does do - read the "how to use this forum sticky post".

So if you want help - read the "how to use this forum sticky post".

This looks suspicious

     scanTimerMillis1 = millis() + 700; }
  //   delayMicroseconds(700);

millis is milliseconds, whereas you’ve got it right next to a commented out delayMicroseconds()… milliseconds and microseconds aren’t the same.

The main problem though is that you need to break open fadeIn(). Instead of having a loop in it, your new fade needs to just check against the time, and do one iteration of the fading only. This means you’ll need to record what step of the fade process you’re on. So the code will be running loop, and every time loop runs, it’ll check the time; if and only if the time is right, then it will look at what step of fade process it’s at, and then do the next step

ArduinoCC_Guy:
Troll much?

Pro tip: Don’t come into a forum, ignore the rules, and then accuse one of the most active and helpful members of the forum (you know, someone with 10000 times as many posts as you - literally) of trolling when they point out that you disregarded the rules. Wtf man?

Hey, thanks for the reply. I commented the delayMicroseconds out form the original sketch. I will try to break the fadeIn apart.

Take care.

@ArduinoC_Guy: if you're wondering why you can't post, it's because of your two abusive posts.

Grumpy Mike asked you to read and act on a few very simple guidelines which are there to help you get answers and ensure your code is legible to everyone, and waste the least amount of time.

Your timeout expires in 48 hours - maybe you can use the time to figure out your problem yourself.

First, I apologize to Moderators for having to deal with the report of my post. I didn't realize saying someone is trolling is abusive. I would think that referencing the F bomb would be worse.

I also apologize to the Moderators for not posting my question correctly, but I did correct my mistake.

Had Mike not been a smart aleck and repeat - read the "how to use this forum sticky post" 4 times, he would have received a better response from me. One time would have been more than enough.

I hope everyone can learn from this, put this behind them, and move on.

Take care,

I was going to try and demonstrate, but not for this clown.

GoForSmoke: I was going to try and demonstrate, but not for this clown.

wow... So much for moving on. Good for me that I don't need you to.

wow... So much for moving on.

So you want to move on but you keep on with the abuse!

Had Mike not been a smart aleck and repeat.......

I don't need help getting rid of delays. I have working techniques for that.

Good luck with the compiler. It will tell you that you are wrong over and over again. And you can argue back.