LED SEQUENCE WITHOUT DELAY

Hi! please consider the follwoing code:

int i,k;
int bp1=7;//button
int led0=6,led1=9,led2=10,led3=11,led4=12;
int sensorPin = A0; // input pin potentiometer
int ledpot = 5;
int sensVal = 0; // variable to store the value coming from the sensor

void setup()
{
pinMode(led1,OUTPUT);
pinMode(led2,OUTPUT);
pinMode(led3,OUTPUT);
pinMode(led4,OUTPUT);
pinMode(bp1,INPUT);

}
void loop()
{

if(digitalRead(bp1,HIGH)) digitalWrite(Led0,HIGH);
else digitalWrite(Led0,LOW);

sensVal = analogRead(sensorPin);
digitalWrite(ledpot, HIGH);
delay(sensVal);
digitalWrite(ledpot, LOW);
delay(sensVal);

for(i=1;i<=4;i++)
{
if(k%4==0)
{
if(i==4) i=1;
if(i ==1) digitalWrite(Led1,HIGH); else digitalWrite(Led1,LOW);
if(i ==2) digitalWrite(Led2,HIGH); else digitalWrite(Led2,LOW);
if(i ==3) digitalWrite(Led3,HIGH); else digitalWrite(Led3,LOW);
if(i ==4) digitalWrite(Led4,HIGH); else digitalWrite(Led4,LOW);
}
k++;
}

}

Now i want to excecute the code without the use of the delay function. considering a timing of 250ms, i check by using the modulus function. but the actual result is, only the first LED reacts(stays OFF) and all the other led's on.
PLEASE ADVISE HOW TO PROCEED. I am new to arduino.
THANKS IN ADVANCE!!

EDITS: i have made the following changes: replaced 'j' by 'k'.
additonal code

The value of k never changes. How does it make sense to check k % 4?

Look at the blink without delay example.

Doesn't k++ increase k ?

PaulS,
I have made the change in my mistake earlier.
also i would like to be able to do other functions while the led sequence runs. When i use the "LED sequence without delay", i am not able to run anyother function.
so, the other fucntions are running on different timing and led sequence is running at 1/4th of their timing.

@michinyon:
it does increase k.

When i use the "LED sequence without delay", i am not able to run anyother function.

You'll need to post that code, then. It is quite possible to make LEDs flash AND do a ton of other stuff while the LEDs are flashing.

urm theres an a code that someone had gave to me when i try to learn about array maybe i could share it with you if i can find it
ok found it .....

see reply number 13

thanks ash. a very complicated code for me, i am much obliged. i have never worked with arrays, so i am a lil taken back. i will try the same but i would still like to know a simpler method. Thanks again!!

@PaulS
Please note the code change!

Please note the code change!

I don't think you paid attention during the "blink without delay" lecture. You need to go back and repeat that class.

ok1 thanks Pauls. I will keep this post alive untill i get back to you!

btw i wanted to know do you need the led to blink at different interval or in a sequence?

Ash,
i want the led sequence to work on 1/4th or fraction of the time taken to complete other processes. for exaple if all the other processes are happening at 1/sec, then the led sequence should be running at 250ms.

Thats interesting
im really is a new programmer for arduino a modest one in PLC....
urm ok then well i guess that you will need your code to run atleast once b4 it knows how long the code would execute. Correct me if im wrong guys .....
anyway
somewhere in your program you need to record the time during the initial start up and at the end of the code b4 it loops....
well ok if i am rite about what your saying.
you will run a function and that function will maybe take about a second to run
so you need the led sequence... that is 1on the rest off then 2 on the rest off then 3 on the rest off and 4 on the rest off.... and the whole led sequence happen in like 1/4th of a second
and if the function take about half a second to run you want the led to finish in 1/8th of a second?

maybe this would help you

long DurationOn=millis();
if(millis()-DurationON>=(250/4)
{
  i++;
}

if(i==4) i=1;
  if(i ==1) digitalWrite(Led1,HIGH); else digitalWrite(Led1,LOW);
  if(i ==2) digitalWrite(Led2,HIGH); else digitalWrite(Led2,LOW);
  if(i ==3) digitalWrite(Led3,HIGH); else digitalWrite(Led3,LOW);
  if(i ==4) digitalWrite(Led4,HIGH); else digitalWrite(Led4,LOW);

I hope this helps...

const byte NUM_STEPS = 3;
byte currStep;

// see blink without delay example
unsigned long prevMillis;

// execute a new step every "interval" milliseconds
unsigned long interval = 1000;    // 1 s to avoid flooding the serial terminal with Serial.println() statements


// perform the expected action(s) for the specified step number
void executeStep(byte stepNumber) {
    switch(stepNumber) {
    case 0:
        // just an example...
        Serial.println("executing step 0");
        break;

    case 1:
        Serial.println("executing step 1");
        break;

    case 2:
        Serial.println("executing step 2");
        break;

    default:
        // this is just to help debugging
        Serial.println("Unexpected step number!");
        break;
    }
}


void setup() {
    Serial.begin(115200);
    
    // start from first step
    currStep = 0;
    
    // execute first step
    executeStep(currStep);
}


void loop() {
    // see blink without delay example...
    // if the time has come to process the next step...
    if (millis() - prevMillis >= interval) {
        prevMillis = millis();

        // switch to next step
        currStep++;
        // restart if needed
        if (currStep >= NUM_STEPS) {
            currStep = 0;
        }
        
        // execute the new step
        executeStep(currStep);
    }
}