Led doesn't turn off instantly.

Arduino Mega 2560
edit: using 180ohm resistors

When I apply 5v to A15 using the 5v on the Arduino board, LED turns ON instantly. But when I pull the 5v from A15, led turn OFF about half a second later.

  1. Is there a way to make it turn off instantly?
  2. How can I turn on multiple leds with one line of code? (ledred1, ledred2, HIGH) (ledred1 + ledred 2, HIGH)?

Here's my code. It's not finished yet.

int ledgr1 = 5;
int ledgr2 = 6;
int ledgr3 = 7;
int ledor1 = 8;
int ledor2 = 9;
int ledor3 = 10;
int ledred1 = 11;
int ledred2 = 12;
int ledred3 = 13;
unsigned long rpm;

void setup(){

pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(11, OUTPUT);
pinMode(10, OUTPUT);
pinMode(9, OUTPUT);
pinMode(8, OUTPUT);
pinMode(7, OUTPUT);
pinMode(6, OUTPUT);
pinMode(5, OUTPUT);

}
void loop(){
int sensorValue = analogRead(A15);
float voltage = sensorValue * (5.0 / 1023.0);
Serial.println(voltage);


{
  

}

if (voltage > 4) digitalWrite (ledred1, HIGH);
             
  
if (voltage < 4) digitalWrite (ledred1, LOW);

}
Serial.println(voltage);

What speed is your serial line running?
(Think carefully)

Please use code tags when posting code.

I honestly do not know. First time writing code.

I tried the ff. No difference.

void setup(){
Serial.begin(115200);

and

Serial.begin(19200);

Are you just leaving the input floating?

My guess is that the ADC value is not falling as quickly as you think it is.
Get the program to print the actual value returned by the ADC.

Also, you don't have any dead band between ON and OFF so it may be flickering so fast you can't see it.
Better to make it go on if > 4.0 and off if less than (say) 3.9

It would also make sense to get rid of the float stuff. 4v should be 818. So why not just say

if (sensorValue >= 818) {

...R

rmoving the float fixed it. Thanks

Robin2:
It would also make sense to get rid of the float stuff. 4v should be 818. So why not just say

...R

Removing the float or setting it to v>818 and v<817 or both fixed it. Thanks

Is there a way to make this work without using extra wires and diodes?

Each led turns on a X volts, then when it reaches Y volts, they should blink.

Code below doesn't work.

if (v > 506) digitalWrite (ledr1, HIGH);
if (v < 505 || v > 800) digitalWrite (ledr1, LOW);
if (v > 557) digitalWrite (ledr2, HIGH);
if (v < 556 || v > 800) digitalWrite (ledr2, LOW);
if (v > 609) digitalWrite (ledr3, HIGH);
if (v < 608 || v > 800) digitalWrite (ledr3, LOW);

if (v > 800) digitalWrite (ledr3 | ledr2 | ledr1 | ledo1 | ledo2 | ledo3 | ledg1 | ledg2 | ledg3, HIGH);
   delay(30);
   digitalWrite (ledr3 | ledr2 | ledr1 | ledo1 | ledo2 | ledo3 | ledg1 | ledg2 | ledg3, LOW);
   delay(30);

Are you just leaving the input floating?

This is an important question. If you attach 5V to an Arduino pin, and then simply remove that wire, the pin will actually STAY at 5V for a while, because there is no place for the the charge to go (essentially.) A TINY bit is used by the arduino, but not enough to return the pin to a lower voltage "instantly."

The circuitry to fix this doesn't need to be complex; a simple pull-down resistor will do it, but you have to pick the value somewhat carefully to avoid interfering with whatever analog source you will actually have driving the pin.

westfw:
This is an important question. If you attach 5V to an Arduino pin, and then simply remove that wire, the pin will actually STAY at 5V for a while, because there is no place for the the charge to go (essentially.) A TINY bit is used by the arduino, but not enough to return the pin to a lower voltage "instantly."

The circuitry to fix this doesn't need to be complex; a simple pull-down resistor will do it, but you have to pick the value somewhat carefully to avoid interfering with whatever analog source you will actually have driving the pin.

That explains it. Thanks

Another question. With nothing hooked up on A15, the leds with the lower volt setting light up. Those are set at 200,250,300 (roughly 1, 1.25 and 1.5v volts).

This is with a LM2596 hooked up to 12v battery stepped down to 9v.

ricekikr:
Code below doesn't work.

You can't write to several pins with a single digitalWrite().
If you really need to do that you need to learn about port manipulation - but I doubt if that is necessary.

Put your LED pin numbers into an array and you can update them all like this

for (n = 0; n < numLeds; n++) {
  digitalWrite(ledPinArray[n] = LOW;
}

You should also look at how the LED flashing is managed in the demo several things at a time

...R