CSC 115.005/006 Sonoma State University Spring 2022
Scribbler 2
CSC 115.005/006:
Programming I
Scribbler 2
Instructor: Henry M. Walker

Lecturer, Sonoma State University
Professor Emeritus of Computer Science and Mathematics, Grinnell College


Course Home References Course Details: Syllabus, Schedule, Deadlines, Topic organization MyroC Documentation Project Scope/
Acknowledgments

Notes:

Laboratory Exercise on Loops

This laboratory exercise provides experiences with three different types of loops: for, while, and do ... while.


Work Started in Class

Initial Experiments with Loops

  1. Copy 3-loops.c to your account, compile and run it, and explain in your own words how each loop works. Also, explain the purpose of the line printf ("\n");. Is this statement in the loop or not? Why?

  2. In the 3-loops.c program, change the initialization of start to 11, recompile, and rerun the program. Describe what happens and explain why.

Printing a Table

  1. In the first segment of this course, you saw a program (quarts.c), that converted quarts to liters. Write a program that prints a table listing the conversions from one to twelve quarts into liters.

    • Use the line printf("%4d%16.4lf\n", quarts, liters); to keep proper spacing.

    Example Output:

    Table of quart and liter equivalents
    Quarts           Liters
       1             0.9463
       2             1.8927 
       3             2.8390 
       4             3.7853 
       5             4.7317 
       6             5.6780 
       7             6.6243 
       8             7.5707 
       9             8.5170 
      10             9.4633 
      11            10.4097 
      12            11.3560 
    
  1. In your first version of the program, implement the loop using a for construct.
  2. In your second version of the program, implement the loop using a while construct.

Loops with a Scribbler 2 Robot

  1. Simple Motion Commands: Write one or more programs that have the Scribbler 2 robot move in various ways:

    1. A for loop should move the scribbler forward 5 times.
    2. A while loop should move the Scribbler in some direction for increasing amounts of time.
    3. A for loop should move the Scribbler some number of times at changing speeds.
    4. A while loop should change both speed and time in the same loop.
  2. Rising Pitch: A program is supposed to beep once at 800 Hz, then increase by 20 Hz every beep for another twelve beeps. Write this program using the following template for a for loop based on an integer variable i:

    for (i = 0; i <= 12; i++)
    {
       int freq = /* compute frequency here */
       rBeep (1.0, freq);
    }
    

Quick Example and Review

The reading for this session identified three basic types of loops: for, while, and do..while. Program 3-loops.c illustrates each of these constructions to print the numbers 1 to 10 on separate lines.

Although the loops have many similarities, one loop may seem to fit a circumstance more naturally than another.

The for loop often focuses on the progression of one or more control variables. The following code emphasizes the values for i during the running of the code.

  /* for loop */
  printf ("for loop\n");
  for (i = start; i <= end; i++)
    {
      printf ("%5d", i);
    }
  printf ("\n");

A while loop often emphasizes the condition under which the loop will continue. Updating variables takes place in the body of the loop and may be complex.

  /* while loop */
  printf ("while loop\n");
  i = start;
  while (i <= end)
    {
      printf ("%5d", i);
      i++;
    }
  printf ("\n");

In contrast to for and while which may skip the loop completely if the condition is not satisfied, the do..while always executes its loop body at least once.

  /* do while */
  printf ("do..while loop\n");
  i = start;
  do
    {
      printf ("%5d", i);
      i++;
    }
  while (i <= end);
  printf ("\n");

Lights on the Fluke

  1. Consider the sequence

    • The Fluke's front LED is turned on (see the MuyroC documentation for rSetLEDFront for details.)
    • The robot moves forward 1 second.
    • The Fluke's front LED is turned off
    • The Fluke's back LED is turned on.
    • The robot moves backware 1 second.
    • The Fluke's back LED is turned off.

    Write a program that repeats the above sequence 5 times using a loop.

Infinite Loops

Computers are quite patient, so once a loop starts, the loop will continue as long as the appropriate condition remains true. If the condition never becomes false, the loop will never stop. For example, the following loop will continue forever

while (1)
{

}

Although this type of construct may seem peculiar, such loops can be useful from time to time. For example, we expect to type commands in a terminal window for as long as the window remains open. Thus, the code behind the terminal window might use a while (1) construct to keep reading and processing commands.

Terminating loop manually

As a practical matter, if you encounter this situation with a program, and if you want the program to stop, you can use the key combination ctrl-c to end your program in the terminal window. In this situation, turning the Scribbler 2 off or pressing the reset button resets the robot.


Homework


Blocking and Nonblocking Commands

  1. Consider the following code segment which includes a movement command and the sounding of three notes.

       rForward (1.0, 5.0);
       rBeep (1.0, 880);
       rBeep (1.0, 1280);
       rBeep (1.0, 1760);
    
    1. Include this code segment in a program, and observe what happens. When the motion starts, do the beeps sound as the robot moves (nonblocking movement), or do the beeps sound after the robot movement has finished (blocking movement)?
    2. Change the duration in rForward from 5.0 to -5.0. Then repeat part a.

    Write, in your own words, what it means for a command to be blocking or nonblocking.

Scribbler motion

The Scribbler 2 movement commands may be organized into two basic groups:

The reading on Scribbler 2 motion provides additional details regarding Scribbler 2 sensor and motion commands.


Spiral Motion

Consider how to make the robot move spirally. Spirals begin from a center point, with the line moving in a circular motion, with a gradually greater distance from the origin.

  1. Write a program that makes the Scribbler 2 robot move in stages in a spiral-like motion. The robot should move forward a little, then turn (e.g., rTurnLeft or rTurnRight), then move forward a little further, then turn, and so on in a spiral shape.

    • Just a single loop is needed here.
    • The overall program should move the robot in an approximate spiral for at least 15 seconds.

Motion With Obstacles

While blind motion can be interesting, sensing obstacles is where motion really gets awesome!

  1. Write a simple program which moves the robot forward until it sees an obstacle.

    Hint: Experimentation with the obstacle sensors suggests moderate variability in the readings obtained. One way to reduce the variability of these readings is to take several readings and average. With the MyroC package, one could use the rGetObstacleTxt(char * value, int sampleSize) function. Here the sampleSize parameter allows one to take several readings of the sensor and compute an average. To get a single reading, use 1 as the sampleSize.

  2. Write a simple program which moves the robot forward until it sees an obstacle, then turns right, then moves forward again until it sees an obstacle.

  3. Now generalize your program so that your robot moves forward until it sees an obstacle, then turns right, moves forward until it sees an obstacle, turns right, moves forward until obstacle, turns right, etc. It should do this until the program is terminated.

Sensor command parameters

Several MyroC sensor commands, such as rGetObstacleTxt(char * value, int sampleSize), have a parameter with type char *. This syntax is C's way of specifying a character string — a sequence of characters within double quotes. In this case, the first parameter indicates which obstacle sensor to consult; the choices are "left", "center" or "middle" or "right", with "center" and "middle" alternatives for the same light sensor.

A later segment of this course will consider character strings in C in substantial detail. For now, think of the type char * as a string of characters, placed in double quotes.



created July 20 2011 by April O'Neill
revised July 29 2011 by April O'Neill
minor editing 24 August 2011 by Henry M. Walker
last full revision 9 October 2011 by Erik Opavsky
revised 13 February 2012 by Erik Opavsky
minor editing (section title change) 20 July 2012 by Henry M. Walker
minor editing (updated URL) 10 September 2013 by Henry M. Walker
revised (merged loops and motion labs) 18 January 2014 by Henry M. Walker
reorganized with heavy editing 21 January 2014 by Henry M. Walker
spiral motion updated 16 September 2014 by Henry M. Walker
readings added 19 September 2014 by Henry M. Walker
notes on blocking and nonblocking added 3 November 2014 by Vasilisa Bashlovkina and Nicholas Knoebber
revisions for new MyroC, expanded discussion of blocking/nonblocking 4 November 2014 by Henry M. Walker
reworked for revised format and context, 17-18 July 2016 by Henry M. Walker
revised to move nested loops to a later lab, December 12, 2021 by Henry M. Walker
Valid HTML 4.01! Valid CSS!
For more information, please contact Henry M. Walker at walker@cs.grinnell.edu.