Hello all!
Please I am very new to this Arduino.
In the code bellow, I want the output become high when it gets to 3v and stays high unless its value is decreased to 2.5V
The Led comes on at 3V and goes off immediately it goes below 3V
int sensorPin = A0; // select the input pin for the potentiometer
int ledPin = 3; // select the pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
sensorValue = sensorValue * 5/1023 ; //its convert digital value back into voltage
If you want to use units of fractional Volts (like 2.5 V) then sensorValue must be a float variable, and you must do floating point calculations to end up with Volts, e.g.
float sensorValue = 0.0; // variable to store the value coming from the sensor
...
sensorValue = sensorValue * 5.0/1024.0 ; //convert digital value into voltage
Ok I have modified the code as directed and it worked, Being new to the Arduino things, I am curios to learn it. I have added LedPin_2 to the code and it also worked but I ran into trouble trying to add on/off delay to only first "LedPin_1"so that it will delay for the specified milliseconds before of or off.
The delay is working on the two Pins but if I remove the delay and put it only on LedPin_2, it worked on it without affecting LedPin_1
Please, how can I do it so the delay will function on a specified pin (Pins) only?
Thanks.
int sensorPin = A0; // select the input pin for the potentiometer
int sensorPin_2 = A1; // select the input pin_2 for the potentiometer
int LedPin_1 = 3; // select the pin for the LED
int ledPin_2 = 13; // select the pin for the LED_2
float sensorValue = 0.0; // variable to store the value coming from the sensor
void setup()
{
pinMode(LedPin_1, OUTPUT); // declare the ledPin as an OUTPUT:
pinMode(ledPin_2, OUTPUT); // declare the ledPin_2 as an OUTPUT:
}
void loop()
{
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
sensorValue = sensorValue * 5.0/1024.0 ; //convert digital value into voltage
if(sensorValue >= 3.0 )
{
delay (15000);
digitalWrite (LedPin_1, HIGH); // turn the LedPin_1 on
}
if(sensorValue <= 2.5 )
{
delay (15000);
digitalWrite(LedPin_1, LOW); // turn the LedPin_1 off
}
if(sensorValue >= 3.2 )
{
digitalWrite(ledPin_2, HIGH); // turn the ledPin_2 on
}
if(sensorValue <= 3.0 )
{
digitalWrite(ledPin_2, LOW); // turn the ledPin_2 off
}
}