random delays in program loop?!?!

for some reason in this program i made it will delay when switching from pin 4 to 3 ( right when the program begins a new loop) and i have no idea why!

however there are no error messages caused by the program

int sensor = 0; void setup() { // the delay also happens upon start up right here

//set all used pins to OUTPUT mode pinMode(3,OUTPUT); pinMode(4,OUTPUT); pinMode(5,OUTPUT); pinMode(6,OUTPUT); pinMode(7,OUTPUT); pinMode(8,OUTPUT);

}

void loop() { //read the state of potentiometer int s = analogRead(sensor); //turn on led, wait for the amount of time equal to the potentiometers position, then turn the next led on //while turning off the previous one digitalWrite(3,1); delay(s); digitalWrite(3,0); s = analogRead(sensor);

digitalWrite(4,1); delay(s); digitalWrite(4,0);

s = analogRead(sensor); digitalWrite(5,1); delay(s); digitalWrite(5,0); s = analogRead(sensor);

digitalWrite(6,1); delay(s); digitalWrite(6,0); s = analogRead(sensor);

digitalWrite(7,1); delay(s); digitalWrite(7,0);

// check for a new potentiometer position s = analogRead(sensor);

//then do the same proccese with the leds in reverse digitalWrite(8,1); delay(s); digitalWrite(8,0); s = analogRead(sensor); digitalWrite(7,1); delay(s); digitalWrite(7,0); s = analogRead(sensor); digitalWrite(6,1); delay(s); digitalWrite(6,0); s = analogRead(sensor); digitalWrite(5,1); delay(s); digitalWrite(5,0);

digitalWrite(4,1); delay(s); digitalWrite(4,0);

//it delays right here for some reason!

}

You really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :)

OldSteve: You really should have read the How to use this forum - please read post at the top of the index page and How to use this forum before posting.

ie Your code and any error messages should always be placed between code tags. Posting it inline as you have done makes it much harder to read or copy and paste for diagnosis.

It's still not too late to edit your post and do this. You'll make potential helpers much happier. :)

i would post the error messages i have ... but i dont have any ... it simply is acting as if there is a delay in the start of the loop

There’s always a delay on power-up.

I can’t see any reason in your code why there should be an additional delay at the beginning of ‘loop()’, apart from the very tiny delay while ‘analogRead()’ executes.

And please don’t forget those code tags. :wink:
(I didn’t only say error messages.) Obviously, you still haven’t read the linked posts, and after 8 posts you should already know better.

still cant get it to work ;-;

brenden_nerd_: still cant get it to work ;-;

What, your program or the code tags?

int sensor = 0;
void setup()
{
    // the delay also happens upon start up right here

    //set all used pins to OUTPUT mode
    pinMode(3, OUTPUT);
    pinMode(4, OUTPUT);
    pinMode(5, OUTPUT);
    pinMode(6, OUTPUT);
    pinMode(7, OUTPUT);
    pinMode(8, OUTPUT);

}

void loop()
{
    //read the state of potentiometer
    int s = analogRead(sensor);
    //turn on led, wait for the amount of time equal to the potentiometers position, then turn the next led on //while turning off the previous one
    digitalWrite(3, 1);
    delay(s);
    digitalWrite(3, 0);
    s = analogRead(sensor);

    digitalWrite(4, 1);
    delay(s);
    digitalWrite(4, 0);

    s = analogRead(sensor);
    digitalWrite(5, 1);
    delay(s);
    digitalWrite(5, 0);
    s = analogRead(sensor);

    digitalWrite(6, 1);
    delay(s);
    digitalWrite(6, 0);
    s = analogRead(sensor);

    digitalWrite(7, 1);
    delay(s);
    digitalWrite(7, 0);

    // check for a new potentiometer position
    s = analogRead(sensor);

    //then do the same proccese with the leds in reverse
    digitalWrite(8, 1);
    delay(s);
    digitalWrite(8, 0);
    s = analogRead(sensor);
    digitalWrite(7, 1);
    delay(s);
    digitalWrite(7, 0);
    s = analogRead(sensor);
    digitalWrite(6, 1);
    delay(s);
    digitalWrite(6, 0);
    s = analogRead(sensor);
    digitalWrite(5, 1);
    delay(s);
    digitalWrite(5, 0);

    digitalWrite(4, 1);
    delay(s);
    digitalWrite(4, 0);

    //it delays right here for some reason!
}

Get rid of all those delay(s);

See: https://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

brenden_nerd_: i would post the error messages i have ... but i dont have any ... it simply is acting as if there is a delay in the start of the loop

How long is this delay?

the delay is about a millisecond

At the end of 'loop()', execution passes back to 'main()' before the loop function is called again, and a little bit of code is executed there, but I doubt that it takes a millisecond.

for (;;) {
    loop();
    if (serialEventRun) serialEventRun();
}

Also an 'analogRead()' takes 100uS, and that's the first thing in 'loop()'. How are you detecting the delay? Are you sure it's not a measuring error?

Does this produce the same ‘random’ delay.

#define ENTRY_COUNT(ARRAY)  (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t   pinPOTENTIOMETER    = A0;

const uint8_t   LED_OFF             = LOW;
const uint8_t   LED_ON              = HIGH;


uint8_t pinsLED[] = { 3, 4, 5, 6, 7, 8, 7, 6, 5, 4 };

void loop()
{
    size_t  index = 0;

    while ( true )
    {
        const uint8_t pin = pinsLED[index];

        digitalWrite(pin, LED_ON);

            delay(analogRead(pinPOTENTIOMETER));

        digitalWrite(pin, LED_OFF);

        index++;
        index %= ENTRY_COUNT(pinsLED);
    }
}

void setup()
{
    for ( size_t i = 0; i < ENTRY_COUNT(pinsLED); i++ )
    {
        pinMode(pinsLED[i], OUTPUT);
    }
}

brenden_nerd_: the delay is about a millisecond

I can't reproduce your problem. Running your exact code on my Uno I get this:

|500x375

The relevant issue is how long is there between D4 (digital pin 4) turning off at the end of loop() and D3 turning on at the start of loop(). As you can see, it is 116 µs, and since an analogRead() call takes around 114 µs then the rest can be explained by the time taken to exit from loop() and re-enter it.

Thus, working as expected.

LarryD: Get rid of all those delay(s);

Why???

The sketch isn't trying to do anything else during those delays, and adding the blink without delay logic would unnecessarily complicate the code. In and of itself, delay() is not the root of all evil, it's only a problem when used in the wrong contexts, and I don't think this is one of those cases.

To use the blink without delay techniques in this sketch, it would have to be totally rewritten as a state machine; or the standard if logic used to check millis() to see if the time has expired would need to be written as a while loop, which puts it right back to being a more complicated way of doing the same thing as delay().

lloyddean:
Does this produce the same ‘random’ delay.

#define ENTRY_COUNT(ARRAY)  (sizeof(ARRAY) / sizeof(ARRAY[0]))

const uint8_t  pinPOTENTIOMETER    = A0;

const uint8_t  LED_OFF            = LOW;
const uint8_t  LED_ON              = HIGH;

uint8_t pinsLED = { 3, 4, 5, 6, 7, 8, 7, 6, 5, 4 };

void loop()
{
    size_t  index = 0;

while ( true )
    {
        const uint8_t pin = pinsLED[index];

digitalWrite(pin, LED_ON);

delay(analogRead(pinPOTENTIOMETER));

digitalWrite(pin, LED_OFF);

index++;
        index %= ENTRY_COUNT(pinsLED);
    }
}

void setup()
{
    for ( size_t i = 0; i < ENTRY_COUNT(pinsLED); i++ )
    {
        pinMode(pinsLED[i], OUTPUT);
    }
}

no yours has no 'random delay, thank you every one

Neither does yours, as I demonstrated.

You are making your led move back and forth. I will bet anything that what is hapenning is that you are double-counting the led at the start and end of your loop.

Lets have a look. Hmm - no, that aint it.

The only off thing is serialeventrun(), which the arduino always does between your loops to see if there’s a new sketch coming down the cable. Maybe there’s something odd happening on the USB network your arduino is plugged into. If you unplug the arduino from USB and power it with a power supply, do you still get the delay?

As to your code: can you at least use loops?

  for(int i = 3; i<8;i++) {
    s = analogRead(sensor);
    digitalWrite(i,1);
    delay(s);
    digitalWrite(i,0);
  }
  for(int i = 8; i>3;i--) {
    s = analogRead(sensor);
    digitalWrite(i,1);
    delay(s);
    digitalWrite(i,0);
  }

Or better still, use state:

int nextLed = 3;
int direction = -1; // because to get to 3, we must be going down

loop() {
    int s = analogRead(sensor);
    digitalWrite(nextLed,1);
    delay(s);
    digitalWrite(nextLed,0);
    if(nextLed == 3 || nextLed == 8) {
      direction *= -1;
    }
    nextLed += direction;
   }
}

Its usually better for your loop to execute quickly and exit, so using state like this means that you loop only runs for one flash rather than for the whole sequence.

You haven't addressed the fact that when I tested his code, there was no "millisecond delay". I got 116 µs which is accounted for by the analogRead().

[quote author=Nick Gammon link=msg=2499279 date=1448777294] You haven't addressed the fact that when I tested his code, there was no "millisecond delay". I got 116 µs which is accounted for by the analogRead().[/quote] Yep. Exactly as expected.

I'd still be interested to hear how brenden_nerd_ was measuring, to discover the claimed 1mS delay. I asked yesterday, but he chose not to answer.

random delays in program loop?!?!

So there wasn't a delay, and it wasn't random? Does that sum up the findings?

[quote author=Nick Gammon link=msg=2499347 date=1448785745] So there wasn't a delay, and it wasn't random? Does that sum up the findings?[/quote] If the shoe fits.....