Timer

I want to create a timer but it says

		timer.schedule(timerTask, 1000, 1000);

that the

.schedule

doesn’t exist

What are you tring to do?

hold a thread? <Bad>

Timer.delay(seconds);

Just create a timer to use in a command:

private Timer t

protected void initialize() {
    	//Reset the Delay timer
    	t.reset();
    	t.start();
}

protected void execute() {
    	
    	//Delay Start of indexer
    	if(t.get()>1.5){
    		//Do Something
    	}
    }

I want to make a motor run for a specific amount of time

your using java ok

What is your base robot? Command, Iterative, Timed, simple?

post what you have already

Im coding in iterative

/*----------------------------------------------------------------------------*/
/* Copyright (c) 2017-2018 FIRST. All Rights Reserved.                        */

/* Open Source Software - may be modified and shared by FRC teams. The code   */
/* must be accompanied by the FIRST BSD license file in the root directory of */
/* the project.                                                               */
/*----------------------------------------------------------------------------*/

package org.usfirst.frc.team7231.robot;


import com.ctre.phoenix.motorcontrol.can.WPI_TalonSRX;
import edu.wpi.first.wpilibj.CameraServer;
import edu.wpi.first.wpilibj.Compressor;
import edu.wpi.first.wpilibj.DigitalInput;
import edu.wpi.first.wpilibj.DoubleSolenoid;
import edu.wpi.first.wpilibj.IterativeRobot;
import edu.wpi.first.wpilibj.Joystick;
import edu.wpi.first.wpilibj.Spark;
import edu.wpi.first.wpilibj.Ultrasonic;
import edu.wpi.first.wpilibj.command.TimedCommand;
import edu.wpi.first.wpilibj.drive.DifferentialDrive;
import java.util.Timer;
import java.util.TimerTask;

public class Robot extends IterativeRobot {
    
    //***Controls***
    Joystick driverController = new Joystick(0);
    Joystick operatorController = new Joystick(1);
    
    //***Pneumatics***
    Compressor airCompressor = new Compressor();

    DoubleSolenoid claw_sUp = new DoubleSolenoid(1, 2, 3);
    DoubleSolenoid claw_sOpen = new DoubleSolenoid(1, 0, 1);

    //***Motors***
    WPI_TalonSRX leftDriveMaster =  new WPI_TalonSRX(1);
    WPI_TalonSRX leftDriveSlave =  new WPI_TalonSRX(2);
    
    WPI_TalonSRX rightDriveMaster =  new WPI_TalonSRX(5);
    WPI_TalonSRX rightDriveSlave =  new WPI_TalonSRX(6);
    
    WPI_TalonSRX armMaster = new WPI_TalonSRX(3);
    WPI_TalonSRX armSlave = new WPI_TalonSRX(4);
    
    Spark claw_mLeft = new Spark(0);
    Spark claw_mRight = new Spark(1);

    
    //***DriveTrain Normal***
    DifferentialDrive driveTrain = new DifferentialDrive(leftDriveMaster, rightDriveMaster);
    
    //***DriveTrain Carrito***
    DriveTrain _driveTrain = new DriveTrain(driverController,leftDriveMaster, leftDriveSlave, rightDriveMaster, rightDriveSlave, driveTrain);


    //***Limitswitch***
    DigitalInput limitswitchTop1 = new DigitalInput(2);
    //DigitalInput limitswitchTop2 = new DigitalInput(1);
    
    DigitalInput limitswitchBottom1 = new DigitalInput(0);
    //DigitalInput limitswitchBottom2 = new DigitalInput(3);
    
    //**Botones Control***
    protected boolean toggle1 = true;
    protected boolean toggle2 = true;
    protected boolean toggle3 = true;
    
    //***Ultrasonic Sensor***
    Ultrasonic u_Sensor = new Ultrasonic(1,1);
    double range = u_Sensor.getRangeInches();
    
    //***Line Follower***
    
    
  
    @Override
    public void robotInit() {
        
        //***SlaveMode Enable
        leftDriveSlave.follow(leftDriveMaster);
        rightDriveSlave.follow(rightDriveMaster);
        
        
        armSlave.follow(armMaster);

        //***Start Aircompressor***
        airCompressor.start();
        
        //***Take Back Pistons***
        claw_sOpen.set(DoubleSolenoid.Value.kReverse);
        claw_sUp.set(DoubleSolenoid.Value.kReverse); 


        //***Cameras***
        CameraServer.getInstance().startAutomaticCapture();
        CameraServer.getInstance().startAutomaticCapture();
        
        //***Set Ultrasonic Sensor in Automatic***
        u_Sensor.setAutomaticMode(true); 
        
    }              
        


    
    @Override
    public void autonomousInit() {
        
        //***Suelta la Canasta***
        claw_sUp.set(DoubleSolenoid.Value.kForward); 
        /*
        claw_sUp.set(DoubleSolenoid.Value.kForward); 
        claw_sUp.set(DoubleSolenoid.Value.kReverse); 
*/
         
    }


    @Override
    public void autonomousPeriodic() {
        /*
        if(seconds != 3)
            seconds = seconds + 1;
            if(seconds == 3)
                if (leftDriveMaster.get() != 0)
        { 
            leftDriveMaster.set(leftDriveMaster.get() - 0.1);
            rightDriveMaster.set(leftDriveMaster.get() - 0.1);
        }
        else {
            leftDriveMaster.set(1);
            rightDriveMaster.set(1);
        }
        */
    }
    
    @Override
    public void teleopInit() {
        
        //***Bajar Canasta***
        claw_sUp.set(DoubleSolenoid.Value.kForward); 
        
    }
    
    @Override
    public void teleopPeriodic() {

        //***Arm***
          if(operatorController.getRawAxis(1) > 0.1) //sube
          {
           if(limitswitchTop1.get() /*&& limitswitchTop2.get()*/)
           {
            armMaster.set(operatorController.getRawAxis(1));
           }
           else
           {
            armMaster.stopMotor();
           }
          }
          else if(operatorController.getRawAxis(1) < -0.1) //baja
          {
           if(limitswitchBottom1.get() /*&& limitswitchBottom2.get()*/)
           {
            armMaster.set(operatorController.getRawAxis(1));
           }
           else
           {
            armMaster.stopMotor();
           }
          }
          else
          {
           armMaster.stopMotor();
          }

          
        
          /*
        //***Garra***
         if(toggle1 == false){ //coger caja
               if(operatorController.getRawButtonPressed(1)){
                   toggle1 = true;
                   claw_mLeft.set(-1);
                   claw_mRight.set(-1);
                   
                   if(claw_mLeft.get() < 0)
                   {
                       claw_sOpen.set(DoubleSolenoid.Value.kForward);
                   }
               }
         
         else if(toggle1 == true){//sacar caja
                   if(operatorController.getRawButtonPressed(1)){
                       toggle1 = false;
                       claw_mLeft.set(1);
                       claw_mRight.set(1);
                       
                       if(claw_mLeft.get() > 0)
                       {
                           claw_sOpen.set(DoubleSolenoid.Value.kReverse); 
                       } 
                   }
               }
         else
         {
             claw_mLeft.stopMotor();
             claw_mRight.stopMotor();
         }
         }
         */
         

          if(operatorController.getRawButton(1)) {
                 /*
              claw_mLeft.set(-1);
                  claw_mRight.set(-1);
              
                  if(claw_mLeft.get() < 0)
                  
                  {*/
                      claw_sOpen.set(DoubleSolenoid.Value.kForward);
                  //}
          }
          else if(operatorController.getPOV(0) >= 0) {
          //else if(operatorController.getRawButton(2)) {
                 /*
              claw_mLeft.set(1);
                  claw_mRight.set(1);
             
                  if(claw_mLeft.get() > 0)
                  {*/
                      claw_sOpen.set(DoubleSolenoid.Value.kReverse);
                //  }
          }
          else
          {
              claw_mLeft.set(0);
                 claw_mRight.set(0);
          }
          
          
          
          
          

          /*
          if(operatorController.getRawButton(2)) {
                  claw_mLeft.set(0.5);
                  claw_mRight.set(0.5);
              
                  if(claw_mLeft.get() > 0)
                  {
                      claw_sOpen.set(DoubleSolenoid.Value.kForward);
                  }
          }
          else
          {
              claw_mLeft.set(0);
                 claw_mRight.set(0);
          }
          */
          
          
          
          /*
          if(operatorController.getRawButtonPressed(2)){
                  //toggle1 = true;
                  claw_mLeft.set(1);
                  claw_mRight.set(1);
             
                  if(claw_mLeft.get() > 0)
                  {
                      claw_sOpen.set(DoubleSolenoid.Value.kReverse);
                  }
         }
         */
          
          
          
          
        //***Take Back Arm in Dead Time***
        if(driverController.getRawButtonPressed(2) && driverController.getRawButtonPressed(3))
        {
               claw_sUp.set(DoubleSolenoid.Value.kReverse); 
        }

        
    
         //***Sensibilidad del Joystick de Manejo***
         double x = driverController.getRawAxis(1)/1.4;
         double y = driverController.getRawAxis(5)/1.4;
         
         //***Modos de Manejo***
        //driveTrain.tankDrive(-x, -y);
        // driveTrain.tankDrive(driverController.getRawAxis(1), driverController.getRawAxis(5)); 
         //driveTrain.arcadeDrive(driverController.getRawAxis(1), driverController.getRawAxis(0)); 
         _driveTrain.Manejo();
        
        
    }

    
    @Override
    public void testPeriodic() {
    }
}

I want it for the autonomous mode

So Drive forward for 3 seconds and decelerate?

Its not the way I would do it but it looks like your keeping it simple. Try:



Timer t;

@Override
    public void autonomousInit() {
        t.reset;
        t.start;
        //***Suelta la Canasta***
        claw_sUp.set(DoubleSolenoid.Value.kForward); 
        /*
        claw_sUp.set(DoubleSolenoid.Value.kForward); 
        claw_sUp.set(DoubleSolenoid.Value.kReverse); 
*/
         
    }


@Override
    public void autonomousPeriodic() {
        /*
        if(t.get() <  3)
            [STRIKE]seconds = seconds + 1;[/STRIKE]
            if(t.get() > 3)
                if (leftDriveMaster.get() != 0)
        { 
            leftDriveMaster.set(leftDriveMaster.get() - 0.1);
            rightDriveMaster.set(leftDriveMaster.get() - 0.1);
        }
        else {
            leftDriveMaster.set(1);
            rightDriveMaster.set(1);
        }

Next I would like to see you get some encoders on the drivetrain and drive distance. Timers will not get you very repeatable actions with battery state. It should get you across the auto line.

When i create a timer and the i want to set the timer to reset and then start in autonomous it doesnt give the option of

t.reset();/CODE] or 

t.start();

Two things:

Did you Import,

import edu.wpi.first.wpilibj.Timer;

The other thing maybe cause I forgot to finnish declaring the timer,


Timer t = new Timer();

Nop, still cant reset or start the timer. Could it be that I’m using iterative and not timed based?

Yes, it was that. You need to use Timed and not Iterative.

Nevermind when I copy all the other code, it doesn’t give me the option. When I create a new robot project either iterative or timed it let me do it but when I copy all the code, it disappears.

I think there is a bug. I created a new project, first create the timer and inserted the start and reset commands, then copied all the other code and it’s working fine.

False.

Take a look at your imports on the Iterative Robot program. I bet your importing

import java.util.Timer;

You need the

import edu.wpi.first.wpilibj.Timer;

Because we didn’t have time to instal encoders HAHAHA, so I’m going to have the timer as a backup.