#include <Servo.h> // including servo library
#include <LiquidCrystal.h> // including LCD display library
const int rs = PB11, en = PB10, d4 = PA4, d5 = PA3, d6 = PA2, d7 = PA1; // declaring pin names and pin numbers of lcd
LiquidCrystal lcd(rs, en, d4, d5, d6, d7); // setting lcd and its parameters
int servoPin = PA0; // declare and initialize pin for servo output PWM
int potPin = PA5; // potentiometer ADC input
int ledPin = PA6; // LED pin
Servo servo; // creating variable servo with datatype Servo
void setup()
{
lcd.begin(16, 2);
lcd.setCursor(2, 0);
lcd.print("PWM Tutorial");
lcd.setCursor(3, 1);
lcd.print("Using Servo");
delay(3000);
lcd.clear();
servo.attach(servoPin); // it connects pin PA0 with motor as control feedback by providing pulses
pinMode(ledPin, OUTPUT); // setting LED pin as output
}
void loop()
{
lcd.clear();
int angle;
int reading;
reading = analogRead(potPin);
angle = map(reading, 0, 1023, 0, 170); // map potentiometer reading to servo angle (0-170 degrees)
servo.write(angle);
int intensity = map(reading, 0, 1023, 0, 255); // map potentiometer reading to LED intensity (0-255)
analogWrite(ledPin, intensity);
if(reading<=125)
{ // set LED intensity
digitalWrite(ledPin,LOW);
}
else{
digitalWrite(ledPin,HIGH);
}
lcd.setCursor(0, 0);
lcd.print("Angle:");
lcd.print(angle);
lcd.setCursor(0, 1);
lcd.print("Intensity:");
lcd.print(intensity);
delay(100);
}