搜索代码

Lesson 91: Controlling a Servo Motor Using a Potentiometer and Displaying the Angle on an LCD

Lesson 91: Controlling a Servo Motor Using a Potentiometer and Displaying the Angle on an LCD

This code (90-1) In this lesson we learn how control a servo motor using a potentiometer to move it to any angle between 0 to 180°. The angle is displayed on LCD screen. Full wiring diagram and wiring is explained with code.
438-Lesson 91: Controlling a servo motor using a potentiometer and displaying the angle on an LCD
语言: C++
/*
 Lesson 91
  * S05 Servo with Potentiometer and LCD1602-I2C
  * 
  Controlling a servo position using a potentiometer (variable resistor)
  by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
 
 Watch full instructions on YouTube https://youtu.be/E-H-4igygzA
  modified on 8 Nov 2013
  by Scott Fitzgerald
  http://www.arduino.cc/en/Tutorial/Knob
 
  Update by Ahmad Shamshiri on January 1, 2019 in Ajax, Ontario, Canada
 
 https://robojax.com?vid=robojax-servo-knob
 */
 
 #include <Servo.h>
 Servo myservo;  // create servo object to control a servo
 
 int potpin = A0;  // analog pin used to connect the potentiometer
 int servoPin = 3; // define a digital pin (with PWM) for servo. Can use only pin 3, 5, 6, 9, 10, 11
 int VCC2 = 8;// define extra VCC for potentiometer
 int val;    // variable to read the value from the analog pin
 
 #include <Wire.h> 
 #include <LiquidCrystal_I2C.h>
 
 // Set the LCD address to 0x27 for a 16 chars and 2 line display
 LiquidCrystal_I2C lcd(0x3F, 16, 2);
 int VCC3 = 4;// define extra VCC for LCD
 int degPos;// degree symbol position
 
 void setup() {
   //
   Serial.begin(9600);          //  setup serial
   myservo.attach(servoPin);  // attaches the servo on pin 3 to the servo object
 
   pinMode(VCC2, OUTPUT);// set pin 8 as output
   digitalWrite(VCC2, HIGH);// set pin 8 HIGH so it acts as 5V pin
 
   // lines bellow are for LCD
   pinMode(VCC3, OUTPUT);// set pin 4 as output
   digitalWrite(VCC3, HIGH);// set pin 4 HIGH so it acts as 5V pin  
   lcd.begin();  // initialize the LCD, 
   // Turn on the backlight and print a message.
   lcd.backlight();    
 }
 
 void loop() {
   
   val = analogRead(potpin)/5;            // reads the value of the potentiometer (value between 0 and 1023)
   val = map(val, 0, 1023/5, 0, 180);     // scale it to use it with the servo (value between 0 and 180)
   myservo.write(val);                  // sets the servo position according to the scaled value
   Serial.print(val);
   Serial.println();
 
   lcd.clear();
   lcd.print("Servo with Pot.");
   lcd.setCursor (0,1); // go to start of 2nd line
   lcd.print("Angle:");  
   lcd.setCursor (7,1); // go to 7 char of of 2nd line
   lcd.print(val);// print angle 
 
   if(val >99)
   {
     degPos =10;
   }else if(val >9)
   {
     degPos =9;
   }else
   {
     degPos =8;
   }
   lcd.setCursor (degPos,1); // go to 11 char of of 2nd line  
   lcd.print((char)223);// degree symbol
     
   delay(500);                           // waits for the servo to get there
 }

资源与参考

文件📁

没有可用的文件。