permanent blinking while code runs

I try to get a constant blinking, while my program runs, with the following code:

int inPin5 = 1;  
int val5 = 0; 
int inPin0 = 0;  
int val0 = 0;
int seuil = 300;
long randNumber;
int USpin = 8;

int motorPin1 = 9;
int motorPin2 = 10;
int motorPin3 = 11;
int motorPin4 = 12;
int delayTime = 1000;

void setup() {
  randomSeed(analogRead(3));
  pinMode(motorPin1, OUTPUT);
  pinMode(motorPin2, OUTPUT);
  pinMode(motorPin3, OUTPUT);
  pinMode(motorPin4, OUTPUT);
  pinMode(USpin, OUTPUT);
}

void blinkUS(){
digitalWrite(USpin, HIGH);   
  delay(500);                  
  digitalWrite(USpin, LOW);    
  delay(500);                  
}

void loop() {
  randNumber = random(1000, 10000);
  int i = 0;
  val5 = analogRead(inPin5);  
 val0 = analogRead(inPin0); 
   if (val5 > seuil) { 
     blinkUS();
    analogWrite(motorPin1, 0);
  digitalWrite(motorPin2, LOW);
  analogWrite(motorPin3, 0);
  digitalWrite(motorPin4, LOW);
  delay(1000);
   blinkUS();
  analogWrite(motorPin1, 180);
  digitalWrite(motorPin2, LOW);
  analogWrite(motorPin3, 180);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
   blinkUS();
   analogWrite(motorPin1, 120);
  digitalWrite(motorPin2, LOW);
  analogWrite(motorPin3, 120);
  digitalWrite(motorPin4, HIGH); 
  delay(randNumber);
}if (val0 > seuil) { 
   blinkUS();
   analogWrite(motorPin1, 0);
  digitalWrite(motorPin2, LOW);
  analogWrite(motorPin3, 0);
  digitalWrite(motorPin4, LOW);
  delay(1000);
   blinkUS();
  analogWrite(motorPin1, 180);
  digitalWrite(motorPin2, LOW);
  analogWrite(motorPin3, 180);
  digitalWrite(motorPin4, LOW);
  delay(delayTime);
   blinkUS();
  analogWrite(motorPin1, 120);
  digitalWrite(motorPin2, HIGH);
  analogWrite(motorPin3, 120);
  digitalWrite(motorPin4, LOW); 
  delay(randNumber);

  }else{
     blinkUS();
    analogWrite(motorPin1, 170);
  digitalWrite(motorPin2, HIGH);
  analogWrite(motorPin3, 160);
  digitalWrite(motorPin4, HIGH);
  
  }
}

Unfornutaly this does not work properly! The blinking is very irregular except when the "else" runs. Does anyone knows why?

Does anyone knows why?

Maybe, but software is not my strongest suit.

You have delays in the loop if parts that are not in the else part. Delays are blocking (nothing proceeds until delay completes). so your void blinkUS() is not being called at a consistant rate from the loop.

Check out the example of blink without using a bocking delay: http://www.arduino.cc/en/Tutorial/BlinkWithoutDelay

Using calculated time periods rather then blocking delays is one way to get consistant cycle times for your program.

Lefty

Yes, the rest of the code takes time so it takes a bit of time before it gets to each delay. The time it takes depends on how much and what code it is running.

You need, as retrolefty said, to use the example blink code without delays. Then, as long as the other code takes less than your delay time to run then it will be an even blink...

Mowcius

There are many libraries that can help you do this.

Here is one (and also mine): http://www.arduino.cc/playground/Code/TimedAction