We'll start with the LED code. When the robot is in Disabled mode, the red LED is on. Once the robot is Enabled, the red LED switches off and the green LED turns on.
Remember the buttons have built-in LEDs. A useful tip: connect the green LED wire to the Start button LED wire, and the red LED wire to the Stop button LED wire, so they share the same pins.
Constant.h
#pragma once
#define _USE_MATH_DEFINES
#include <math.h>
namespace constant
{
// Outputs from the working robot
static constexpr int RUNNING_LED = 19;
static constexpr int STOPPED_LED = 21;
}
Robot.h
#pragma once
#include <frc/TimedRobot.h>
#include <frc2/command/Command.h>
#include <frc/DigitalOutput.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;
void RunningLED();
private:
frc::DigitalOutput runningLED{constant::RUNNING_LED};
frc::DigitalOutput stoppedLED{constant::STOPPED_LED};
int count = 1;
bool prev = true;
};
Robot.cpp
#include "Robot.h"
#include <frc/smartdashboard/SmartDashboard.h>
#include <frc2/command/CommandScheduler.h>
void Robot::RobotInit()
{
runningLED.Set(false);
stoppedLED.Set(false);
count = 1;
prev = true;
}
void Robot::RobotPeriodic()
{
frc2::CommandScheduler::GetInstance().Run();
frc::SmartDashboard::PutBoolean("Running LED", runningLED.Get());
frc::SmartDashboard::PutBoolean("Stopped LED", stoppedLED.Get());
}
void Robot::DisabledInit()
{
stoppedLED.Set(true);
runningLED.Set(false);
}
void Robot::DisabledPeriodic()
{
}
void Robot::AutonomousInit()
{
stoppedLED.Set(false);
runningLED.Set(false);
count = 1;
prev = true;
}
void Robot::AutonomousPeriodic()
{
RunningLED();
}
void Robot::TeleopInit()
{
stoppedLED.Set(false);
runningLED.Set(false);
count = 1;
prev = true;
}
void Robot::TeleopPeriodic()
{
RunningLED();
}
void Robot::TestPeriodic() {}
void Robot::RunningLED()
{
if ((count % 25) == 0)
{
if (prev)
{
runningLED.Set(false);
prev = false;
}
else
{
runningLED.Set(true);
prev = true;
}
count = 1;
}
else
{
count++;
}
}
#ifndef RUNNING_FRC_TESTS
int main() { return frc::StartRobot<Robot>(); }
#endif
Challenge. Pair this LED code with the encoder challenge where you made a four-wheel robot move forward a set distance. Make the red LED switch on and the green LED switch off when the robot reaches its target distance — indicating the code has finished running.
I suggest using the control panel in every challenge from this point forward. Add it wherever you can — it's one of the most important parts of robotics.