🤖 VEX V5 Programming Practice

Master Variables, Conditionals, Logic Operators & Sensors

📊 Flowcharts

What is a Flowchart?

A flowchart is a visual diagram that shows the steps in a program from start to finish. It helps you plan your robot's behavior before you write any code!

Why use flowcharts?

  • Plan your program logic before coding
  • See the flow of decisions and actions clearly
  • Find problems in your logic early
  • Communicate your ideas to teammates
  • Debug programs more easily

Flowchart Shapes for VEX Programming

Start Program

Square

Start/Stop Program

Action Block

Rectangle

Action/Process

IF/THEN

Up Triangle

Conditional (IF)

END

Down Triangle

End Conditional

Forever Loop

Magenta Rectangle

Forever Loop

Condition True

Green Rectangle

TRUE Path

ELSE

Orange Rectangle

FALSE Path (ELSE)

💡 Flow Direction: Arrows connect shapes showing the order of operations. Programs flow from top to bottom. Conditionals split into two paths (YES/NO or TRUE/FALSE) and then rejoin at the END IF triangle.

Flowchart Examples

Example 1: Simple Forward Drive

Task: Robot drives forward for 3 rotations then stops.

Flowchart Example 1

Example 2: Bumper Stop Program

Task: Drive forward until bumper is pressed, then stop.

Flowchart Example 2

Practice Exercise: Create Your Own Flowchart!

Challenge: Draw a flowchart for this program:

START Read Vision Sensor IF red object detected THEN Turn Left 90° ELSE IF blue object detected THEN Turn Right 90° ELSE Drive Forward 1 rotation END Stop Motors END

Shapes you'll need:

  • 1 Green Square (START)
  • 1 Blue Rectangle (Read Sensor)
  • 2 Orange Up Triangles (IF conditions)
  • 3 Blue Rectangles (Actions: Turn Left, Turn Right, Drive Forward)
  • 1 Blue Rectangle (Stop Motors)
  • 2 Purple Down Triangles (END IF)
  • 1 Red Square (STOP)

Tip: Draw it on paper first! Remember to label YES/NO paths from conditionals.

🎯 Flowchart Best Practices:
  • Always start with a START shape and end with a STOP shape
  • Use arrows to show the direction of flow
  • Label conditional branches clearly (YES/NO or TRUE/FALSE)
  • Every UP triangle (IF) needs a matching DOWN triangle (END IF)
  • Keep it neat and organized - easier to understand!
  • Test your flowchart by following the arrows step-by-step

📊 Variables

What are Variables?

Variables are containers that store information in your program. Think of them like labeled boxes where you can keep numbers, text, or true/false values.

Types of Variables:

  • Number: Stores numeric values (e.g., distance, speed, counter)
  • Boolean: Stores true/false values (e.g., is robot moving?, is sensor triggered?)
  • String: Stores text (e.g., "Forward", "Stopped")

Practice Example 1: Counter Variable

Challenge: Create a program that counts how many times the robot moves forward.

Blocks you'll need:

  • Set variable "moveCount" to 0
  • When button pressed
  • Drive forward for 1 rotation
  • Change "moveCount" by 1
  • Print "moveCount" to brain screen

Goal: Every time you press a button, the robot moves forward and displays the total number of moves.

Practice Example 2: Speed Control Variable

Challenge: Use a variable to control robot speed that changes based on conditions.

Steps:

  1. Create a variable called "robotSpeed" and set it to 50
  2. Drive forward at "robotSpeed" percent velocity
  3. After 2 seconds, change "robotSpeed" by 25 (now 75%)
  4. Continue driving at new speed
  5. Display speed on brain screen

Practice Example 3: Boolean Flag Variable

Challenge: Use a boolean variable to track if the robot is in "fast mode" or "slow mode".

Logic:

  • Create boolean variable "fastMode" set to false
  • If button A pressed, set "fastMode" to true, drive at 100% speed
  • If button B pressed, set "fastMode" to false, drive at 30% speed
  • Print current mode to screen

🔀 Conditional Statements

What are Conditionals?

Conditional statements let your robot make decisions based on conditions. They follow the pattern: "IF this is true, THEN do this, ELSE do something else."

Block Types:

  • IF-THEN: Execute code only if condition is true
  • IF-THEN-ELSE: Execute one block if true, another if false
  • IF-THEN-ELSE IF-THEN-ELSE: Check multiple conditions in order
  • WAIT UNTIL: Pause program until a condition becomes true
  • REPEAT UNTIL: Keep repeating actions until a condition becomes true
  • REPEAT (x): Repeat actions a specific number of times
  • FOREVER: Repeat actions continuously without stopping
  • WHILE: Repeat actions as long as a condition remains true

Practice Example 1: Temperature Check

Challenge: Display different messages based on motor temperature.

Pseudocode:

IF motor temperature > 50°C THEN Print "Motor is hot!" Stop motors ELSE Print "Temperature OK" Continue driving END

Test it: Run motors for a while and see the message change.

Practice Example 2: Multi-Speed Selector

Challenge: Use IF-THEN-ELSE IF-THEN-ELSE to set different speeds based on button presses.

Logic:

IF button A pressed THEN Set speed to 25% (Slow) ELSE IF button B pressed THEN Set speed to 50% (Medium) ELSE IF button C pressed THEN Set speed to 100% (Fast) ELSE Stop motors END

Practice Example 3: Battery Level Warning

Challenge: Check battery percentage and respond appropriately.

Logic:

  • IF battery > 75% THEN: Print "Battery Good" in green
  • ELSE IF battery > 25% THEN: Print "Battery OK" in yellow
  • ELSE: Print "Charge Soon!" in red and stop robot

🔗 Logic Operators

What are Logic Operators?

Logic operators combine multiple conditions to make complex decisions.

Operators:

  • AND: Both conditions must be true
  • OR: At least one condition must be true
  • NOT: Inverts the condition (true becomes false)

Comparison Operators: = (equal), ≠ (not equal), > (greater), < (less), ≥ (greater or equal), ≤ (less or equal)

Practice Example 1: AND Operator - Safe to Move

Challenge: Robot should only move if BOTH battery is good AND temperature is OK.

Logic:

IF (battery > 25%) AND (motor temperature < 50°C) THEN Drive forward Print "Safe to operate" ELSE Stop Print "Not safe - check robot" END

Practice Example 2: OR Operator - Emergency Stop

Challenge: Stop robot if ANY emergency condition is met.

Logic:

IF (button X pressed) OR (battery < 10%) OR (motor temp > 60°C) THEN Stop all motors immediately Print "EMERGENCY STOP" Flash screen red ELSE Continue normal operation END

Practice Example 3: Complex Logic - Smart Navigation

Challenge: Combine multiple operators for intelligent behavior.

Logic:

IF (distance < 200mm) AND (NOT button A pressed) THEN Turn right ELSE IF (distance > 500mm) OR (button A pressed) THEN Drive forward fast ELSE Drive forward slow END

What this does: If something is close AND you're not overriding, turn. If path is clear OR you force it, go fast. Otherwise, go slow.

Practice Example 4: Range Checker

Challenge: Check if a value is within a range.

Logic:

Create variable "speed" = 65 IF (speed >= 50) AND (speed <= 75) THEN Print "Speed in optimal range" ELSE Print "Adjust speed" END

📏 Range Finder (Distance Sensor)

🔧 Hardware Setup

  1. Connect the VEX Distance Sensor to a 3-wire port (e.g., Port A, B, C, etc.)
  2. Ensure the sensor is firmly seated in the port
  3. Mount sensor facing forward on your robot
  4. Keep sensor lens clean for accurate readings

In VEXcode Blocks:

  • Go to Devices menu
  • Add device → Distance Sensor
  • Name it (e.g., "FrontDistance")
  • Select the 3-wire port you used

Sensor Range: Detects objects from ~20mm to 2000mm (2 meters)

How It Works

The Distance Sensor uses ultrasonic waves (sound waves) to measure how far away objects are. It sends out a sound pulse and measures how long it takes to bounce back.

Key Blocks:

  • Distance found by FrontDistance - Returns distance in mm
  • Is object detected by FrontDistance - Returns true/false

Practice Example 1: Object Detection

Challenge: Stop the robot when it gets close to a wall.

Algorithm:

Forever loop: Drive forward at 50% speed IF distance found by FrontDistance < 150mm THEN Stop motors Print "Object detected!" Wait 2 seconds Drive backward for 0.5 rotations

Skills practiced: Reading sensor values, conditionals, robot control

Practice Example 2: Variable Speed Based on Distance

Challenge: Robot slows down as it gets closer to objects.

Algorithm:

Create variable "currentDistance" Forever loop: Set currentDistance = distance found by FrontDistance Print currentDistance to screen IF currentDistance > 500mm THEN Drive at 100% speed ELSE IF currentDistance > 250mm THEN Drive at 50% speed ELSE IF currentDistance > 100mm THEN Drive at 25% speed ELSE Stop Print "Too close!" END

Practice Example 3: Parking Challenge

Challenge: Drive forward and stop exactly 200mm from a wall.

Algorithm:

Drive forward at 30% speed Wait until (distance found by FrontDistance < 220mm) Stop motors Print "Distance: " + distance found by FrontDistance IF (distance > 190mm) AND (distance < 210mm) THEN Print "Perfect park!" ELSE Print "Try again" END

Make it harder: Add points based on accuracy!

💡 Pro Tip: Distance sensors can have trouble with very dark or very shiny surfaces. Test your programs on different materials!

👁️ Vision Sensor

🔧 Hardware Setup

  1. Connect Vision Sensor to a Smart Port (1-21) on the V5 Brain
  2. Smart ports have a different connector than 3-wire ports
  3. Mount sensor with clear field of view
  4. Ensure good lighting in your environment

In VEXcode Blocks:

  • Go to Devices menu
  • Add device → Vision Sensor
  • Name it (e.g., "Vision")
  • Select the Smart Port you used
  • Configure Color Signatures: Use VEX Vision Utility to teach the sensor colors

Setting Up Color Signatures

Before using the Vision Sensor, you need to "teach" it what colors to look for:

  1. Connect brain to computer
  2. Open VEX Vision Utility (in VEXcode)
  3. Point sensor at colored object you want to detect
  4. Click "Capture" to save that color as a signature (SIG_1, SIG_2, etc.)
  5. Give it a name like "RED_CUBE" or "GREEN_BALL"
  6. Download signatures to the sensor

Key Blocks:

  • Take snapshot with Vision.SIG_1 - Look for a specific color
  • Is Vision detecting signature SIG_1 - Returns true/false
  • Vision largest object centerX - X position of detected object
  • Vision largest object width - How wide the object appears

Practice Example 1: Color Detection

Setup: Configure SIG_1 for red objects

Challenge: Robot says "Found it!" when it sees a red object.

Algorithm:

Forever loop: Take snapshot with Vision.RED_CUBE IF Vision detecting signature RED_CUBE THEN Stop motors Print "Found red object!" Wait 1 second ELSE Drive forward slowly Print "Searching..." END

Practice Example 2: Object Tracking

Setup: Configure SIG_1 for a colored object

Challenge: Robot turns to keep the object centered in view.

Algorithm:

Create variable "objectX" Forever loop: Take snapshot with Vision.SIG_1 IF Vision detecting signature SIG_1 THEN Set objectX = Vision largest object centerX Print "Object at X: " + objectX IF objectX < 120 (object is left of center) THEN Turn left slowly ELSE IF objectX > 200 (object is right of center) THEN Turn right slowly ELSE Stop (object centered!) Print "Locked on target!" ELSE Spin right (search for object) END

Explanation: Vision sensor has ~316 pixels width. Center is around 158. We use 120-200 as "close enough" to center.

Practice Example 3: Multi-Color Sorting

Setup: Configure SIG_1 = RED, SIG_2 = BLUE, SIG_3 = GREEN

Challenge: Robot responds differently to each color.

Algorithm:

Forever loop: Take snapshot with Vision.SIG_1 (check red) IF Vision detecting signature SIG_1 THEN Print "Red detected - Action A" Turn left 90 degrees Wait 1 second Take snapshot with Vision.SIG_2 (check blue) ELSE IF Vision detecting signature SIG_2 THEN Print "Blue detected - Action B" Drive forward 1 rotation Wait 1 second Take snapshot with Vision.SIG_3 (check green) ELSE IF Vision detecting signature SIG_3 THEN Print "Green detected - Action C" Turn right 90 degrees Wait 1 second ELSE Print "No color detected" END

Practice Example 4: Object Size Detection

Challenge: Determine if object is close or far based on its size.

Algorithm:

Create variable "objectSize" Forever loop: Take snapshot with Vision.SIG_1 IF Vision detecting signature SIG_1 THEN Set objectSize = Vision largest object width Print "Size: " + objectSize IF objectSize > 100 THEN Print "Object very close!" Stop ELSE IF objectSize > 50 THEN Print "Object nearby" Drive slow ELSE Print "Object far away" Drive normal speed END

Concept: Objects appear larger when closer to camera!

💡 Pro Tip: Vision sensors work best with solid, bright colors in good lighting. Avoid shiny or reflective objects. Test your signatures in the actual competition/classroom environment!

🔘 Bumper Switch

🔧 Hardware Setup

  1. Connect Bumper Switch to a 3-wire port (e.g., Port A, B, C, etc.)
  2. The switch has three wires - ensure proper orientation
  3. Mount on robot where it will hit obstacles first (front bumper)
  4. Test by pressing it manually - you should feel a click

In VEXcode Blocks:

  • Go to Devices menu
  • Add device → Bumper Switch
  • Name it (e.g., "FrontBumper", "RearBumper")
  • Select the 3-wire port you used

How It Works

A bumper switch is a simple digital sensor that's either pressed or not pressed. It's perfect for collision detection!

Key Blocks:

  • FrontBumper pressed - Returns true when switch is pressed
  • FrontBumper released - Returns true when switch is not pressed
  • When FrontBumper pressed - Event-based programming (advanced)

States:

  • Pressed (true): Switch is pushed in
  • Released (false): Switch is not pushed

Practice Example 1: Basic Collision Detection

Challenge: Stop robot when bumper hits something.

Algorithm:

Drive forward at 50% speed Wait until (FrontBumper pressed) Stop all motors Print "Collision detected!"

Skills practiced: Reading digital sensor, wait until command

Practice Example 2: Reverse and Continue

Challenge: When robot hits wall, back up and continue.

Algorithm:

Forever loop: Drive forward at 50% speed IF FrontBumper pressed THEN Stop motors Print "Hit detected!" Drive backward for 1 rotation Turn right 45 degrees Print "Adjusted course"

Practice Example 3: Collision Counter

Challenge: Count how many times the robot hits obstacles.

Algorithm:

Create variable "collisionCount" = 0 Create variable "wasPressed" = false Forever loop: Drive forward at 50% speed IF (FrontBumper pressed) AND (NOT wasPressed) THEN Change collisionCount by 1 Set wasPressed = true Print "Collisions: " + collisionCount Stop motors Wait 0.5 seconds Drive backward for 0.5 rotations Turn right 90 degrees END IF FrontBumper released THEN Set wasPressed = false

Explanation: The "wasPressed" variable prevents counting the same collision multiple times.

Practice Example 4: Dual Bumper Navigation

Setup: Add FrontBumper and RearBumper to your robot

Challenge: Robot responds differently based on which bumper is hit.

Algorithm:

Forever loop: Drive forward at 40% speed END IF FrontBumper pressed THEN Print "Front collision!" Stop motors Drive backward for 1 rotation Turn right 90 degrees END IF RearBumper pressed THEN Print "Rear collision!" Stop motors Drive forward for 1 rotation Turn left 90 degrees END

Practice Example 5: Bumper + Logic Operators

Challenge: Complex behavior using bumper with other conditions.

Algorithm:

Create variable "autoMode" = true Create variable "emergencyStops" = 0 Forever loop: IF autoMode AND (FrontBumper released) Drive forward at 60% speed END IF FrontBumper pressed THEN Stop motors Change emergencyStops by 1 Print "Stops: " + emergencyStops IF emergencyStops >= 5 THEN Set autoMode = false Print "Too many collisions - Manual mode" ELSE Wait 1 second Drive backward for 0.5 rotations END IF (NOT autoMode) AND (button A pressed) THEN Set autoMode = true Set emergencyStops = 0 Print "Auto mode resumed" END

Concept: After 5 collisions, robot switches to manual mode for safety!

Practice Example 6: Touch-to-Start

Challenge: Use bumper as a start button.

Algorithm:

Print "Press bumper to start" Wait until (FrontBumper pressed) Wait until (FrontBumper released) Print "Starting in 3..." Wait 1 second Print "2..." Wait 1 second Print "1..." Wait 1 second Print "GO!" Drive forward at 75% speed for 3 rotations Turn right 90 degrees Drive forward at 75% speed for 3 rotations Print "Complete!"
💡 Pro Tip: Bumper switches are binary (on/off) so they're the simplest sensors. They're great for learning about digital sensors and event-driven programming. Try combining them with other sensors for smarter robots!