Elevator - Sonar Problem

My students tried three variations on the elevator project (sonar, line follower, and quad encoder). None of my six groups could get their elevator to work correctly (push button sends the elevator to the appropriate floor). The sonar was set in cm and the debugger displayed the correct values, but we could not get it to go to each floor. We were using RobotC 4.5, we downloaded the firmware and master CPU files before downloading the code, and a fresh battery was used often.

Below is our code, could anyone give me pointers on how to get this working. The compiler didn’t find any coding errors and I couldn’t find any hardware problems using the motor and senor debugger.

#pragma config(Sensor, dgtl3, LimitSwitch1, sensorTouch)
#pragma config(Sensor, dgtl4, LimitSwitch2, sensorTouch)
#pragma config(Sensor, dgtl5, LimitSwitch3, sensorTouch)
#pragma config(Sensor, dgtl6, Sonar, sensorSONAR_inch)
#pragma config(Motor, port2, MainMotor, tmotorVex393_MC29, openLoop)
//!!Code automatically generated by ‘ROBOTC’ configuration wizard !!//

task main()
{
while(1==1)

if(SensorValue(LimitSwitch1)==1) //* push limit switch 1 to make the elevator go to the first floor*//
{
if(SensorValue(Sonar)==1)
{stopMotor(MainMotor);}

if(SensorValue(Sonar) >1)
{startMotor(MainMotor,60);
untilSonarLessThan(1.1,Sonar);
stopMotor(MainMotor);}
}

if(SensorValue(LimitSwitch2)==1) //* Push limit switch 2 to make the elevator go to floor 2, regardless of where it was before.*//
{
if(SensorValue(Sonar)==4)
{stopMotor(MainMotor);}

if(SensorValue(Sonar) >4)
{startMotor(MainMotor,60);
untilSonarLessThan(4.1,Sonar);
stopMotor(MainMotor);}

if(SensorValue(Sonar) <4)
{startMotor(MainMotor,-60);
untilSonarGreaterThan(3.9,Sonar);
stopMotor(MainMotor);}
}

if(SensorValue(LimitSwitch3)==1) //* Push limit switch 3 to make the elevator go to floor 3.*//
{
if(SensorValue(Sonar) <6.9)
{startMotor(MainMotor,-60);
untilSonarGreaterThan(6.9,Sonar);
stopMotor(MainMotor);}

	else(SensorValue(Sonar)==7);
		{stopMotor(MainMotor);}
}

}

There are couple of areas to address.

A while loop will execute the statement that follows. In the code you posted this will only be the conditional check for the first touch switch, to check all three switches braces must be used to surround the test for all three switches.

SensorValue returns a value as an integer (ie. you cannot retrieve a value of 1.1 inches, only 1 inch or 2 inches ), if you need more accuracy than that I suggest you use cm or mm as the units.

A revised version of the code that may work (I don’t have an elevator to test this on :slight_smile: ) could be as follows.

Regards

James Pearman
Robomatter

#pragma config(Sensor, dgtl3,  LimitSwitch1,   sensorTouch)
#pragma config(Sensor, dgtl4,  LimitSwitch2,   sensorTouch)
#pragma config(Sensor, dgtl5,  LimitSwitch3,   sensorTouch)
#pragma config(Sensor, dgtl6,  Sonar,          sensorSONAR_mm)
#pragma config(Motor,  port2,           MainMotor,     tmotorVex393_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

// Use mm for more accuracy

task main()
{
    while(1==1)
        {
        //* push limit switch 1 to make the elevator go to the first floor*//
        if(SensorValue LimitSwitch1 ] == 1 )   
            {
            // Elevator is at low level
            if(SensorValue Sonar ] <= 30 )
                {
                stopMotor(MainMotor);
                }
            else
            if(SensorValue Sonar ] > 30)
                {
                startMotor(MainMotor,60);
                untilSonarLessThan( 35, Sonar );
                stopMotor(MainMotor);
                }
            }
    
        //* Push limit switch 2 to make the elevator go to floor 2, regardless of where it was before.*//
        if(SensorValue LimitSwitch2 ] == 1 )   
            {
            if(SensorValue Sonar ] >= 95 || SensorValue Sonar ] <= 105 )
                {
                stopMotor(MainMotor);
                }
            else    
            if(SensorValue Sonar ] > 105)
                {
                startMotor(MainMotor,60);
                untilSonarLessThan(105,Sonar);
                stopMotor(MainMotor);
                }
            else
            if(SensorValue Sonar ] < 95 )
                {
                startMotor(MainMotor,-60);
                untilSonarGreaterThan(95,Sonar);
                stopMotor(MainMotor);
                }
            }
    
        //* Push limit switch 3 to make the elevator go to floor 3.*//
        if(SensorValue LimitSwitch3 ] == 1 )   
            {
            if(SensorValue Sonar ] < 175)
                {
                startMotor(MainMotor,-60);
                untilSonarGreaterThan(170,Sonar);
                stopMotor(MainMotor);
                }
            else
            // Elevator at top
            if(SensorValue Sonar ] >= 175 )
                {
                stopMotor(MainMotor);
                }
            }
        }
}