🤖 VEX V5 Programming Practice

Master Variables, Conditionals, Logic Operators & Sensors

← Back to Robotics Home

📊 Flowcharts

Launch the interactive flowchart builder in a new window so you can keep this guide open for reference.

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 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: Creating and Displaying a Variable

Challenge: Learn how to create a variable and display its value on the brain screen.

Step-by-step instructions:

  1. Open VEX V5 Code Visual Blocks
  2. Go to the Logic category (orange blocks)
  3. Click "Make a Variable" button
  4. Name your variable "score"
  5. Drag the "set [score] to [0]" block into your workspace
  6. Change the value from 0 to 10
  7. From the Events category, drag a "when started" block to the top
  8. From the Screen category, drag a "print [ ]" block below
  9. Return to the Logic category and drag the oval "score" variable block
  10. Place the "score" variable block into the empty slot of the "print [ ]" block

Goal: Display the value of "score" on the brain screen.

Practice Example 2: Changing Variable Values

Challenge: Create a program that changes the robot's speed by updating a variable.

Step-by-step instructions:

  1. Open VEX V5 Code Visual Blocks
  2. Go to the Logic category (orange blocks)
  3. Click "Make a Variable" button
  4. Name your variable "driveSpeed"
  5. From Events, drag "when started" block to workspace
  6. From Logic, drag "set [driveSpeed] to [0]" and change the value to 25
  7. From Drivetrain, drag "set drive velocity to [0] %" block
  8. From Logic, drag the oval "driveSpeed" variable block into the velocity slot
  9. From Screen, drag "print [ ]" block
  10. From Logic, drag the oval "driveSpeed" variable into the print block
  11. From Drivetrain, drag "drive [forward]" block
  12. From Control, drag "wait [1] seconds" and change to 3 seconds
  13. From Logic, drag another "set [driveSpeed] to [0]" and change value to 100
  14. From Drivetrain, drag another "set drive velocity to [0] %" block
  15. From Logic, drag the oval "driveSpeed" variable into the velocity slot
  16. From Screen, drag "print [ ]" block
  17. From Logic, drag the oval "driveSpeed" variable into the print block

Goal: The robot starts driving forward at 25% speed (showing "25" on the screen), then after 3 seconds accelerates to 100% speed (showing "100" on the screen) to demonstrate how changing a variable's value changes the robot's behavior!

🔀 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:

Set buttonPressed to 0 Forever loop: IF BumperA pressed THEN Change buttonPressed by 1 Wait 0.05 seconds END IF buttonPressed = 1 THEN Set drive velocity to 25% ELSE IF buttonPressed = 2 THEN Set drive velocity to 50% ELSE IF buttonPressed = 3 THEN Set drive velocity to 100% ELSE IF buttonPressed = 4 THEN Set buttonPressed to 1 ELSE Set drive velocity to 25% END END END END END

Practice Example 3: Battery Level Warning

Challenge: Check battery percentage and respond appropriately.

Logic:

Forever loop: IF battery capacity ≥ 80 THEN Print "Battery Fully Charged" on screen and set cursor to next row ELSE IF (battery capacity ≥ 50) AND (battery capacity < 80) THEN Print "Battery above 50%" on screen and set cursor to next row ELSE IF (battery capacity < 50) AND (battery capacity > 25) THEN Print "Battery is low" on screen and set cursor to next row ELSE IF battery capacity ≤ 25 THEN Print "Battery is dangerously low" on screen and set cursor to next row Stop driving END END END END END

Practice Example 4: Count Down Timer

Challenge: Create a count down timer to lift off.

Logic:

Set countDown to 10 Print join "Count down to blast off in: " countDown on screen While countDown > 0 Change countDown by -1 Wait 1 second Clear screen Print join "Count down to blast off in: " countDown on screen End Print "Blast off!" on screen

🔗 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 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 END END 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 THEN Turn left slowly ELSE IF objectX > 200 THEN Turn right slowly ELSE Stop Print "Locked on target!" END END ELSE Spin right END 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 END END 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) 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!