Instantly share code, notes, and snippets.
Created
June 18, 2016 00:56
-
Save john212/5ba774a7c15de23ffb89c0935ba256be to your computer and use it in GitHub Desktop.
Arduino Servo Example - Using Potentiometer To Vary Servo or Pushbutton To Set To Fixed Position
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/************************************************** | |
Udemy Arduino Step-by-Step Course | |
Section 5 Motors Lecture 51 Servo Motors Part 2 | |
Quiz Servo With 3 Buttons (not graded, not for credit, fun only:-) | |
by J. Cantlin | |
June 17, 2016 | |
***************************************************/ | |
/************************************************** | |
Description of Program | |
A small servo motor is setup to be contolled by a | |
10k potentiomer. Moving the pot with move the servo | |
throughtout its range of motion (approximately 170 degrees). | |
The servo will also be set to fix positions of 60, 90 and 180 | |
degrees when pushbuttons 1, 2, or 3 are pressed respectively. | |
A 1000uF capacitor is placed across the +5vdc to Ground | |
to protect the Arduino from the high starting current | |
when the servo power is first applied. | |
***************************************************/ | |
/************************************************** | |
Summary of Arduino Uno Analog Pins Used: | |
A0 = 10k potentiometer (wiper arm = center pin) | |
A1 = | |
A2 = | |
A3 = | |
A4 = | |
A5 = | |
***************************************************/ | |
/************************************************** | |
Summary of Arduino Uno Digital Pins Used: | |
00 = //Avoid RX pin - used for serial port | |
01 = //Avoid TX pin - used for serial port | |
02 = button 2 (between 10k resistor and ground) | |
03 = button 2 (between 10k resistor and ground) | |
04 = button 3 (between 10k resistor and ground) | |
05 = | |
06 = | |
07 = | |
08 = | |
09 = servo control signal (orange wire for this servo) | |
10 = | |
11 = | |
***************************************************/ | |
/********************************************************************** | |
VarSpeedServo.h Arduino Library: | |
The VarSpeedServo.h Arduino library allows the use of up to 8 servos moving | |
asynchronously (because it uses interrupts) to a position specified in the | |
write(position) function. In addition, you can optionally set the speed of | |
a move (0 to 255), or set a wait flag "true" to have the program wait until | |
the servo move is complete before proceeding. You can create sequences | |
of moves that run asynchronously. Format: write(pos,speed,true) | |
This code is an adaptation of the standard Arduino Servo.h library. | |
Reference: | |
https://github.com/netlabtoolkit/VarSpeedServo | |
***********************************************************************/ | |
#include <VarSpeedServo.h> | |
VarSpeedServo myservo; //create a new servo object to control a servo | |
// Analog Pin Assignments | |
const int potPin = 0; //analog pin used to connect the potentiometer | |
// Digital Pin Assignments | |
const int servoPin = 9; // the digital pin used for the servo | |
const int button1Pin = 2; // the digital pin used for the button 1 | |
const int button2Pin = 3; // the digital pin used for the button 2 | |
const int button3Pin = 4; // the digital pin used for the button 3 | |
// Initialize Global Variables | |
int valPot; // variable to read the value from the analog pin | |
int button1State = 0; // button status variable, initial state = 0 = not pressed | |
int button2State = 0; // button status variable, initial state = 0 = not pressed | |
int button3State = 0; // button status variable, initial state = 0 = not pressed | |
void setup() | |
{//****start setup function**** | |
//Serial.begin(9600); //set the serial port to 9600 baud if monitor is used | |
myservo.attach(servoPin); //attaches the new servo object to the assigned digital pin | |
// initialize the pushbutton pins as inputs | |
pinMode(button1Pin, INPUT); | |
pinMode(button2Pin, INPUT); | |
pinMode(button3Pin, INPUT); | |
}//****endof setup function**** | |
void loop() | |
{//****start infinite loop**** | |
valPot = analogRead(potPin); //read the potentiometer analog value | |
delay(10); // short delay to allow readings to stabilize | |
valPot = map(valPot, 0, 1023, 0, 180); // scale potVal to servo (0 to 180 degrees) | |
myservo.write(valPot); // sets servo position according to the scaled value | |
// read the state of the pushbuttons - HIGH = pushed | |
button1State = digitalRead(button1Pin); | |
button2State = digitalRead(button2Pin); | |
button3State = digitalRead(button3Pin); | |
if(button1State == HIGH) | |
{//****start button1 pressed loop**** | |
myservo.write(60); //move to 60% at full speed, wait until at 60% | |
}//****endof button1 pressed loop**** | |
if(button2State == HIGH) | |
{//****start button2 pressed loop**** | |
myservo.write(90); //move to 90% at full speed, wait until at 90% | |
}//****endof button2 pressed loop**** | |
if(button3State == HIGH) | |
{//****start button3 pressed loop**** | |
myservo.write(180); //move to 180% at full speed, wait until at 180% | |
}//****endof button3 pressed loop**** | |
/********************************************************************/ | |
// Print data used for testing and debugging - comment out as needed. | |
// Warning: using the serial port may conflict with servo commands. | |
// Serial.print("Potentiometer Reading: "); | |
// Serial.println(valPot, DEC); // print the potentiometer reading, ln for new line | |
// Serial.print("Button 1 Reading: "); | |
// Serial.println(button1State); // print the potentiometer reading, ln for new line | |
// Serial.print("Button 2 Reading: "); | |
// Serial.println(button2State); // print the potentiometer reading, ln for new line | |
// Serial.print("Button 3 Reading: "); | |
// Serial.println(button3State); // print the potentiometer reading, ln for new line | |
// delay(100); // waitfor next reading | |
/********************************************************************/ | |
}//****endof infinite loop**** | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment