Interfacing Potentiometer with Arduino (reading Analog value)
See Also: Arduino Development Platform
Code in action: (Video)
Example code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27,16,2); //LCD address 0x27
int v1=200;
int v2=400;
int v3=600;
int v4=800;
int v5=1000;
void setup()
{
lcd.init(); // initialize the lcd
lcd.backlight(); // lcd backlight ON
lcd.setCursor(0,0); // 0 row, 0 column
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
// Serial.begin(9600);
int potValue = analogRead(A0);
}
void loop()
{
lcd.clear();
int potValue = analogRead(A0);
// Serial.println(potValue);
lcd.setCursor(0,0);
lcd.print("Pot.Value:");
// lcd.setCursor(1,11);
lcd.print(potValue);
delay(100);
if (potValue<50)
LED(0,0,0,0,0);
else if (potValue<v1)
LED(1,0,0,0,0);
else if(potValue>=v1 && potValue<v2)
LED(1,1,0,0,0);
else if(potValue>=v2 && potValue<v3)
LED(1,1,1,0,0);
else if(potValue>=v3 && potValue<v4)
LED(1,1,1,1,0);
else
LED(1,1,1,1,1);
}
void LED(int n1,int n2,int n3,int n4,int n5)
{
if(n1==1)
digitalWrite(2,HIGH);
else if(n1==0)
digitalWrite(2,LOW);
if(n2==1)
digitalWrite(3,HIGH);
else if(n2==0)
digitalWrite(3,LOW);
if(n3==1)
digitalWrite(4,HIGH);
else if(n3==0)
digitalWrite(4,LOW);
if(n4==1)
digitalWrite(5,HIGH);
else if(n4==0)
digitalWrite(5,LOW);
if(n5==1)
digitalWrite(6,HIGH);
else if(n5==0)
digitalWrite(6,LOW);
}
No comments:
Post a Comment