RAPPAHANNOCK COMMUNITY COLLEGE STEM CAMP
  • Home
  • 2021 STEM Camp (HS)
    • PreAcademy prep
    • Day 1: Basics, Lights, and Temperature Sensor >
      • Day 1 Supplement: Measure Temperature
      • Day 1 Supplement: IR Temp Sensor
      • Day 1 Supplement: Identifying Resistors
      • Supplement: Controlling Buttons
      • Supplement: Controlling A Servo
    • Day 2: Measuring Pulse >
      • How the body absorbs light
      • Day 2 Supplement: IR Light Proximity Sensor
      • Day 2 Supplement: Motion Sensor
      • Code to start later on plotter
    • Day 3 Supplement: OLED Screen Basics >
      • Display Screen Temperature
      • IR Temp Sensor And Screen
      • Scrolling Screen Graph
    • Day 4: Finishing up/Show >
      • Pictures to OLED
      • BPM Pulse Sensor
      • Multimeter
  • Shared Google Folder
  • The Teachers
  • Archive
    • Pictures from prior years
    • 2021 STEM Camp (MS) >
      • Home (2021)
      • PreAcademy prep
      • Day 1: Basics, Lights, and Temperature Sensor >
        • Day 1 Supplement: Identifying Resistors
        • Day 1 Supplement: Measure Temperature
        • Day 1 Supplement: IR Light Proximity Sensor
      • Day 2: Ultrasound >
        • Day 2 Supplement: IR Temp Sensor
        • Day 2 Supplement: Controlling A Servo
      • Day 3: Measuring Pulse >
        • Day 3 Supplement: Motion Sensor
      • Day 4: OLED Screen Basics >
        • Temperature Screen Display
        • IR Temp Sensor And Screen
      • Day 5 Friday: Finishing up/Show
    • 2020 STEM Camp >
      • Home (2020)
      • The 2020 Teachers
      • PreAcademy prep
      • Day 1 Monday: Basics and Lights >
        • Day 1 Supplement Activity
      • Day 2 Tuesday: Servos >
        • Day 2 (Suppliment Activities)
      • Day 3 Wednesday: Making an ECG/EKG! >
        • Day 3 (Supplement Page)
      • Day 4 Thursday: Controlling Servos with EMG >
        • Day 4 Supplemental Activity
      • Day 5 Friday: Finishing up/Show >
        • Unused lessons/content
    • 2018 Lessons >
      • Day 1: Basic Coding
      • Day 2: Motors
      • Day 3: Build Robot Claw
      • Day 4: Saving Positions
      • Day 5: Presentations
    • 2017 Camp >
      • Day 1 (Monday)
      • Day 2: Multicolor LED and Keypad
      • Day 3: Servo and Keypad
      • Day 4: Multiple Motors/Stepper Motor
      • Day 5: Presentations
    • Pictures archive for lessons (some graphic images)
  • Contact Us!

Today's Lesson  

Today we are going to learn how to use the servo. It is important to check which pins your code uses matches what pins you plug things into. 
We will then try and get the servo to work with the keypad. 
Over the course of the morning you will also start  building your animatronic and designing your skit.
Your presentation skit should not be longer than 5 minutes.  

Servo sweep

A servo is a motor that moves in steps smoothly and only between certain angles
+- 180 degrees. We will start with a simple code to control a servo to go back and forward.

​Build the circuit below and try out the code below. Play with the delay and the angles in the for loops to see what happens
Picture
Picture

Make your servo move back and forward just a certain amount of degrees.

Picture

Get your servo to move with the keypad

Now we will try to get the servo to move clockwise and counterclockwise by pushing keypad buttons. Use the code you just wrote above that worked and move it to your keypad code. Try to move it yourself but if it does not work you can try  copying what is below to a new page.

​Once you get it, try to change some parts to see how it works. 
#include<Keypad.h>
#include <Servo.h>
Servo myservo;

int pos=90; //we will start the servo at 90 deg
int del=15;    //this is the delay for the functions
const byte ROWS = 4; //four rows
const byte COLS = 4; //four columns
int change = 30; //how much the servo goes back and forward
int newPos; //this is a variable used to move the servo to a new position of pos+ or - change

char hexaKeys[ROWS][COLS] = {
  {'1','2','3','A'},  //define the symbols on the buttons of the keypads
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'}
};
byte rowPins[ROWS] = {9, 8, 7, 6}; //connect to the row pinouts of the keypad
byte colPins[COLS] = {5, 4, 3, 2}; //connect to the column pinouts of the keypad

Keypad customKeypad=Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS);

void setup() {
  myservo.attach(10);   //this is the number that your servo is connected to
  myservo.write(pos); 
}

void loop() {
 char customKey = customKeypad.getKey();
  if (customKey == '8'){
  newPos = pos+change;     //Variable moves pos counterclockwise by x degrees. Default is 30 deg

    for (; pos <= newPos; pos++){ //makes servo go counter clockwise to new newPos
      myservo.write(pos);
      delay (del);
 }
}

   if (customKey == '9'){         //this button will move it backwards
     newPos = pos-change; //Variable moves pos counterclockwise by x degrees. Default is 30 deg

    for (; pos >= newPos && pos>5; pos--){ //makes servo go counter clockwise to new newPos
      myservo.write(pos);
      delay (del);
   }
  }

   if (customKey == '7'){  //this button makes it move back and forward
     for (pos = 90; pos <= 130; pos += 1){
  myservo.write(pos);
  delay(del);
}
for (pos = 130; pos >= 90; pos -= 1){
  myservo.write(pos);
  delay(del);
 }
}  
  
   }
   
Powered by Create your own unique website with customizable templates.
  • Home
  • 2021 STEM Camp (HS)
    • PreAcademy prep
    • Day 1: Basics, Lights, and Temperature Sensor >
      • Day 1 Supplement: Measure Temperature
      • Day 1 Supplement: IR Temp Sensor
      • Day 1 Supplement: Identifying Resistors
      • Supplement: Controlling Buttons
      • Supplement: Controlling A Servo
    • Day 2: Measuring Pulse >
      • How the body absorbs light
      • Day 2 Supplement: IR Light Proximity Sensor
      • Day 2 Supplement: Motion Sensor
      • Code to start later on plotter
    • Day 3 Supplement: OLED Screen Basics >
      • Display Screen Temperature
      • IR Temp Sensor And Screen
      • Scrolling Screen Graph
    • Day 4: Finishing up/Show >
      • Pictures to OLED
      • BPM Pulse Sensor
      • Multimeter
  • Shared Google Folder
  • The Teachers
  • Archive
    • Pictures from prior years
    • 2021 STEM Camp (MS) >
      • Home (2021)
      • PreAcademy prep
      • Day 1: Basics, Lights, and Temperature Sensor >
        • Day 1 Supplement: Identifying Resistors
        • Day 1 Supplement: Measure Temperature
        • Day 1 Supplement: IR Light Proximity Sensor
      • Day 2: Ultrasound >
        • Day 2 Supplement: IR Temp Sensor
        • Day 2 Supplement: Controlling A Servo
      • Day 3: Measuring Pulse >
        • Day 3 Supplement: Motion Sensor
      • Day 4: OLED Screen Basics >
        • Temperature Screen Display
        • IR Temp Sensor And Screen
      • Day 5 Friday: Finishing up/Show
    • 2020 STEM Camp >
      • Home (2020)
      • The 2020 Teachers
      • PreAcademy prep
      • Day 1 Monday: Basics and Lights >
        • Day 1 Supplement Activity
      • Day 2 Tuesday: Servos >
        • Day 2 (Suppliment Activities)
      • Day 3 Wednesday: Making an ECG/EKG! >
        • Day 3 (Supplement Page)
      • Day 4 Thursday: Controlling Servos with EMG >
        • Day 4 Supplemental Activity
      • Day 5 Friday: Finishing up/Show >
        • Unused lessons/content
    • 2018 Lessons >
      • Day 1: Basic Coding
      • Day 2: Motors
      • Day 3: Build Robot Claw
      • Day 4: Saving Positions
      • Day 5: Presentations
    • 2017 Camp >
      • Day 1 (Monday)
      • Day 2: Multicolor LED and Keypad
      • Day 3: Servo and Keypad
      • Day 4: Multiple Motors/Stepper Motor
      • Day 5: Presentations
    • Pictures archive for lessons (some graphic images)
  • Contact Us!