You are on page 1of 1

The following program describes how to write an arduino sketch to read an

analog value then use it to control the brightness of an LED :

int readPin=A0;
int ledPin=12;
int waitT=750;
float Vr=0.0;
String mess1="Vr is greater than 2.5v and less than 3.5";
String mess2="Vr is greater than 3.5";
String mess3="Vr is less than 2.5v";

void setup() {
// put your setup code here, to run once:
pinMode(readPin,INPUT);
pinMode(ledPin,OUTPUT);
Serial.begin(9600);

void loop() {
// put your main code here, to run repeatedly:
Vr=float(analogRead(readPin))*5.0/1023.0;
Serial.println(Vr);

if (Vr>2.5 && Vr<3.5)


{
Serial.println(mess1);
digitalWrite(ledPin, HIGH);
}
else if(Vr>3.5)
{
Serial.println(mess2);
digitalWrite(ledPin,LOW);
}
else Serial.println(mess3);

delay(waitT);

I hope it is helpful to beginners in arduino.

You might also like