Go Down

Topic: Using a voltage senor to trigger Blink LED Routine (Read 178 times) previous topic - next topic

Winovator

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
 

}

surveyranger

#1
Apr 11, 2019, 05:23 am Last Edit: Apr 11, 2019, 05:25 am by surveyranger
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.  

Code: [Select]

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.

neiklot

#2
Apr 12, 2019, 10:38 am Last Edit: Apr 12, 2019, 10:50 am by neiklot
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.




Go Up