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.
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
+- 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
Make your servo move back and forward just a certain amount of degrees.
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.
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);
}
}
}
#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);
}
}
}