Is there an alternative to copy-paste for repeating code?

Slides:



Advertisements
Similar presentations
Variables and Functions ROBOTC Software. Variables A variable is a space in your robots memory where data can be stored, including whole numbers, decimal.
Advertisements

08/2012Tanya Mishra1 EASYC for VEX Cortex Llano Estacado RoboRaiders FRC Team 1817.
Autonomy using Encoders Intro to Robotics. Goal Our new task is to navigate a labyrinth. But this time we will NOT use motor commands in conjunction with.
Programming Concepts (Part A) Ping Hsu. What is a program? WHITE CAKE RECIPE 1.Preheat oven to 350 degrees F (175 degrees C). 2.Grease and flour a 9x9.
Programming – Touch Sensors Intro to Robotics. The Limit Switch When designing robotic arms there is always the chance the arm will move too far up or.
What is RobotC?!?! Team 2425 Hydra. Overview What is RobotC What is RobotC used for What you need to program a robot How a robot program works Framework.
For Loops. Challenge: Racer ● Simulate a race that says “Now on lap X” for 10 laps. ● Make X vary, so it says 1, then 2, then 3 ● Use only one output.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
New Tools And Workshop Mod & For Loops. Modulo Calculates the remainder (remember long division?) % Examples: 7 % 3 10 % 2 2 % 3 evaluates to 1 evaluates.
Autonomy using Encoders Intro to Robotics. Autonomy/Encoders Forward for Distance In this unit, you will learn to use the encoders to control the distance.
Chapter 4 Grouping Objects. Flexible Sized Collections  When writing a program, we often need to be able to group objects into collections  It is typical.
Loops For, While, and Do While. Loop structure Loops need a control variable to operate correctly. This variable must be declared, initialized, updated,
Getting Started in RobotC // Comment task main() motor[] {} wait1Msec() ; = Header Code Compile Download Run Take out your notes.
Variables and Functions ROBOTC Software © 2012 Project Lead The Way, Inc.Principles of Engineering.
RobotC Remote Control. Learning Objectives: Focusing on Virtual World with Physical Examples Understand Real-Time Joystick Mapping Understand how to use.
ROBOTC for IFI Timothy Friez Class #4 2/5/08. Agenda for Tonight Timers Functions Your Questions.
Loops. Review Control Structure – making decisions if-statement Program Design Understand what you want to do Design your solution Write and test your.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
Tarik Booker CS 242. What we will cover…  Functions  Function Syntax  Local Variables  Global Variables  The Scope of Variables  Making Functions.
Robotics Programming Wall Follow Line tracking for a set amount of time Line tracking for a distance.
COP 3275 – Finishing Loops and Beginning Arrays Instructor: Diego Rivera-Gutierrez.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Introduction To Repetition The for loop
Robotics Programming Using Shaft Encoders
Programming Mehdi Bukhari.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
StartStruck in a Virtual World
Formatting Output.
Topic 5 for Loops -Arthur Schopenhauer
StarStruck in a Virtual World: Fantasticbot Version
Movement using Shaft Encoders
Using Encoders to go Straight
Autonomy using Encoders
Programming – Touch Sensors
Problem Solving (design of programs) CS140: Introduction to Computing 1 8/26/13.
Math in C The math blocks you've used in Scratch can all be recreated in C!
RobotC Sensors.
Getting Started in RobotC
For Loops October 12, 2017.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Using variables, for..loop and functions to help organize your code
LRobot Game.
Basketball Drill Programming Alternatives
Unit-1 Introduction to Java
Introduction to Object-Oriented Programming with Java--Wu
An Introduction to VEX IQ Programming with Modkit
Getting Started in RobotC
Java Programming Arrays
Java Programming Loops
Auto Straightening using VEX Shaft Encoders
Programming – Remote Control Statements
Topic 1: Problem Solving
Programming – Remote Control Statements
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Repeating Actions (Loops)
For Loops.
Sridhar Narayan Java Basics Sridhar Narayan
Just Basic Lesson 17 Part 1 Mr. Kalmes.
CISC181 Introduction to Computer Science Dr
RobotC While loop When and how to use the while loop
Robotics Programming Using Shaft Encoders
Robotics Programming Using Shaft Encoders
Remote Control For this activity we will use the Squarebot
Game Over Module 4 Lesson 2.
Java Programming Loops
C Programming Pointers
How can you make a guessing game?
Robotics Programming Review and Quiz.
Robotics Programming Using Shaft Encoders
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Is there an alternative to copy-paste for repeating code? Today we will look at a way to have robotC repeat code without having to copy..paste..copy..paste…

Learning Targets Be able to create and use a variable Be able to read a program that uses a for loop Be able to write a program that uses a for loop.

For loop to the rescue Great for repeating a chunk of code a set number of times Like Sentry Simulation Basketball drills … We will incorporate this into the Basketball drills program, then let you implement it on the Sentry Simulation

Looking at Potential Solutions to Basketball Drills Pseudo Code Go forward long enough to cross the first line Come back Go forward long enough to cross the second line Go forward lone enough to cross the third line With enough guessing and checking, you can get the correct values for the wait1Msec()

Using a Variable to help with changes Variable name rules. An integer variable. Start with a letter No spaces – punctuation Not a reserved word Describes what it is storing. If there was only a way to repeat a chuck on code without having to copy - paste

For loop in RobotC When to use it Syntax When you want to repeat something a set number of times Syntax for(int line = 1; line<=3; line++) { //Code repeated } Declares an integer variable called line and gives it an initial value of 1 If the line variable is less than or equal to 3 when it reaches this, it will do the loop another time. After completing the loop, it will add 1 to the variable line. In this example it will repeat the code inside the {} three times. Once when line = 1 Once when line = 2 And Once when line = 3

Describe how the robot would move with the following code int x = 1000; for(int line = 1; line<=3; line++) { motor[leftMotor] = 50; motor[rightMotor] = 50; wait1Msec(x*line); motor[leftMotor] = -50; } for(int line = 1; line<=3; line++) { motor[leftMotor] = 50; motor[rightMotor] = 50; wait1Msec(1000*line); motor[leftMotor] = -50; } for(int line = 1; line<=3; line++) { motor[leftMotor] = 50; motor[rightMotor] = 50; wait1Msec(1000); motor[leftMotor] = -50; }

No loop vs. for loop

Since line = 1 the first time through this loop line Since line = 1 the first time through this loop line*timeToLine is the same as 1*2400 = 2400 the first time through this loop. Then 2*2400 = 4800 the second time and 3*2400 = 7200 the third time. RobotC does the math inside the () before executing the wait1Msec() command For loop example

Today Complete the Movement Challenges Incorporate the for loop For each Challenge you complete using the for loop to help with the solution, you get 10% Extra Credit for the assignment

For loop