Need help with robotc asap! Thank you!

Hello to who reads this.

I am a student currently working on a project that requires me to make an elevator.
In the process of creating the elevator, I have ran into some code problems.
Below is the code that I have been working on, and I have not been able to get any motor to run whatsoever.

Before I show the code, this is how it should function.

  1. I press a button.
  2. The code runs through yValue, which gives the future floor value.
  3. Code calculates difference between future floor and current floor (y - x)
  4. Moves the desired distance
  5. x, the current floor value, now equals y
  6. Repeat the process

#pragma config(Sensor, in1, door, sensorPotentiometer)
#pragma config(Sensor, dgtl5, Floorone, sensorTouch)
#pragma config(Sensor, dgtl6, Floortwo, sensorTouch)
#pragma config(Sensor, dgtl7, Floorthree, sensorTouch)
#pragma config(Sensor, dgtl8, Encoder, sensorQuadEncoder)
#pragma config(Motor, port2, Cable, tmotorVex269_MC29, openLoop)
#pragma config(Motor, port3, door, tmotorVex269_MC29, openLoop)

void halt()
{
motor[Cable] = 0;
}
int yValue(int prevY) //The y value is the value of the future floor
{
int y;
if(SensorValue[Floorone] == 1)
{y = 1;}
else if(SensorValue[Floortwo] == 1)
{y = 2;}
else if(SensorValue[Floorthree] == 1)
{y = 3;}
return y;
}
/void move(int diff)
{
if(diff < 0)
{
while(SensorValue[Encoder] > 500 * diff)
{
motor[Cable] = -63;
}
}
else if(diff > 0)
{
while(SensorValue[Encoder] < 500 * diff)
{
motor[Cable] = 63;
}
}
halt();
}
/
void moveTime(int i)
{
writeDebugStreamLine("int i is:%i ", i);
if(i < 0 && i >= -2)
{
motor[cable] = 63;
wait(1000);
}
if(i > 0 && i <= 2)
{
motor[cable] = -63;
wait(1000);
}
halt();
wait(1000);
}

task main()
{
int x = 1;
int y = 1;
SensorValue[Encoder] = 0;
while(true)
{
//waitUntil(SensorValue[Floorone] == 1 || SensorValue[Floortwo] == 1 || SensorValue[Floorthree] == 1);
for(int i = 0; i < 4; i++)
{
y = yValue(y);
writeDebugStreamLine(“int y and x is:%i “, y ,”,”, x);
int diff = y - x;
moveTime(diff);
halt();
x = y;
}
halt();
}
}

Note: I have two functions, move and moveTime. move uses the encoder and moveTime uses time to function. I don’t mind a fix for either one, however I would like a solution to not require any other sensors as we are short on resources and time to implement them. Thank you again.

I don’t have much time, but here are a few things I spotted:

  1. What is the point of passing prevY if you don’t use it here: int yValue(int prevY)?
  2. You say “x, the current floor value, now equals y.” However, you set y before moving in your for loop. You don’t change y. Then you set x to y. That means x is set to the prior floor.
  3. If yValue initially returns 1, then diff=0, so it won’t move. Then x will be set to 1. Next time through yValue will still return 1, so it won’t move, and this will happen forever.
  4. You say “Moves the desired distance” and “//waitUntil(SensorValue[Floorone] == 1 || SensorValue[Floortwo] == 1 || SensorValue[Floorthree] == 1);” but I don’t see anywhere where you’re making sure you move exactly to one of those floors as you’re only using moveTime.

Thanks!

I’m not sure if this is related to your answer, however I found that by removing my yValue function and placing it into the while loop itself fixed it entirely.

^^;

With a little more time here…

I don’t understand why you have the for loop at all. You do something 4 times and repeat that forever with while(true). But you’re not doing anything with i, and 4*forever is still forever. You could remove the for loop entirely. That would clean up the code a lot.

Those buttons are the buttons to be pressed to tell it where to go, right? I was misunderstanding what you were setting before. So you tell it the floor you want, and record that as where you are, assuming you’ve actually moved to it. That assumption is worrisome. It would be find in pure computer science, but it’s problematic with physical scenarios. So my big question about how you know it’s actually at the new floor remains. But you do have encoders there you can use, so I would use them instead of the timer.

I think I do now see the big problem in the original code, though. Let’s say you don’t press anything, so y=1 and x=1. You then run moveTime(0), which specifically sets the motor to stop for 1000 ms (the halt and wait at the end). You now have a tiny fraction of a second to get the new button press in before the motors again stop for 1000 ms. This essentially means the only way a button press would necessarily register is if it’s held down for more than 1000 ms so it necessarily gets picked up. If it’s held down for less time, like a typical quick press, there is only a chance it will register.

What I would do is cycle through checking the buttons and only call the command to run the motor if a new floor has been selected.