Desperate Ultrasonic library coding simplification needed

This is a code that I have been struggling with, it is not efficient at all but I am out of time to fix it on my own and desperately need some help. When I moved from analog IR sensors to digital ultrasonic (for cost and better range) and then implemented the new ultrasonic ping library I got in over my head and now this is all I can do to keep the project functioning.

btw this is for a mega 2560 and there 12 LEDs but if this code can be simplified I can add the rest.

Please help.

#include <Ultrasonic.h>
Ultrasonic ultrasonic1 (22,23);
Ultrasonic ultrasonic2 (24,24);

int pwmLed1 = 2;
int pwmLed2 = 3;

// start of the low flicker variables 
static int flicker_low_min1 = 50;
static int flicker_low_max2 = 60;

// end of the low flicker variables
static int flicker_low_min3 = 61;
static int flicker_low_max4 = 71;

// start of the middle flicker variables 
static int flicker_mid_min1 = 120;
static int flicker_mid_max2 = 130;

// end of the middle flicker variables
static int flicker_mid_min3 = 140;
static int flicker_mid_max4 = 150;

// start of the high flicker variables 
static int flicker_high_min1 = 200;
static int flicker_high_max2 = 240;

// end value of the high flicker variables
static int flicker_high_min3 = 230;
static int flicker_high_max4 = 256;

// delay between each low-high-low cycle
static int flicker_hold_min = 40; // milliseconds
static int flicker_hold_max = 80; // milliseconds

// delay after each low-high-low cycle
static int flicker_pause_min = 5; // milliseconds
static int flicker_pause_max = 10;  // milliseconds

// delay low to high and high to low cycle
static int flicker_speed_min = 900; // microseconds
static int flicker_speed_max = 1000; // microseconds

void setup() {
  Serial.begin(9600);
  pinMode (pwmLed1, OUTPUT);
  pinMode (pwmLed2, OUTPUT);
  }

//low flicker start, stop, random
int flicker_random_low_start = 0;
int flicker_random_low_end = 0;
int flicker_random_low = 0;

//mid flicker start, stop, random
int flicker_random_mid_start = 0;
int flicker_random_mid_end = 0;
int flicker_random_mid = 0;

//high flicker start, stop, random
int flicker_random_high_start = 0;
int flicker_random_high_end = 0;
int flicker_random_high = 0;

// flicker random speed
int flicker_random_speed_start = 0;
int flicker_random_speed_end = 0;

void loop()
{
  int brightness1, brightness2; 
  int distance1, distance2;

//low flicker algorithm
  flicker_random_low_start = random(flicker_low_min1, flicker_low_max2);
  flicker_random_low_end = random(flicker_low_min1, flicker_low_max2);

// random time for low
  flicker_random_low = random(flicker_low_min3, flicker_low_max4);

  //mid flicker algorithm
  flicker_random_mid_start = random(flicker_mid_min1, flicker_mid_max2);
  flicker_random_mid_end = random(flicker_mid_min1, flicker_mid_max2);

  // random time for mid
  flicker_random_mid = random(flicker_low_min3, flicker_low_max4);

  //high flicker algorithm
  flicker_random_high_start = random(flicker_high_min1, flicker_high_max2);
  flicker_random_high_end = random(flicker_high_min1, flicker_high_max2);

  // random time for high
  flicker_random_high = random(flicker_high_min3, flicker_high_max4);

  // random time for speed
  flicker_random_speed_start = random(flicker_speed_min, flicker_speed_max);
  flicker_random_speed_end = random(flicker_speed_min, flicker_speed_max);

  // calculating ultrasonic distance
  distance1 = ultrasonic1.Ranging(CM); 
  distance2 =ultrasonic2.Ranging(CM);
      
  // mapping brightness to distance
  brightness1 = map (distance1, 256, 70, 30, 256);
  brightness2 = map (distance2, 256, 70, 30, 256);
      
  // mapping ifs and exclusions
  if (distance1 > 550)distance1 = 550;
  if(brightness1>256)brightness1=255;
  if (brightness1< 30) brightness1 = 30;

  if (distance2 > 550)distance2 = 550;
  if(brightness2>256)brightness2=255;
  if (brightness3< 30) brightness3 = 30;

  if (distance3 > 550)distance3 = 550;
  if(brightness3>256)brightness3=255;
  if (brightness3< 30) brightness3 = 30;

  if (distance4 > 550)distance4 = 550;
  if(brightness4>256)brightness4=255;
  if (brightness4< 30) brightness4 = 30;

  if (distance5 > 550)distance5 = 550;
  if(brightness5>256)brightness5=255;
  if (brightness5< 30) brightness5 = 30;

  if (distance6 > 550)distance6 = 550;
  if(brightness6>256)brightness6=255;
  if (brightness6< 30) brightness6 = 30;

  //LED1
  // low flicker write to LED1
  if (distance1 >= 300) 
  {
    //map low brightness
    analogWrite (pwmLed1, brightness1);
    delayMicroseconds(500);

    //map low flicker
    for (int i = flicker_random_low_start; i<flicker_random_low; i++) {
      analogWrite(pwmLed1, i);
      delayMicroseconds(flicker_random_speed_start);
    }
    delay(random(flicker_hold_min, flicker_hold_max));
    // low flicker
    for (int i = flicker_random_low; i>=flicker_random_low_end; i--) {
      analogWrite(pwmLed1, i);
      delayMicroseconds(flicker_random_speed_end);
    }
    // pause
    delay(random(flicker_pause_min, flicker_pause_max));
  }

  // mid flicker write to LED
  if (distance1 <300) 
  {
    //map mid brightness
    analogWrite (pwmLed1, brightness1);
    delayMicroseconds(500);
    //map mid flicker
    for (int i = flicker_random_mid_start; i<flicker_random_mid; i++) {
      analogWrite(pwmLed1, i);
      delayMicroseconds(flicker_random_speed_start);
    }
    delay(random(flicker_hold_min, flicker_hold_max));
    // mid flicker
    for (int i = flicker_random_mid; i>=flicker_random_mid_end; i--) {
      analogWrite(pwmLed1, i);
      delayMicroseconds(flicker_random_speed_end);
    }
    // pause
    delay(random(flicker_pause_min, flicker_pause_max));
  }

// high flicker write to LED
  if (distance1 < 200 ) 
  {
    //map high brightness
    analogWrite (pwmLed1, brightness1);
    delayMicroseconds(500);
    // map high flicker
    for (int i = flicker_random_high_start; i<flicker_random_high; i++) {
      analogWrite(pwmLed1, i);
      delayMicroseconds(flicker_random_speed_start);
    }
    // hold
    delay(random(flicker_hold_min, flicker_hold_max));
    // high flicker
    for (int i = flicker_random_high; i>=flicker_random_high_end; i--) {
      analogWrite(pwmLed1, i);
      delayMicroseconds(flicker_random_speed_end);
    }
    // pause
    delay(random(flicker_pause_min, flicker_pause_max));
  }


  // LED off
  if (distance1 <70) 
  {
    analogWrite (pwmLed1, brightness1);
    delay (10000);
  }  

  //LED2
  // low flicker write to LED2
  if (distance2 >= 300) 
  {
    //map low brightness
    analogWrite (pwmLed2, brightness2);
    delayMicroseconds(500);

    //map low flicker
    for (int i = flicker_random_low_start; i<flicker_random_low; i++) {
      analogWrite(pwmLed2, i);
      delayMicroseconds(flicker_random_speed_start);
    }
    delay(random(flicker_hold_min, flicker_hold_max));
    // low flicker
    for (int i = flicker_random_low; i>=flicker_random_low_end; i--) {
      analogWrite(pwmLed2, i);
      delayMicroseconds(flicker_random_speed_end);
    }
    // pause
    delay(random(flicker_pause_min, flicker_pause_max));
  }

// mid flicker write to LED
  if (distance2 <300) 
  {
    //map mid brightness
    analogWrite (pwmLed2, brightness2);
    delayMicroseconds(500);
    //map mid flicker
    for (int i = flicker_random_mid_start; i<flicker_random_mid; i++) {
      analogWrite(pwmLed2, i);
      delayMicroseconds(flicker_random_speed_start);
    }
    delay(random(flicker_hold_min, flicker_hold_max));
    // mid flicker
    for (int i = flicker_random_mid; i>=flicker_random_mid_end; i--) {
      analogWrite(pwmLed2, i);
      delayMicroseconds(flicker_random_speed_end);
    }
    // pause
    delay(random(flicker_pause_min, flicker_pause_max));
  }

// high flicker write to LED
  if (distance2 < 200 ) 
  {
    //map high brightness
    analogWrite (pwmLed2, brightness2);
    delayMicroseconds(500);
    // map high flicker
    for (int i = flicker_random_high_start; i<flicker_random_high; i++) {
      analogWrite(pwmLed2, i);
      delayMicroseconds(flicker_random_speed_start);
    }
    // hold
    delay(random(flicker_hold_min, flicker_hold_max));
    // high flicker
    for (int i = flicker_random_high; i>=flicker_random_high_end; i--) {
      analogWrite(pwmLed2, i);
      delayMicroseconds(flicker_random_speed_end);
    }
    // pause
    delay(random(flicker_pause_min, flicker_pause_max));
  }

// LED off
  if (distance2 <70) 
  {
    analogWrite (pwmLed2, brightness2);
    delay (10000);
  }  
  
  Serial.print(ultrasonic1.Ranging(CM));
  Serial.print("cm");
  Serial.print(" : ");
  Serial.print(brightness1);
  Serial.print(" --- ");
  Serial.print(ultrasonic2.Ranging(CM));
  Serial.print("cm");
  Serial.print(" : ");
  Serial.print(brightness2);
  Serial.println(" --- ");
  delay (5);
}

Moderator edit: Ouch! My poor scrolling finger. CODE TAGS added.

I see all sorts of delay() and delayMicroseconds()... these all need to be eliminated. See "Blink without delay:

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

tylernt: I see all sorts of delay() and delayMicroseconds()... these all need to be eliminated. See "Blink without delay:

http://arduino.cc/en/Tutorial/BlinkWithoutDelay

Also, read up on PWM, as a way to get rid of all those flickers.

An array could help you code the code in half (about).