Download presentation
Presentation is loading. Please wait.
Published byDouglas Singleton Modified over 6 years ago
1
Control a motors angular position with a flex sensor
2.2 Flex Control Control a motors angular position with a flex sensor
2
2.1 Learning Objectives Different types of Motors
Pulse Width Modulation (PWM) Flex and Bend Sensors Mapping and Converting a range of numbers
3
2.2 DC Motors DC (Directional Current) Motors are basically electromagnets that converts electrical power into mechanical power by electromagnetism Therefore depending on the direction of current flow you can change the direction the motor spins Speed is a result of how much voltage is applied
4
2.2 Servo Motors Servos are an electromagnetic motor with a built in potentiometer Servos move from 0 to 180 degrees Potentiometer is used to measure the angular position They are able to hold their position and exert a force. The amount of force a servo can exert is known as torque
5
2.2 Pulse Width Modulation
Servos receive a pulse every 20ms The length of that pulse determines how far the motor will turn For Example: 1.0ms pulse moves 0 degrees 1.5ms pulse moves 90 degrees 2.0ms pulse moves 180 degrees To maintain the position of the servo the pulse is continuously being sent
6
2.2 Flex Sensors Acts as a variable resistor when flexed
A voltage divider is used to detect the change in resistance Generally the more bend the more resistance is provided We then obtain a range of analog values depending on flexion
7
2.2 Breadboard Diagram Hardware: Servo Motor Flex Sensor 10k Resistor
8
2.2 Variable Initialization
Initialize the variables at the top of the program similarly to other C languages. #include <Servo.h> // Servo Object Servo myServo; // Flex Sensor Pin initialization const int FLEX_PIN = A0; // Servo Motor Pin initialization const int SERVO_PIN = 9; // Initialize variables for position int flexPos; int flexPosPrev; int servoPos; CODE
9
2.2 Setup We now initialize our servo motor and start its position at location 0. void setup() { // Start the Serial Monitor to display data Serial.begin(9600); flexPos = 0; flexPosPrev = 0; servoPos = 0; // Initialize what we are doing with each pin variables with "pinMode". myServo.attach(SERVO_PIN); } CODE
10
2.2 Loop In our loop now we want to map out the servos position according to the flex sensors bend resistance void loop() { flexPos = analogRead(FLEX_PIN); // Read the analog input of the flex sensor. // This if statement only changes the servos position of a difference greater than 30 is occurred. if (abs(flexPos - flexPosPrev) >= 30) { servoPos = map(flexPos, 600, 900, 0, 180); // use the flexPos to map out the new servoPos variable servoPos = constrain(servoPos, 0, 180); // Constrain the max and min values to 0 and 180. myServo.write(servoPos); // Write the servoPos variable to the Servo. This will start turning the Servo Serial.print(" "); Serial.print(flexPos); Serial.print(" "); Serial.println(servoPos); delay(50); flexPosPrev = flexPos; // Reset the flexPosPrev variable to be used again to keep the if statement going. } CODE
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.