Using a voltage senor to trigger Blink LED Routine

Hello All, It's my first attempt at programming Arduino and I am very novice, so I apologize for the dumb questions I am about to ask.

I have an external voltage sensor, connected to my Mega 2560. I am simply trying to use the voltage sensor to start the Blink programme.

The voltage from the sensor is not constant. I just need it to trigger the onboard LED

I noticed that I should not have multiple delay statements as my serial monitor appears to be getting its delay from the delay statement for the LED.

Anyways, I would be most grateful if someone could have a quick look and fix/explain what I am doing wrong.

In a nutshell, this is what I am trying to achieve:

IF any voltage is detected by the voltage sensor, THEN start the blink programme!

Please see my sketch below:

int analogInput = A1;
float vout = 0.0;
float vin = 0.0;
float R1 = 818000.0; //
float R2 = 179150.0; //
int value = 0;
void setup(){
pinMode(analogInput, INPUT);
Serial.print("DC VOLTMETER");
// initialize digital pin LED_BUILTIN as an output.

void loop(){
// read the value at analog input
value = analogRead(analogInput);
vout = (value * 5.0) / 1024.0; // see text
vin = vout / (R2/(R1+R2));
// the loop function runs over and over again forever

Serial.print("INPUT V= ");

digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
delay(5000); // wait for a second
digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
delay(2000); // wait for a second


First recommendation is to check out the sticky at the top of the forum for how to post code...and other very helpful stuff like posting pictures of schematics and how to ask good questions.

Second, what have you tried? What did it do right/wrong?

Third, I don't see an if statement in there.

value = analogRead(analogInput);   // check for voltage on A1 
if (value > 0) { // 0-1023 value possible
   serial print stuff;
   do the blink sketch;
else {
   serial print other stuff;

Consider making the number bigger than 0 to avoid false positives.

There's no "blink sketch" that you can run, that would be a function: there's only one sketch. And somewhere in the "if..else" it needs to make sure the led is off when it's not blinking.

And apart from that, if you want any decent level of response, you really need to look at blink without delay.

Take all the blink without delay stuff in the example into a function say doTheBlinking, and call that from loop(), if the reading is > 0 (or >25 say, as surveyranger suggests).

In the else of the if, explicitly turn the led off, since it might get cancelled in the on half of its cycle and be left on.