-
Notifications
You must be signed in to change notification settings - Fork 7
Coding Examples and Help
The goal of the code section is to help provide a reference for students when starting projects or assignments.
For the following tutorials, we have defined our parts as such:
| Part | Variable Name |
|---|---|
| Left Motor | left |
| Right Motor | right |
| Marker | marker |
| Light | light |
| Light Sensor | lightSensor |
| Range Sensor | rangeSensor |
| Laser | laser |
Note: If you would like to follow along exactly with the following code, you will need to change your part names in GradBot.
A delay is used to wait for a certain length of time before completing the next segment of the code.
To use a delay, we will use this function:
await delay(1000);
In GradBot, we measure delay time in milliseconds. There are 1000 milliseconds in a second. Increase or decrease the milliseconds in the delay to shorten or lengthen the time the delay is active.
Conditional Statement: A conditional statement is a set of rules performed if a certain condition is met.
Nesting: Occurs when one programming construct is included within another.
The conditional statements we will be discussing are:
if-then statements
if-else-statements
nesting statements
Conditional statements are widely used and very powerful when it comes to programming. They have the power to steer a program in a certain direction based on current values. Let's take a look at a basic example and then an example that can be applied to GradBot.
If-Then Statements are the most common and widely used conditional statement. They are a fundamental piece in almost all programming languages, and they are super easy to use. Let's build an example to show how they work.
Example: Let's say you want to buy a candy bar. You enter the store with $5 and go to the candy section. After picking your favorite candy you go to the checkout and give the cashier the money. They return your change, and you leave with your sweet tooth fulfilled.
In this example, you know that if you have enough money to buy the candy then you can purchase the candy. The simple program for this would appear as such:
if(you have enough money){
get the candy bar
buy the candy bar
get your change
leave the store
}
You may wonder, what happens if you don't have enough money for the candy bar? In that case you would not do anything inside of the if statement's brackets. This condition must be met or that section is skipped over.
Let's use the same example but say that your friend Josh has some candy at his house that you can have if you can't get some from the store. This will lead us to what's called an if-else statement.
The simple code for a situation like this would appear as such:
if(you have enough money){`
Get the candy bar
Buy the candy bar
Get your change
Leave the store
}
else{
Go to Josh's house
Get candy from Josh
}
Nesting Statements are when we see statements put inside of statements. The following case is an example of nesting:
if(condition){
if(condition){
}
}
The inner if statement will only execute if the outer if statement is true. For, while, do-while, and all other statements can be nested with eachother. This is a common and incredibly useful practice in computer programming.
Loops are used in programming to go over the same section of code multiple times. They help to decrease the code that needs to be written and increase productivity. The three main types of loops we will talk about are:
For-Loops
While-Loops
Do-While Loops
We'll be discussing each loop to ensure the function and purpose of each is clear.
Definition: The For-Loop is used to repeat a certain statement a known number of times. This is known as an entry-controlled loop. This means that certain conditions must be met before the loop is executed. It is possible to have a loop not execute at all if it does not meet entry level criteria.
Pseudo code:
The structure for a for loop is as follows:
for(control/counter variable; loop condition; increment)
The control/counter variable is used to count the number of times that the loop has been executed. The loop condition tells the loop to continue to loop until the condition is no longer being met, and the increment is used to change the
counter each time the loop is executed.
Code:
Let's look at an example of a for loop.
var counter= 0;
for(var i = 0; i < 10; i = i + 1){
left.setPower(counter);
right.setPower(counter);
counter = counter + 10;
await delay(1000);
}
In this situation we see that the for-loop initializes a variable called 'i'. As long as 'i' is less than 10 the loop will continue to execute. Finally, the last statement increases 'i' by 1 each time the loop operates. This loop is designed to run 10 times and increment the power of the vehicle by 10 every second until it reaches its max speed.
Definition:
While-loops are used to execute a section of code until the condition in the while statement is no longer true. This, like the for loop, is an entry-controlled loop. The condition must be satisfied before the loop is executed.
Code:
In the example below we will be increasing the car's power by 10 once every second until it reaches 100 (the maximum power).
var counter = 0;
while(counter < 100){
left.setPower(counter);
right.setPower(counter);
counter = counter + 10;
await delay(1000);
}
The code inside the while loop will execute as long as the value of counter is less than 100. Once the value reaches 100 or is above 100, the while loop will not execute the next time and will continue on.
Note: If you enter a while loop, the ENTIRE loop will finish before testing the condition again. This means that values inside will continue to change, even if they no longer satisfy the condition. There is also no guarantee that a while loop will function even once.
Definition: These loops are very similar to while-loops, with one major difference. A do-while loop is guaranteed to execute at least one time since its conditional is at the bottom. This is known as an exit-controlled loop.
Code:
Let's look at the same example as above but in a do-while loop.
var counter = 0;
do{
left.setPower(counter);
right.setPower(counter);
counter = counter + 10;
await delay(1000);
}while(counter<100)
If we were to change the value of the counter to 1000, the while loop would skip over the section in the loop whereas the do-while loop would execute the code in the do-while statement at least once before skipping over the loop.
Summary: If you find yourself repeating the same sections of code over and over, consider using a loop! It will make your code more manageable as well as increase productivity.
a. Forward movement:
left.setPower(100);
right.setPower(100);
Note: Forward movement can have values in the range of 1 to 100.
b. Backward movement:
left.setPower(-100);
right.setPower(-100);
Note: Forward movement can have values in the range of -1 to -100.
Turning is GradBot is performed by setting the power of one wheel higher than the other. A larger gap in between the power the wheels are set to will result in a smaller turn radius.
a. Left Turn:
left.setPower(50);
right.setPower(100);
Note: This will create a counterclockwise circle.
b. Right Turn:
left.setPower(100);
right.setPower(50);
Note: This will create a clockwise circle.
a. Begin Drawing:
marker.penDown();
b. Stop Drawing:
Marker.penUp();
Note: Stop drawing is on by default.
If you'd like to see some sample GradBot Student Assignments, click here.