LST Robotics Logo

Learning • Sensors • Tech

LST Robotics

Lessons
← Back to C++ Coding Track

C++ Coding Track · Lesson 9

Control Panel Start & Stop Buttons

This lesson reads the Start and Stop buttons on the control panel. The code counts down from 10 to 0 when the Start button is pressed, and can be stopped at any time with the Stop button, which resets the countdown back to 10 seconds.

Wiring. The buttons can only connect to the FlexDIO pins. Connect the NC terminal to signal — but for the Emergency Stop, connect to NO instead — and the COM terminal to negative. You can join all the negative wires together and run them to a single negative pin.

Constant.h

#pragma once
#define _USE_MATH_DEFINES
#include <math.h>
// Defines a namespace to hold constant values
namespace constant
{
  static constexpr int START_BUTTON = 8;
  static constexpr int STOP_BUTTON = 11;
}

Robot.h

#pragma once
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc/DigitalInput.h>
#include <frc/Timer.h>
#include "RobotContainer.h"
#include "Constants.h"
class Robot : public frc::TimedRobot {
public:
  void RobotInit() override;
  void RobotPeriodic() override;
  void DisabledInit() override;
  void DisabledPeriodic() override;
  void AutonomousInit() override;
  void AutonomousPeriodic() override;
  void TeleopInit() override;
  void TeleopPeriodic() override;
  void TestPeriodic() override;
private:
  frc::DigitalInput startButton{constant::START_BUTTON};
  frc::DigitalInput stopButton{constant::STOP_BUTTON};
  frc::Timer countdownTimer;
  bool countdownActive = false;
  bool startWasPressed = false;
  bool stopWasPressed = false;
  const double countdownLength = 10.0;
};

Robot.cpp

#include "Robot.h"
#include <frc/smartdashboard/SmartDashboard.h>
#include <cmath>
void Robot::RobotInit()
{
  countdownActive = false;
  countdownTimer.Stop();
  countdownTimer.Reset();
  frc::SmartDashboard::PutNumber("Countdown", 10);
  frc::SmartDashboard::PutString("Countdown Status", "Waiting");
}
void Robot::RobotPeriodic()
{
  bool startPressed = !startButton.Get();
  bool stopPressed = !stopButton.Get();
  if (startPressed && !startWasPressed && !countdownActive)
  {
    countdownActive = true;
    countdownTimer.Reset();
    countdownTimer.Start();
    frc::SmartDashboard::PutString("Countdown Status", "Running");
  }
  if (stopPressed && !stopWasPressed)
  {
    countdownActive = false;
    countdownTimer.Stop();
    countdownTimer.Reset();
    frc::SmartDashboard::PutNumber("Countdown", 10);
    frc::SmartDashboard::PutString("Countdown Status", "Stopped");
  }
  if (countdownActive)
  {
    double timeLeft = countdownLength - countdownTimer.Get();
    if (timeLeft > 0)
    {
      frc::SmartDashboard::PutNumber("Countdown", std::ceil(timeLeft));
    }
    else
    {
      countdownActive = false;
      countdownTimer.Stop();
      frc::SmartDashboard::PutNumber("Countdown", 0);
      frc::SmartDashboard::PutString("Countdown Status", "Finished");
    }
  }
  startWasPressed = startPressed;
  stopWasPressed = stopPressed;
}
void Robot::DisabledInit()
{
  countdownActive = false;
  countdownTimer.Stop();
  countdownTimer.Reset();
  frc::SmartDashboard::PutNumber("Countdown", 10);
  frc::SmartDashboard::PutString("Countdown Status", "Disabled");
}
void Robot::DisabledPeriodic() {}
void Robot::AutonomousInit()
{
  countdownActive = false;
  countdownTimer.Stop();
  countdownTimer.Reset();
  frc::SmartDashboard::PutNumber("Countdown", 10);
  frc::SmartDashboard::PutString("Countdown Status", "Ready");
}
void Robot::AutonomousPeriodic() {}
void Robot::TeleopInit()
{
  countdownActive = false;
  countdownTimer.Stop();
  countdownTimer.Reset();
  frc::SmartDashboard::PutNumber("Countdown", 10);
  frc::SmartDashboard::PutString("Countdown Status", "Ready");
}
void Robot::TeleopPeriodic() {}
void Robot::TestPeriodic() {}
#ifndef RUNNING_FRC_TESTS
int main() { return frc::StartRobot<Robot>(); }
#endif
Challenge. Pair this with the LEDs: make the green LED turn on only when Start is pressed, and the red LED turn on when Stop is pressed, when the timer runs out, and before Start has been pressed. Then try pairing the buttons with the four-wheel robot so it only starts moving when Start is pressed.
← Control Panel LEDs Simple Servo Movement →