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.begin(9600);
Serial.print("DC VOLTMETER");
// initialize digital pin LED_BUILTIN as an output.
pinMode(LED_BUILTIN, 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= ");
Serial.println(vin,2);
delay(500);
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
}