Skip to content
Parallax Learn (Stage)

Parallax Learn (Stage)

This is the staging site. Please see https://learn.parallax.com for the official site.

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence Tutorial Series
          • Cybersecurity: Radio Data tutorialCybersecurity Tutorial Series
          • cyber:bot Tutorial Series
          • Boe-Bot Tutorial SeriesBoe-Bot Tutorial Series
          • Arduino Shield-Bot Tutorial Series
          • ActivityBot with C TutorialsActivityBot with C Tutorials
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot with BlocklyProp Tutorial Series
          • Scribbler 3 Tutorial SeriesScribbler 3 Tutorial Series
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education Tutorial Series
          • Propeller C-Language BasicsPropeller C Basics and Projects
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit C Tutorial Series
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit BlocklyProp Tutorials
          • Badge WX Tutorial SeriesBadge WX Tutorial Series
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics and Projects
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • Robotics with the Board of Education Shield for Arduino

Robotics with the Board of Education Shield for Arduino

How TestServoSpeed Works

The sketch TestServoSpeed increments the value of a variable named pulseWidth by 25 each time through a for loop.

  // Loop counts with pulseWidth from 1375 to 1625 in increments of 25.

  for(int pulseWidth = 1375; pulseWidth <= 1625; pulseWidth += 25)

With each repetition of the for loop, it displays the value of the next pulse width that it will send to the pin 13 servo, along with a user prompt.

    Serial.print("pulseWidth = ");           // Display pulseWidth value
    Serial.println(pulseWidth);
    Serial.println("Press a key and click"); // User prompt
    Serial.println("Send to start servo...");

After Serial.begin in the setup loop, the Arduino sets aside some memory for characters coming in from the Serial Monitor.  This memory is typically called a serial buffer, and that’s where ASCII values from the Serial Monitor are stored.  Each time you use Serial.read to get a character from the buffer, the Arduino subtracts 1 from the number of characters waiting in the buffer. 

A call to Serial.available will tell you how many characters are in the buffer.  This sketch uses while(Serial.available() = = 0) to wait until the Serial Monitor sends a character.  Before moving on to run the servos, it uses Serial.read( ) to remove the character from the buffer.  The sketch could have used int myVar = Serial.read( ) to copy the character to a variable.  Since the code isn’t using the character’s value to make decisions, it just calls Serial.read, but doesn’t copy the result anywhere.  The important part is that it needs to clear the buffer so that Serial.available( ) returns zero next time through the loop.  

while(Serial.available() == 0);      // Wait for character
Serial.read();                       // Clear character

Where is the while loop’s code block?
The C language allows the while loop to use an empty code block, in this case to wait there until it receives a character.  When you type a character into the Serial Monitor, Serial.available returns 1 instead of 0, so the while loop lets the sketch move on to the next statement.  Serial.read removes that character you typed from the Arduino’s serial buffer to make sure that Serial.available returns 0 next time through the loop.   You could have typed this empty while loop other ways:
   while(Serial.available() == 0) {}  
…or:
   while(Serial.available() == 0) {}; .

After the Arduino receives a character from the keyboard, it displays the “Running…” message and then makes the servo turn for 6 seconds.  Remember that the for loop this code is in starts the pulseWidth variable at 1375 and adds 25 to it with each repetition.  So, the first time through the loop, servoLeft is 1375, the second time through it’s 1400, and so on all the way up to 1625. 

Each time through the loop, servoLeft.writeMicroseconds(pulseWidth) uses the value that pulseWidth stores to set servo speed.  That’s how it updates the servo’s speed each time you send a character to the Arduino with the Serial Monitor.

Serial.println("Running...");
servoLeft.writeMicroseconds(pulseWidth); // Pin 13 speed=pulse
delay(6000);                             // ..for 6 seconds
servoLeft.writeMicroseconds(1500);       // Pin 13 speed=stop

Printer-friendly version
Example Sketch: Test Servo Speed
Prev
Optional: Record Your Own Transfer Curve Data
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn (Stage) • Built with GeneratePress