How to use Millis(function) to rid Delays in my sketch

I need some understanding in applying the millis() into this sketch. I have not done anything about counters or timers everything is this method. And now I think I am running into problem when I run this one with another program everything starts to hang up. cheers

#include <Wire.h>

#include <Stepper.h>

void setup() {

//configure pin2,4 as an input and enable the internal pull-up resistor

pinMode(2, INPUT_PULLUP);
pinMode(4, INPUT_PULLUP);
pinMode(5, OUTPUT); // set pin 5 dir output
pinMode(7, OUTPUT); // set pin 7 step output
digitalWrite(5, HIGH); // set pin 5 initially to LOW
digitalWrite(7, LOW); // set pin 7 initially to LOW

Serial.begin(9600);
}

void loop() {
//read the pushbutton value into a variable

int switchVala = digitalRead(2);
int switchValb = digitalRead(4);

if (switchVala == LOW){
digitalWrite(5, LOW);
digitalWrite(7, HIGH); // Toggle pin 7 to HIgh
delayMicroseconds(500); // Wait
digitalWrite(7, LOW); // Toggle pin 9 to LOW
delayMicroseconds(500); // Wait
}

else if (switchValb == LOW){
digitalWrite(5, HIGH);
digitalWrite(7, HIGH);
delayMicroseconds(500);
digitalWrite(7, LOW);
delayMicroseconds(500);
}
}

Your delays are 500 micro seconds. Millis() is milliseconds so your delay is 0.5milliseconds.

You might have to re think using millis().

Weedpharma

I am wondering the same thing, but i don't want to thread jack so i will post my code into an new tread Thx

Puno: And now I think I am running into problem when I run this one with another program everything starts to hang up.

What does this mean?

The code you posted looks OK. 500 microseconds is not very long, so should not cause visible hangups.

If you are saying this does not work when combined with some other code, you need to post the code that does not work, not this code. Please post the combined code that you are having a problem with, or we cannot help you.

@Puno: Please edit your post, select the code, and put it between [code][/code] tags.

You can do that by hitting the “Code” icon above the posting area. It is the first icon, with the symbol: </>

The demo several things at a time illustrates how to use millis() to manage timing. The same technique works with micros()

An Arduino can do 8000 instructions in 500 µsecs.

...R