Code
Using our state machine as our primary framework, this demonstrates the fully integrated code used in the final competition.
#include <Arduino.h>
#include <Metro.h>
// IR Pins defined
#define CENTER_IR_PIN 17
#define LEFT_IR_PIN 18
#define RIGHT_IR_PIN 16
#define FRONT_RIGHT_IR_PIN 19
#define FRONT_LEFT_IR_PIN 14
#define BALL_IR_PIN 23
// Team Color Pins
#define COLOR_PIN 0
int red = 1;
int blue = 0;
int color = red; // initialized as red team
// RGB Pins defined
#define RGB_RED_PIN 7
#define RGB_GREEN_PIN 8
#define RGB_BLUE_PIN 9
// colors defined
#define RGB_Red 255, 0, 0
#define RGB_Blue 0, 0, 255
#define RGB_Green 0, 255, 0
#define RGB_Thistle 216, 191, 216
#define RGB_Orange 255, 215, 0
#define RGB_Yellow 255, 255, 25
#define RGB_Cyan 70, 240, 240
#define RGB_Brown 170, 110, 40
#define RGB_Maroon 128, 0, 0
#define RGB_Pink 250, 190, 212
#define RGB_White 255, 255, 255
// Timers
#define GAMETIME 130000 //2min 10 sec
static Metro gameTimer = Metro(GAMETIME);
#define OneSheepTime 15000
static Metro OneSheepTimer = Metro(OneSheepTime);
#define TurnTime 2000
static Metro TurnTimer = Metro(TurnTime);
#define Forward_Drive_Time 4000
static Metro ForwardTimer = Metro(Forward_Drive_Time);
#define ScoringTime 4000 //set this to make sure we drive past the junction
static Metro Past_junction_Score = Metro(ScoringTime);
#define getsheeptime 8000
static Metro getsheeptimer = Metro(getsheeptime);
// IR variables
volatile int centerIR;
volatile int leftIR;
volatile int rightIR;
volatile int frontLeftIR;
volatile int frontRightIR;
volatile int sensorsReading = 1;
volatile int sheepcollected = 0;
int counter = 0;
// Thresholds and Counters
int lineSensed = 0;
int turnsMade = 0;
int sheepSensed = 0;
int ultraDetected = 6;
int linesensedfront = 0;
// Motor Pins
#define EN_A_PIN 4
#define DIR_A1_PIN 5
#define DIR_A2_PIN 6
#define EN_B_PIN 3
#define DIR_B3_PIN 1
#define DIR_B4_PIN 2
#define EN_C_PIN 20
#define DIR_C1_PIN 21
#define DIR_C2_PIN 22
// Speed of wheels
int SPEED = 255;
int motorMultiplier = 100;
int SPEEDA = (SPEED * motorMultiplier) / 100; // speed adjuster
// Wheel Functions
void fullSpeedBackwards(void);
void AWheelsBackward(void);
void BWheelsBackward(void);
void AWheelsHalfBackward(void);
void BWheelsHalfBackward(void);
void fullSpeedForwards(void);
void AWheelsForward(void);
void BWheelsForward(void);
void stopAllWheels(void);
// Navigating functions
void checkSensors(void);
void lineFollowing(void);
void motorCompTester(void);
void GrabOneSheep(void);
void collectSheep(void);
void releaseSheep(void);
void checkForSheep(void);
void intakeOFF(void);
void turnRight(void);
void turnLeft(void);
void orientingTowardsLine(void);
void detectteam(void);
void ReturnToLZ(void);
void RGB_color(int red_light_value, int green_light_value, int blue_light_value);
void TurningForLineFollowing(void);
void orientingTowardsLineBlue(void);
void lineFollowingBlue(void);
void TurningForLineFollowingBlue(void);
// state definitions
typedef enum
{
STATE_GET_MEGADUCK,
STATE_RETURN_TO_LZ,
STATE_LZ_TURN,
STATE_ORIENTING_TOWARD_LINE,
STATE_FOLLOWING_LINE,
STATE_SCORING,
STATE_DRIVE_STRAIGHT,
STATE_DRIVE_STRAIGHT_SENSING,
STATE_JUNCTION_ORIENT,
STATE_PREPARE_FOR_LINE_FOLLOWING,
STATE_END_GAME
} States_t;
// States
States_t state; // just declaring
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(CENTER_IR_PIN, INPUT);
pinMode(LEFT_IR_PIN, INPUT);
pinMode(RIGHT_IR_PIN, INPUT);
pinMode(FRONT_LEFT_IR_PIN, INPUT);
pinMode(FRONT_RIGHT_IR_PIN, INPUT);
pinMode(BALL_IR_PIN, INPUT);
pinMode(EN_A_PIN, OUTPUT);
pinMode(EN_B_PIN, OUTPUT);
pinMode(EN_C_PIN, OUTPUT);
pinMode(DIR_A1_PIN, OUTPUT);
pinMode(DIR_A2_PIN, OUTPUT);
pinMode(DIR_B3_PIN, OUTPUT);
pinMode(DIR_B4_PIN, OUTPUT);
pinMode(DIR_C1_PIN, OUTPUT);
pinMode(DIR_C2_PIN, OUTPUT);
pinMode(COLOR_PIN, INPUT);
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
detectteam();
state = STATE_GET_MEGADUCK; // initialiing starting state
lineSensed = analogRead(CENTER_IR_PIN) - 40; //setting IR thresholds at the start
sheepSensed = analogRead(BALL_IR_PIN) + 40;
linesensedfront = analogRead(FRONT_RIGHT_IR_PIN) - 20;
gameTimer.reset();
}
void loop() {
checkSensors();
switch (state)
{
case STATE_GET_MEGADUCK:
RGB_color(RGB_White);
GrabOneSheep();
break;
case STATE_RETURN_TO_LZ:
RGB_color(RGB_Blue);
ReturnToLZ();
TurnTimer.reset();
break;
case STATE_LZ_TURN:
RGB_color(RGB_Green);
SPEEDA = 93;
SPEED = 100;
if (color == red)
{
turnRight();
}
else if (color == blue)
{
turnLeft();
}
ForwardTimer.reset();
break;
case STATE_DRIVE_STRAIGHT:
RGB_color(RGB_Red);
fullSpeedForwards();
if (ForwardTimer.check())
{
state = STATE_ORIENTING_TOWARD_LINE;
}
break;
case STATE_ORIENTING_TOWARD_LINE:
RGB_color(RGB_Blue);
if (color == red)
{
orientingTowardsLine();
}
else if (color == blue)
{
orientingTowardsLineBlue();
}
break;
case STATE_PREPARE_FOR_LINE_FOLLOWING:
RGB_color(RGB_Thistle);
if (color == red)
{
TurningForLineFollowing();
}
else if (color == blue)
{
TurningForLineFollowingBlue();
}
Past_junction_Score.reset();
break;
case STATE_FOLLOWING_LINE:
RGB_color(RGB_White);
if (color == red)
{
lineFollowing();
}
else if (color == blue)
{
lineFollowingBlue();
}
break;
case STATE_SCORING:
RGB_color(RGB_Red);
stopAllWheels();
releaseSheep();
break;
case STATE_END_GAME:
RGB_color(RGB_Pink);
stopAllWheels();
break;
default: // Should never get into an unhandled state
Serial.println("What is this I do not even...");
}
}
void motorCompTester(void)
{
analogWrite(EN_A_PIN, SPEED);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void fullSpeedForwards(void) {
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, LOW);
digitalWrite(DIR_A2_PIN, HIGH);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, HIGH);
digitalWrite(DIR_B4_PIN, LOW);
}
void AWheelsForward(void)
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, LOW);
digitalWrite(DIR_A2_PIN, HIGH);
}
void BWheelsForward(void)
{
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, HIGH);
digitalWrite(DIR_B4_PIN, LOW);
}
void fullSpeedBackwards(void)
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void AWheelsBackward(void)
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
}
void BWheelsBackward(void)
{
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void AWheelsHalfBackward(void)
{
analogWrite(EN_A_PIN, SPEEDA / 2);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
}
void BWheelsHalfBackward(void)
{
analogWrite(EN_B_PIN, SPEED / 2);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void stopAllWheels(void)
{
analogWrite(EN_A_PIN, 0);
analogWrite(EN_B_PIN, 0);
}
void turnRight(void)
{
if (!TurnTimer.check())
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, LOW);
digitalWrite(DIR_A2_PIN, HIGH);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
else
{
state = STATE_DRIVE_STRAIGHT;
}
}
void turnLeft(void)
{
if (!TurnTimer.check())
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, HIGH);
digitalWrite(DIR_B4_PIN, LOW);
}
else
{
state = STATE_DRIVE_STRAIGHT;
}
}
void checkSensors(void)
{
if (gameTimer.check())
{
state = STATE_END_GAME;
}
if (analogRead(CENTER_IR_PIN) > lineSensed)
{
centerIR = LOW;
}
else
centerIR = HIGH;
if (analogRead(LEFT_IR_PIN) > lineSensed)
{
leftIR = LOW;
}
else
leftIR = HIGH;
if (analogRead(RIGHT_IR_PIN) > lineSensed)
{
rightIR = LOW;
}
else
rightIR = HIGH;
if (analogRead(FRONT_RIGHT_IR_PIN) > linesensedfront)
{
frontRightIR = LOW;
}
else
frontRightIR = HIGH;
if (analogRead(FRONT_LEFT_IR_PIN) > linesensedfront)
{
frontLeftIR = LOW;
}
else
frontLeftIR = HIGH;
}
void lineFollowing(void)
{
if (rightIR && !centerIR)
{
stopAllWheels();
AWheelsForward(); // turn right
}
else if (centerIR && !rightIR)
{
stopAllWheels();
BWheelsForward(); // turn left
}
else if (centerIR && rightIR && !leftIR)
{
stopAllWheels();
fullSpeedForwards();
}
else if (leftIR)
{
stopAllWheels();
AWheelsForward();
}
else if (frontLeftIR && Past_junction_Score.check())
{
state = STATE_SCORING;
}
}
void lineFollowingBlue(void)
{
if (leftIR && !centerIR)
{
stopAllWheels();
BWheelsForward(); // turn left
}
else if (centerIR && !leftIR)
{
stopAllWheels();
AWheelsForward(); // turn right
}
else if (centerIR && leftIR && !rightIR)
{
stopAllWheels();
fullSpeedForwards();
}
else if (rightIR)
{
stopAllWheels();
BWheelsForward();
}
else if (frontRightIR && Past_junction_Score.check())
{
state = STATE_SCORING;
}
}
void GrabOneSheep()
{
if (sheepcollected == 0)
{
fullSpeedForwards();
collectSheep();
checkForSheep();
}
else
{
intakeOFF();
state = STATE_RETURN_TO_LZ;
}
}
void collectSheep()
{
analogWrite(EN_C_PIN, 255);
digitalWrite(DIR_C1_PIN, LOW);
digitalWrite(DIR_C2_PIN, HIGH);
}
void releaseSheep()
{
analogWrite(EN_C_PIN, 255);
digitalWrite(DIR_C1_PIN, HIGH);
digitalWrite(DIR_C2_PIN, LOW);
}
void checkForSheep()
{
if (analogRead(BALL_IR_PIN) > sheepSensed)
{
sheepcollected = 1;
}
// Serial.println(analogRead(BALL_IR_PIN));
}
void intakeOFF()
{
analogWrite(EN_C_PIN, 0);
}
void ReturnToLZ(void)
{
if (!frontRightIR && !frontLeftIR)
{
stopAllWheels();
fullSpeedBackwards();
}
else if (frontLeftIR || frontRightIR)
{
stopAllWheels();
state = STATE_LZ_TURN;
}
}
void orientingTowardsLine(void)
{
if (!frontLeftIR)
{
fullSpeedForwards();
}
else if (frontLeftIR)
{
fullSpeedForwards();
delay(800);
state = STATE_PREPARE_FOR_LINE_FOLLOWING;
}
}
void orientingTowardsLineBlue(void)
{
if (!frontRightIR)
{
fullSpeedForwards();
}
else if (frontRightIR)
{
fullSpeedForwards();
delay(800);
state = STATE_PREPARE_FOR_LINE_FOLLOWING;
}
}
void TurningForLineFollowing(void)
{
if (centerIR)
{
state = STATE_FOLLOWING_LINE;
}
else if (!rightIR)
{
AWheelsBackward();
BWheelsForward();
}
}
void TurningForLineFollowingBlue(void)
{
if (centerIR)
{
state = STATE_FOLLOWING_LINE;
}
else if (!leftIR)
{
AWheelsForward();
BWheelsBackward();
}
}
void detectteam(void)
{
if (digitalRead(COLOR_PIN) == 1)
{
color = red;
}
else if (digitalRead(COLOR_PIN) == 0)
{
color = blue;
}
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(RGB_RED_PIN, red_light_value);
analogWrite(RGB_GREEN_PIN, green_light_value);
analogWrite(RGB_BLUE_PIN, blue_light_value);
}
#include <Metro.h>
// IR Pins defined
#define CENTER_IR_PIN 17
#define LEFT_IR_PIN 18
#define RIGHT_IR_PIN 16
#define FRONT_RIGHT_IR_PIN 19
#define FRONT_LEFT_IR_PIN 14
#define BALL_IR_PIN 23
// Team Color Pins
#define COLOR_PIN 0
int red = 1;
int blue = 0;
int color = red; // initialized as red team
// RGB Pins defined
#define RGB_RED_PIN 7
#define RGB_GREEN_PIN 8
#define RGB_BLUE_PIN 9
// colors defined
#define RGB_Red 255, 0, 0
#define RGB_Blue 0, 0, 255
#define RGB_Green 0, 255, 0
#define RGB_Thistle 216, 191, 216
#define RGB_Orange 255, 215, 0
#define RGB_Yellow 255, 255, 25
#define RGB_Cyan 70, 240, 240
#define RGB_Brown 170, 110, 40
#define RGB_Maroon 128, 0, 0
#define RGB_Pink 250, 190, 212
#define RGB_White 255, 255, 255
// Timers
#define GAMETIME 130000 //2min 10 sec
static Metro gameTimer = Metro(GAMETIME);
#define OneSheepTime 15000
static Metro OneSheepTimer = Metro(OneSheepTime);
#define TurnTime 2000
static Metro TurnTimer = Metro(TurnTime);
#define Forward_Drive_Time 4000
static Metro ForwardTimer = Metro(Forward_Drive_Time);
#define ScoringTime 4000 //set this to make sure we drive past the junction
static Metro Past_junction_Score = Metro(ScoringTime);
#define getsheeptime 8000
static Metro getsheeptimer = Metro(getsheeptime);
// IR variables
volatile int centerIR;
volatile int leftIR;
volatile int rightIR;
volatile int frontLeftIR;
volatile int frontRightIR;
volatile int sensorsReading = 1;
volatile int sheepcollected = 0;
int counter = 0;
// Thresholds and Counters
int lineSensed = 0;
int turnsMade = 0;
int sheepSensed = 0;
int ultraDetected = 6;
int linesensedfront = 0;
// Motor Pins
#define EN_A_PIN 4
#define DIR_A1_PIN 5
#define DIR_A2_PIN 6
#define EN_B_PIN 3
#define DIR_B3_PIN 1
#define DIR_B4_PIN 2
#define EN_C_PIN 20
#define DIR_C1_PIN 21
#define DIR_C2_PIN 22
// Speed of wheels
int SPEED = 255;
int motorMultiplier = 100;
int SPEEDA = (SPEED * motorMultiplier) / 100; // speed adjuster
// Wheel Functions
void fullSpeedBackwards(void);
void AWheelsBackward(void);
void BWheelsBackward(void);
void AWheelsHalfBackward(void);
void BWheelsHalfBackward(void);
void fullSpeedForwards(void);
void AWheelsForward(void);
void BWheelsForward(void);
void stopAllWheels(void);
// Navigating functions
void checkSensors(void);
void lineFollowing(void);
void motorCompTester(void);
void GrabOneSheep(void);
void collectSheep(void);
void releaseSheep(void);
void checkForSheep(void);
void intakeOFF(void);
void turnRight(void);
void turnLeft(void);
void orientingTowardsLine(void);
void detectteam(void);
void ReturnToLZ(void);
void RGB_color(int red_light_value, int green_light_value, int blue_light_value);
void TurningForLineFollowing(void);
void orientingTowardsLineBlue(void);
void lineFollowingBlue(void);
void TurningForLineFollowingBlue(void);
// state definitions
typedef enum
{
STATE_GET_MEGADUCK,
STATE_RETURN_TO_LZ,
STATE_LZ_TURN,
STATE_ORIENTING_TOWARD_LINE,
STATE_FOLLOWING_LINE,
STATE_SCORING,
STATE_DRIVE_STRAIGHT,
STATE_DRIVE_STRAIGHT_SENSING,
STATE_JUNCTION_ORIENT,
STATE_PREPARE_FOR_LINE_FOLLOWING,
STATE_END_GAME
} States_t;
// States
States_t state; // just declaring
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(CENTER_IR_PIN, INPUT);
pinMode(LEFT_IR_PIN, INPUT);
pinMode(RIGHT_IR_PIN, INPUT);
pinMode(FRONT_LEFT_IR_PIN, INPUT);
pinMode(FRONT_RIGHT_IR_PIN, INPUT);
pinMode(BALL_IR_PIN, INPUT);
pinMode(EN_A_PIN, OUTPUT);
pinMode(EN_B_PIN, OUTPUT);
pinMode(EN_C_PIN, OUTPUT);
pinMode(DIR_A1_PIN, OUTPUT);
pinMode(DIR_A2_PIN, OUTPUT);
pinMode(DIR_B3_PIN, OUTPUT);
pinMode(DIR_B4_PIN, OUTPUT);
pinMode(DIR_C1_PIN, OUTPUT);
pinMode(DIR_C2_PIN, OUTPUT);
pinMode(COLOR_PIN, INPUT);
pinMode(RGB_RED_PIN, OUTPUT);
pinMode(RGB_GREEN_PIN, OUTPUT);
pinMode(RGB_BLUE_PIN, OUTPUT);
detectteam();
state = STATE_GET_MEGADUCK; // initialiing starting state
lineSensed = analogRead(CENTER_IR_PIN) - 40; //setting IR thresholds at the start
sheepSensed = analogRead(BALL_IR_PIN) + 40;
linesensedfront = analogRead(FRONT_RIGHT_IR_PIN) - 20;
gameTimer.reset();
}
void loop() {
checkSensors();
switch (state)
{
case STATE_GET_MEGADUCK:
RGB_color(RGB_White);
GrabOneSheep();
break;
case STATE_RETURN_TO_LZ:
RGB_color(RGB_Blue);
ReturnToLZ();
TurnTimer.reset();
break;
case STATE_LZ_TURN:
RGB_color(RGB_Green);
SPEEDA = 93;
SPEED = 100;
if (color == red)
{
turnRight();
}
else if (color == blue)
{
turnLeft();
}
ForwardTimer.reset();
break;
case STATE_DRIVE_STRAIGHT:
RGB_color(RGB_Red);
fullSpeedForwards();
if (ForwardTimer.check())
{
state = STATE_ORIENTING_TOWARD_LINE;
}
break;
case STATE_ORIENTING_TOWARD_LINE:
RGB_color(RGB_Blue);
if (color == red)
{
orientingTowardsLine();
}
else if (color == blue)
{
orientingTowardsLineBlue();
}
break;
case STATE_PREPARE_FOR_LINE_FOLLOWING:
RGB_color(RGB_Thistle);
if (color == red)
{
TurningForLineFollowing();
}
else if (color == blue)
{
TurningForLineFollowingBlue();
}
Past_junction_Score.reset();
break;
case STATE_FOLLOWING_LINE:
RGB_color(RGB_White);
if (color == red)
{
lineFollowing();
}
else if (color == blue)
{
lineFollowingBlue();
}
break;
case STATE_SCORING:
RGB_color(RGB_Red);
stopAllWheels();
releaseSheep();
break;
case STATE_END_GAME:
RGB_color(RGB_Pink);
stopAllWheels();
break;
default: // Should never get into an unhandled state
Serial.println("What is this I do not even...");
}
}
void motorCompTester(void)
{
analogWrite(EN_A_PIN, SPEED);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void fullSpeedForwards(void) {
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, LOW);
digitalWrite(DIR_A2_PIN, HIGH);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, HIGH);
digitalWrite(DIR_B4_PIN, LOW);
}
void AWheelsForward(void)
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, LOW);
digitalWrite(DIR_A2_PIN, HIGH);
}
void BWheelsForward(void)
{
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, HIGH);
digitalWrite(DIR_B4_PIN, LOW);
}
void fullSpeedBackwards(void)
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void AWheelsBackward(void)
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
}
void BWheelsBackward(void)
{
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void AWheelsHalfBackward(void)
{
analogWrite(EN_A_PIN, SPEEDA / 2);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
}
void BWheelsHalfBackward(void)
{
analogWrite(EN_B_PIN, SPEED / 2);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
void stopAllWheels(void)
{
analogWrite(EN_A_PIN, 0);
analogWrite(EN_B_PIN, 0);
}
void turnRight(void)
{
if (!TurnTimer.check())
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, LOW);
digitalWrite(DIR_A2_PIN, HIGH);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, LOW);
digitalWrite(DIR_B4_PIN, HIGH);
}
else
{
state = STATE_DRIVE_STRAIGHT;
}
}
void turnLeft(void)
{
if (!TurnTimer.check())
{
analogWrite(EN_A_PIN, SPEEDA);
digitalWrite(DIR_A1_PIN, HIGH);
digitalWrite(DIR_A2_PIN, LOW);
analogWrite(EN_B_PIN, SPEED);
digitalWrite(DIR_B3_PIN, HIGH);
digitalWrite(DIR_B4_PIN, LOW);
}
else
{
state = STATE_DRIVE_STRAIGHT;
}
}
void checkSensors(void)
{
if (gameTimer.check())
{
state = STATE_END_GAME;
}
if (analogRead(CENTER_IR_PIN) > lineSensed)
{
centerIR = LOW;
}
else
centerIR = HIGH;
if (analogRead(LEFT_IR_PIN) > lineSensed)
{
leftIR = LOW;
}
else
leftIR = HIGH;
if (analogRead(RIGHT_IR_PIN) > lineSensed)
{
rightIR = LOW;
}
else
rightIR = HIGH;
if (analogRead(FRONT_RIGHT_IR_PIN) > linesensedfront)
{
frontRightIR = LOW;
}
else
frontRightIR = HIGH;
if (analogRead(FRONT_LEFT_IR_PIN) > linesensedfront)
{
frontLeftIR = LOW;
}
else
frontLeftIR = HIGH;
}
void lineFollowing(void)
{
if (rightIR && !centerIR)
{
stopAllWheels();
AWheelsForward(); // turn right
}
else if (centerIR && !rightIR)
{
stopAllWheels();
BWheelsForward(); // turn left
}
else if (centerIR && rightIR && !leftIR)
{
stopAllWheels();
fullSpeedForwards();
}
else if (leftIR)
{
stopAllWheels();
AWheelsForward();
}
else if (frontLeftIR && Past_junction_Score.check())
{
state = STATE_SCORING;
}
}
void lineFollowingBlue(void)
{
if (leftIR && !centerIR)
{
stopAllWheels();
BWheelsForward(); // turn left
}
else if (centerIR && !leftIR)
{
stopAllWheels();
AWheelsForward(); // turn right
}
else if (centerIR && leftIR && !rightIR)
{
stopAllWheels();
fullSpeedForwards();
}
else if (rightIR)
{
stopAllWheels();
BWheelsForward();
}
else if (frontRightIR && Past_junction_Score.check())
{
state = STATE_SCORING;
}
}
void GrabOneSheep()
{
if (sheepcollected == 0)
{
fullSpeedForwards();
collectSheep();
checkForSheep();
}
else
{
intakeOFF();
state = STATE_RETURN_TO_LZ;
}
}
void collectSheep()
{
analogWrite(EN_C_PIN, 255);
digitalWrite(DIR_C1_PIN, LOW);
digitalWrite(DIR_C2_PIN, HIGH);
}
void releaseSheep()
{
analogWrite(EN_C_PIN, 255);
digitalWrite(DIR_C1_PIN, HIGH);
digitalWrite(DIR_C2_PIN, LOW);
}
void checkForSheep()
{
if (analogRead(BALL_IR_PIN) > sheepSensed)
{
sheepcollected = 1;
}
// Serial.println(analogRead(BALL_IR_PIN));
}
void intakeOFF()
{
analogWrite(EN_C_PIN, 0);
}
void ReturnToLZ(void)
{
if (!frontRightIR && !frontLeftIR)
{
stopAllWheels();
fullSpeedBackwards();
}
else if (frontLeftIR || frontRightIR)
{
stopAllWheels();
state = STATE_LZ_TURN;
}
}
void orientingTowardsLine(void)
{
if (!frontLeftIR)
{
fullSpeedForwards();
}
else if (frontLeftIR)
{
fullSpeedForwards();
delay(800);
state = STATE_PREPARE_FOR_LINE_FOLLOWING;
}
}
void orientingTowardsLineBlue(void)
{
if (!frontRightIR)
{
fullSpeedForwards();
}
else if (frontRightIR)
{
fullSpeedForwards();
delay(800);
state = STATE_PREPARE_FOR_LINE_FOLLOWING;
}
}
void TurningForLineFollowing(void)
{
if (centerIR)
{
state = STATE_FOLLOWING_LINE;
}
else if (!rightIR)
{
AWheelsBackward();
BWheelsForward();
}
}
void TurningForLineFollowingBlue(void)
{
if (centerIR)
{
state = STATE_FOLLOWING_LINE;
}
else if (!leftIR)
{
AWheelsForward();
BWheelsBackward();
}
}
void detectteam(void)
{
if (digitalRead(COLOR_PIN) == 1)
{
color = red;
}
else if (digitalRead(COLOR_PIN) == 0)
{
color = blue;
}
}
void RGB_color(int red_light_value, int green_light_value, int blue_light_value)
{
analogWrite(RGB_RED_PIN, red_light_value);
analogWrite(RGB_GREEN_PIN, green_light_value);
analogWrite(RGB_BLUE_PIN, blue_light_value);
}