"Smart" wheelchar using IR and sonar sensors

I am working on an Arduino Uno project for a friend, who was recently paralyzed from the waist down. He uses a motorized wheelchair to get around. I am fairly new to programming and could use some help.

The project places 4 IR sensors underneath the chair (SHARP distance sensors), facing down, to notify him of a curb drop or changing terrain. We've also placed a 2 sonar sensors facing back (Ping))) ultrasonic), which notify him when someone is approaching from behind. To notify him, we are using a piezo beeper to notify him of the change, and an arrangement of LEDs, which correspond to which sensors are being activated (e.g. the front left IR sensors lights up the top left light on the LED grid).

To turn the sensor capabilities on/off, we have an enable button on a platform above his waist that he can control with his hand.

Here are some issues I'm coming across:

  • The enable button is very spotty. Sometimes when I press and release it, nothing happens.
  • I'd like the piezo to beep once, fairly fast, and also light up the corresponding LED. I do not know how to do this.
  • How can I get the corresponding LED to light up?

My code:

 #define enable 2 // pushbutton

#define topSonar 3 // 
#define bottomSonar 4

#define topSonarLight 5
#define bottomSonarLight 6

#define frontLeftLight 7
#define frontRightLight 8
#define backLeftLight 9
#define backRightLight 10

#define piezo 11

#define frontLeftSensor A0
#define frontRightSensor A1
#define backLeftSensor A2
#define backRightSensor A3

int i, enableState, lastEnableState = 0; // enable button vars

// vars for timing without delays
int interval = 250; // ms
int t0 = 0; // initial time

long duration; // used in sonar ping() function
int topDistance; // used in ping() also
int bottomDistance;
int distance;

int sonarDangerZone = 80; // cm
int irDangerZone = 300; // 

void setup() {
	Serial.begin(9600);
	pinMode(enable, INPUT);
	pinMode(piezo, OUTPUT);
	pinMode(sonarLight, OUTPUT);
	pinMode(frontLeftLight, OUTPUT);
	pinMode(frontRightLight, OUTPUT);
	pinMode(backLeftLight, OUTPUT);
	pinMode(backRightLight, OUTPUT);
	pinMode(topSonar, OUTPUT);
	pinMode(bottomSonar, OUTPUT);
} // end setup

unsigned long ping() {
	
  pinMode(topSonar, OUTPUT);
  digitalWrite(topSonar, LOW);
  delayMicroseconds(2);
  digitalWrite(topSonar, HIGH);
  delayMicroseconds(5);
  digitalWrite(topSonar, LOW);
  
  pinMode(topSonar, INPUT);
  duration = pulseIn(topSonar, HIGH);
 int topDistance = duration / 29 /2;

	pinMode(bottomSonar, OUTPUT); 
	digitalWrite(bottomSonar, LOW); 
	delayMicroseconds(2); 
	digitalWrite(bottomSonar, HIGH); 
	delayMicroseconds(5); 
	digitalWrite(bottomSonar, LOW); 
	pinMode(bottomSonar, INPUT); 
	digitalWrite(bottomSonar, HIGH); 
  
	int bottomEcho = pulseIn(bottomSonar, HIGH); 
	int bottomDistance = bottomEcho / 29 / 2; // convert to cm
	
	if(topDistance < bottomDistance) {
		distance = topDistance;
	} else {
		distance = bottomDistance;
	}

	return distance;
	
} // end ping()

void checkEnable() {
	enableState = digitalRead(enable);
	
	if(enableState != lastEnableState) {
		if (enableState == HIGH) {
			i++;
		} // 
	} // 
	
	lastEnableState = enableState;
	
	return;
} // end checkEnable

void loop() {
	
	checkEnable();
	
	if(i % 2 == 1) { 
		while(millis() > t0 + interval) {
			
			if(ping() > 30) {
				
				digitalWrite(piezo, HIGH);
                                digitalWrite(topSonarLight, HIGH);


				
				
			} else {
				digitalWrite(piezo, LOW);
                                digitalWrite(topSonarLight, LOW); // I want this to be either the top or bottom light depending on which sensor is triggered more
			}
			
		
			
			while(analogRead(frontLeftSensor) < irDangerZone) { // bools
				digitalWrite(frontLeftLight, HIGH);
			} 
			
			
			while(analogRead(frontRightSensor) < irDangerZone) {
				digitalWrite(frontRightLight, HIGH);
			} // front right
			
			while(analogRead(backLeftSensor) < irDangerZone) {
				digitalWrite(backLeftLight, HIGH);
			} // back left
			
			while(analogRead(backRightSensor) < irDangerZone) {
				digitalWrite(backRightLight, HIGH);
			} // back right
			
			t0 = millis();
		} // end interval while
	} // end enable if
} // end loop

I'd appreciate any pointers. Thanks.

The enable function seemed to be a hardware issue, and this has been fixed.

I'd still appreciate any guidance on the fundamentals of my program. Thanks.