ROBOTC.net forums
http://robotc.net/forums/

Vex Elevator Code not working
http://robotc.net/forums/viewtopic.php?f=11&t=11235
Page 1 of 1

Author:  Michael23B [ Tue May 19, 2015 10:13 am ]
Post subject:  Vex Elevator Code not working

I have no idea why my code would work, so if you guys could fix and/or optimize it that would be great! The LEDs were the only thing working a bit ago, but now nothing works, and I have no idea why. What should happen is if you press a limit switch, it calculates the difference between which limit switch you pressed (1 2 or 3) and the current floor the elevator is on. It then should move the elevator cart accordingly, and also update the LEDs. If nothing is pressed for a while, it should go back down to the first floor. I would appreciate some help :)
Code:
#pragma config(Sensor, dgtl1,  LimitSwitch1,   sensorTouch)
#pragma config(Sensor, dgtl2,  LimitSwitch2,   sensorTouch)
#pragma config(Sensor, dgtl3,  LimitSwitch3,   sensorTouch)
#pragma config(Sensor, dgtl4,  SONAR,          sensorSONAR_inch)
#pragma config(Sensor, dgtl10, LED3,           sensorLEDtoVCC)
#pragma config(Sensor, dgtl11, LED2,           sensorLEDtoVCC)
#pragma config(Sensor, dgtl12, LED1,           sensorLEDtoVCC)
#pragma config(Motor,  port2,           mainmotor,     tmotorVex269_MC29, openLoop)
//*!!Code automatically generated by 'ROBOTC' configuration wizard               !!*//

task main()
{
int level;
int time;
int F;
int A;
int change;
level = 0;
time = 0;
startMotor(mainmotor,63);
while(SensorValue(SONAR)>2)
{
}
stopMotor(mainmotor);
turnLEDOn(LED3);
while(true)
{
   time = time + 1;
   if(SensorValue(LimitSwitch1)==1 || SensorValue(LimitSwitch2)==1 || SensorValue(LimitSwitch3)==1)
   {
      if(SensorValue(LimitSwitch1)==1)
      {
            F = 0;
      }
      if(SensorValue(LimitSwitch2)==1)
      {
         F = 1;
      }
      if(SensorValue(LimitSwitch3)==1)
      {
         F = 2;
      }
      time = 0;
      change = F - level;
      turnLEDOff(LED1);
      turnLEDOff(LED2);
      turnLEDOff(LED3);
      level = level + change;
      A = 0;
      if(change != 0)
      {
         A = change/abs(change);
      }
      startMotor(mainmotor,-63*A);
      while(SensorValue(SONAR) != 2*(F + 1))
      {
      }
      stopMotor(mainmotor);
      if(level==0)
      {
         turnLEDOn(LED3);
      }
      if(level==1)
      {
         turnLEDOn(LED2);
      }
      if(level==2)
      {
         turnLEDOn(LED1);
      }
   }
   if(time == 500 && SensorValue(SONAR)>2)
   {
      level = 0;
      startMotor(mainmotor,63);
      while(SensorValue(SONAR) > 3)
      {
      }
      stopMotor(mainmotor);
   }
}
}

Author:  Michael23B [ Tue May 19, 2015 4:16 pm ]
Post subject:  Re: Vex Elevator Code not working

Nevermind, I think I got it to work even though I hardly did anything.... just need to adjust the SONAR so it works properly.

Page 1 of 1 All times are UTC - 5 hours [ DST ]
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/