/*
Controlling a servo position using a potentiometer (variable resistor)
by Michal Rinott <http://people.interaction-ivrea.it/m.rinott>
modified on 8 Nov 2013
by Scott Fitzgerald
http://www.arduino.cc/en/Tutorial/Knob
*/
#include <Servo.h>
#include <LiquidCrystal_I2C.h>
#define I2C_ADDR 0x27
#define LCD_COLUMNS 20
#define LCD_LINES 2
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_LINES);
#define LED_BUILTIN 4
#define LED_ON digitalWrite(LED_BUILTIN, HIGH)
#define LED_OFF digitalWrite(LED_BUILTIN, LOW);
#define KAKI_POTENSIO A0
Servo myservo; // create servo object to control a servo
int potpin = 0; // analog pin used to connect the potentiometer
int val;
int progressBari = 0;
int i = 0;
byte progressBar[8] = {
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
B11111,
};
int pos = 0;
void setup(){
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println("16 M ainul mafazi aplikasi sensor, lcd, led, servo sensor potensiometer mulai....");
myservo.attach(9);
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.println(" 16 ");
lcd.setCursor(0,1);
lcd.println(" Ainul Mafazi ");
delay(5000);
lcd.setCursor(0,1);
lcd.println(" ");
lcd.begin(16, 2);
lcd.print("Posisi : ");
lcd.createChar(0, progressBar);
}
uint16_t data;
uint16_t pot_pos;
uint16_t servo_pos;
void loop(){
lcd.print("Posisi : ");
lcd.setCursor(0,1);
lcd.clear();
data = analogRead(KAKI_POTENSIO);
lcd.setCursor(12,0);
lcd.print(pot_pos);
progressBari=map(data, 0, 1023, 0, 17);
for (i=0; i<progressBari; i++)
{
lcd.setCursor(i, 1);
lcd.write(byte(0));
}
data = analogRead(KAKI_POTENSIO);
servo_pos = map(data, 0, 1023, 0, 180);
pot_pos = map(data, 0, 1023, 0, 90);
Serial.println("raw[" + String(data)+ "], degree[" + String(pot_pos)+"], [" + String(servo_pos)+"]");
if (pot_pos >=45)
{
LED_ON;
lcd.setCursor(0,0);
//lcd.println("LED_ON");
}
if (pot_pos < 45)
{
LED_OFF;
lcd.setCursor (0,0);
//lcd.println("LED_OFF");
}
{
val = analogRead(potpin); // reads the value of the potentiometer (value between 0 and 1023)
val = map(val, 0, 1023, 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
delay(15); // waits for the servo to get there
}
}