Week 21: Servo Movement

Overview of Activity:

During this meeting, members learned about logic statements and loop statements (While loop, Do-while loop, and For loop). Members also learned about Operators and how they are important to making conditions. Finally, members were given the code for servos, which was explained line by line.

Operators

Operators are symbols designed to tell the computer to do a specific mathematical or logical task. An example of an operator that we've already used is the = sign to set a variable equal to a number!

*See slides 3-6 for a list of commonly used operators*

*Further information can be found here*

Logic Statements

A common logic statement is the if statement.

  • If statements execute a set of code when condition is true.

  • An optional else statement follows an if statement, executing a separate set of code when the condition for the if statement is false.

  • An optional elseif statement follows an if statement, executing a separate set of code when an alternative condition is true.

*See slide 8 for syntax and further explanation*

Loop statements

Some common loop statements are the While loop and the For loop

  • A While loop repeats a code when a certain condition is constantly met. If there is nothing to break the loop, the loop would go on forever.

  • A Do-while loop is the same as a while loop except it performs the set of code at least once before checking for a certain condition.

  • A For loop is more complicated and has a more limited condition, but it prevents loops from going forever and allows the servo to be programmed at ease.

*See slides 9-11 for syntax and further explanation*

Servo Code & Explanation

  • #include <Servo.h> imports the Servo library. A library contains functions for specific hardwares and allows you to program them easily.

  • Servo myservo; creates a servo object called myservo that contains access to all functions in the Servo library.

  • myservo.attach(); tells the Arduino which pin the servo is connected to.

  • myservo.write(); tells the servo to go to reach a specific position (from 0 degrees to 180 degrees)

  • The first for loop makes the servo go from 0 degrees to 180 degrees gradually at 1 degree every 15 miliseconds

  • The second for loop makes the servo go from 180 degrees to 0 degrees gradually at 1 degree every 15 miliseconds

Servo Code

The Presentation

3/3 Logic/Loop and Servo Meeting

Questions?

If you have any questions, feel free to go to our discord server and ask us in the #programming channel. You can also show us your success in the #general-photo channel.