1:00 am in the night of a summer holidays, bored as hell with nothing to do, not in the mood of making something without a led & lights.
The components of this project include:
- 3 Led's(Red, Yellow, Green)
- 330 Ohm resistors x 3
- A potentiometer
- A few jumper m-m cables.
Potentiometer is being used to regulate the amount of voltage and depending upon that amount a specific Led lights up.
If voltage is < or = 3volts; Green Led Lights up.
If voltage is < 4 volts and > 3 volts; Yellow Led Lights up.'
If voltage is < or = 5volts and > 4 volts; Red Led Lights up.
Real Life ApplicationsCreate a alert system for a specific current where if voltage is higher than a specific amount the application burns up, In this application upon the voltage being surpasses a red led glows up & if it close to the danger amount but more than the average amount a yellow led glows up. Upon current being the right amount, green led glows up.
CODE BREAKDOWNint rPin = 11 ;
int yPin = 12 ;
int gPin = 13 ;
Setting these variable of digital pins with a boar digital pin value.
You can use any Digitalpin of your convenience
int readPin = A0;
float Val ;
float readVal ;
Setting up a new variable **readPin** for a value of analog pin that will be used to later with analogRead() command.
Giving null values to variables Val & readVal so that we can set their values later.
String myString = "The Potentiometer Voltage is : " ;
int wait = 1000;
Setting up a new string variable myString that will be used later for formatting our serialprint() outputs.
Also made a delay variable wait with a value 0f 1000 milliseconds or 1 second.
You can name these variables whatever you want, But they should make sense to you
pinMode(rPin,OUTPUT);
pinMode(yPin,OUTPUT);
pinMode(gPin,OUTPUT);
Declaring those variable we had created earlier for digital pins as output.
pinMode(readPin,INPUT);
Serial.begin(9600);
}
Declaring that we will use the analog pin with variable readPin as INPUT.
We also set the baud rate of serial monitor as 9600.
readVal = analogRead(readPin);
Val = (5./1023.)*readVal;
Giving the value of readVal as the reading of analogRead() from analog pin readPin we had set.
we would have read the value from 0 to 1023 or 1024 numbers.
0 = 0 volts
1023 = 5 volts
as it is a 10 bit value (2^10)
To read this in volts we would use our other variable Val we had setup, since we know that 5 volts = 1023 so 1 volts = 5/1023.
now that we know the value of 1v we need to multiply it with readVal value we get in order to get the value of voltage.
if (Val <= 3.00) {
digitalWrite(gPin,HIGH);
digitalWrite(yPin,LOW);
digitalWrite(rPin,LOW);
}
Here we are using condition statements.
If val is less than or equal to 3, gPin or the Green Pin will LIGHT UP.
if (Val < 4.00 && Val > 3.00) {
digitalWrite(gPin,LOW);
digitalWrite(yPin,HIGH);
digitalWrite(rPin,LOW);
}
Here if Val is less than 4 and greater than 3, yPin or YellowPin will LIGHT UP.
if (Val > 4.00 && Val<= 5.00) {
digitalWrite(gPin,LOW);
digitalWrite(yPin,LOW);
digitalWrite(rPin,HIGH);
}
Here if Val is > 5 and < or = 5 then rPin or Red Pin glows up.
Serial.print(myString);
Serial.println(Val);
delay(wait);
}
Here we are printing our previous string we had set up on our serial monitor just to format it, cause OCD hurts.
After that we are printing the Val values to cross-check our programme with leds.
Atlast, we use the good old delay programme to give our eyes mercyyyyyy!!!!.
The last few programmes didn't have an inline cause for some reason, they would not work anymore. I am sorry for the inconvenience caused.SERIAL MONITOR
Comments
Please log in or sign up to comment.