speed up void loop?

Hi guys, Im trying to run a gear position indicator and a shift light both from a single Arduino nano, I'm having some problems making it to run fast enough.

A brief overview of the functions of this project is as follows;

The shift light takes a 5v square wave as input, the frequency is measured, and a neopixel strip is illuminated/flashed accordingly.

The gear position indicator takes an analog signal from 2 potentiometers to measure the gear lever position, then 2 bytes of information are sent to a 16 bit shift register (UAA2022) to control a 16 segment LED display.

There is also brightness control, there is a voltage divider with a photoresistor that provides an anolog signal that is then mapped to an output value (0-255), and updated every 2 seconds (using milli, not delay).
It is then used to set pixel brightness for the neopixels, and sent as a PWM output to the shift register CO pin.

When the Arduino recognises the engine has started a startup sequence runs once, the leds light up and flash and turn off again.

Both the shift light and gear indicator work well on their own, but once i combine everything to run on the single neo it is increadibly slow.......

I am hoping someone could help me go through the code and streamline it in order to make the loop run faster. Im still a beginner (This is actually my first project) and im sure there are alot of mistakes in there that are costing me time in the loop. I could use some mentoring...

Another problem I am experiencing is the UAA2022 shift register/16 segment driver isnt responding properly to shift out. For some reason the first bit isnt picked up so an extra bit has to be added manually at the end. If anyone could explain what is going on there i would also be very greatful :slight_smile:

v1.4.ino (9.04 KB)

Hands on the ouija board everyone

For help, please read and follow the instructions in the "How to use this forum" post.

Perhaps it is something to do with all those delays

The delays are used to make the neopixels flash, they should only occur when the tacho input is over a certain frequency. Under the specified frequence the delays shouldnt affect the loop, or is that a misunderstanding on my part?

  if(millis()  > time_now + period){

Unusual.

It would be helpful to use the IDE's auto-format tool on your code

pulseIn doesn't return a float.

How long does a pixel.show() take?
(Hint: You can get your code to tell you.)

Regarding the following;

if(millis()  > time_now + period){

The purpose here was to update the brightness every 2 seconds.
The reasoning behind it was "pixels.setBrightness" is not reccomended to be used in void loop as it is considered "lossy", it is reccomended to use it once in void setup. I thought if I updated it every 2 seconds rather than every loop cycle i could achieve active brightness control without the losses you'd get from running it normally in loop, and still be able to use the rgb color values listed in the array.

As I said earlier, I'm a beginner and im not sure if what I'm doing here is correct, i can only explain my reasoning and hope for constructive criticism.

millis() returns the time now relative to the last reset.

It doesn't make sense to ask if the time now is greater than the time now plus some period.
This is simple semantics.

Take another look at the "blink without delay" example in the IDE, and note the lack of the addition operator.

Micky,
You will get better, more helpful and more friendly help if you follow the forum instructions as you have already been asked in reply #2. Post ALL your code in the forum in code tags please, not as an attachment.

If you have delays in your code then that's the core of your problem, they need to go. Read:
Using millis for timing
Demonstration for several things at the same time

Thank you.

I got an error messaage that it was over 9000 caracters, so I couldnt post the complete code in a post, otherwise I would have done that.

The code for the millis was taken from here, It works fine for updating the brightness every 2 seconds, and even with it removed the loop is still slow as hell.

The code for the millis was taken from here,

The semantics are still stupid.

Just to clarify, if delay is used inside a function, like this:

if (igfreq >= shiftFrequency) {
    //overrev flash
    //default color=red
    pixels.fill(pixels.Color(120, 0, 0));
    pixels.show();
    delay(20);
    pixels.fill(pixels.Color(0, 0, 0));
    pixels.show();
    delay(20);

and igfreq is smaller than shiftfrequency, will the delays still affect the loop?

Just to clarify, have you tried measuring your loop time?

If you're worried about the timing printing code affecting your loop length, average the time, and only print it every two seconds.

I don’t really understand what you mean by that...
Here’s what’s going on, if I run the code for the gear indicator by itself, when I change gear the display update instantly. If I run the complete code including the shift light Code, when I change gear it takes about 3 seconds for the display to update.
I don’t think the delays are the problem, because the delays only occur during shift flash and overrev flash, at which point the delays aren’t a problem. I may be wrong, I did ask but I didn’t get an answer....
The 2 second refresh for brightness control, however stupid it may be written, also does not seem to be the problem as the loop remains slow with this part removed from the code.

I have a feeling it’s just a combination of time consuming processes such as digitalwrite, shiftout, the neopixel library and so on. There are a lot of clock cycles getting used up, and I need streamline it somewhat.

It’s easy to just take a glance at the code and say it’s stupid, but it would be nice if you or someone else took a closer look and could tell me where the best place is to start trimming fat.

TheMemberFormerlyKnownAsAWOL:
The semantics are still stupid.

And it’s borked for integer wrap around at the 49 day mark because it uses addition instead of subtraction...

if(millis() - last_millis  >= period) {

I don't think the delays are the problem

Fix the delays, as in, get rid of them. If the problem persists after that then look elsewhere for the problem. There's no point debating it with me, delays are a problem, that's my advice. You don't HAVE to take my advice of course, but I won't consider trying to help any more until the delays are gone.

Currently working on it. I’m not trying to debate with you, but maybe if you‘d answer my questions (post #11 for example) I’d better understand your reasoning. I literally started coding like a month ago but you guys are acting like I’m supposed to perfectly understand everything you’re saying. Some small explanations, so I can actually LEARN and UNDERSTAND would be super cool.

I will let you know if the problem persists without delays.

mickymik:
Just to clarify, if delay is used inside a function, like this:

if (igfreq >= shiftFrequency) {

//overrev flash
    //default color=red
    pixels.fill(pixels.Color(120, 0, 0));
    pixels.show();
    delay(20);
    pixels.fill(pixels.Color(0, 0, 0));
    pixels.show();
    delay(20);



and igfreq is smaller than shiftfrequency, will the delays still affect the loop?

No, the delays in that section will not affect loop().

you can't expect people to download your files and create a project on there PC's just to see your code to answer your question. you should post your code as text in tags. there is a button for the above where you type you question. also, if your project is running then specify the lines that you think could be more proficient. getting fast help online is all about writing questions in the right way. always make things easy to answer as possible. and you will get quicker answers

@taterking, did you check the filesize?