doing more them one thing at a time

parts:
Octopus Voltage Divider Module
Arduino nano
5v solid state rely (dual channel only using one)

what i’m trying you do is make a adjustable low voltage cut off, but i’m not sure where to put more code to make the nano do multiple things. i am currently able to measure my voltage and i want something like

if value (analogInput = 3) is less the 10.00, then pin D3 = high (do i have to tell it else d3= low? or will it just be normally low)

using the high on pin D3 to control my relay

i am putting my relay in line with the supply voltage, so im thinking that by cutting off the supply once its off it will stay

i’m using this code to measure voltage:

/*
  Voltmeter
  Voltmeter base on voltage divider concept.
    Code based on: http://www.clarenceho.net:8123/blog/articles/2009/05/17/arduino-test-voltmeter
  Coded by: arduinoprojects101.com
*/



// variables for input pin and control LED
  int analogInput = 3;
  float vout = 0.0;
  float vin = 0.0;
  float R1 = 15000.0;    // !! resistance of R1 !!
  float R2 = 1000.0;     // !! resistance of R2 !!

// variable to store the value 
  int value = 0;

void setup(){
  // declaration of pin modes
  Serial.begin(9600);
}

void loop(){
  // read the value on analog input
  value = analogRead(analogInput);

  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2/(R1+R2));  
  Serial.println(vin);
  // sleep...
  delay(500);
}

thanks so much if it wasn’t for the forums help i would learn nothing! thank you :slight_smile:

do i have to tell it else d3= low? or will it just be normally low

It will normally (read that as always) be whatever state it was set to last time.

// variable to store the value 
  int value = 0;

What value? That variable goes out of scope before it is used.

You can only do one thing at a time. However, the time it takes to do a thing is VERY short, so it can appear as though the Arduino is doing many things "at once".

PaulS:

// variable to store the value 

int value = 0;



What value? That variable goes out of scope before it is used.

What do you mean? Isn't it declared with global scope?

Several things at the same time

UKHeliBob:
Several things at the same time

i did read that post (and it was good) but from just starting, and using code i did not write i’m not sure what its doing . i trying to figure out if there is a variable already being stored that i can use in an if statement .

vin = vout / (R2/(R1+R2));

is vin storing the voltage before sending to serial?

could i use:

if vin < 10.0 then D3 = high? (or something like that, knowing i will have to declare D3 as something that i need to read up on)

Yes, you can use any valid expression in an "if" statement. That expression can contain any variables that you have declared. It must follow the correct syntax for an expression.

What do you mean? Isn't it declared with global scope?

No. There are two variables with the same name - one global and one local to setup. Having two variables with the same name can best be described using a word that rhymes with stupid.

PaulS:
No. There are two variables with the same name - one global and one local to setup. Having two variables with the same name can best be described using a word that rhymes with stupid.

Did you download a different version? I'm looking at his setup() now and it only contains a serial.begin()

PaulS:
No. There are two variables with the same name - one global and one local to setup. Having two variables with the same name can best be described using a word that rhymes with stupid.

Wouldn't that be more like Narcissus, than Cupid? Can't think of anything else that rhymes. :slight_smile:

There is nothing wrong with using value. The problem is having a global one and a local one (in setup). The one in setup() is doing nothing. Delete it.

If the intent was to assign a value to the variable named value, in setup(), then delete the int keyword, so it is the global variable that is assigned a value.

ok thanks for all the help. it seems to be doing what i want, and this is what i used. since i again don't know what i'm doing this could very well be sloppy, but it works so far. this is turning on my led (pin 13)
when the voltage is below 10.0, and turning it off when its above 10.0. im thinking im going to have to set it to high to start so my circuit will work. so using this pin to pull in my relay's signal trigger should work. i still don't understand how "value" being declared twice as when i delete from the setup it gave me an error. PaulS if you want to dumb it down for me and show me what you mean i would appreciate it. either way thank you for your help.

i added:
const int ledPin = 13; // the number of the LED pin

if (vin < 10.0) digitalWrite(ledPin, HIGH);
if (vin > 10.0) digitalWrite(ledPin, LOW);

/*
  Voltmeter
  Voltmeter base on voltage divider concept.
    Code based on: http://www.clarenceho.net:8123/blog/articles/2009/05/17/arduino-test-voltmeter
  Coded by: arduinoprojects101.com
*/

const int ledPin =  13;      // the number of the LED pin

if (vin < 10.0) digitalWrite(ledPin, HIGH); 
  if (vin > 10.0) digitalWrite(ledPin, LOW);

// variables for input pin and control LED
  int analogInput = 3;
  float vout = 0.0;
  float vin = 0.0;
  float R1 = 15000.0;    // !! resistance of R1 !!
  float R2 = 1000.0;     // !! resistance of R2 !!

// variable to store the value 
  int value = 0;

void setup(){
  // declaration of pin modes
  Serial.begin(9600);
}

void loop(){
  // read the value on analog input
  value = analogRead(analogInput);

  vout = (value * 5.0) / 1024.0;
  vin = vout / (R2/(R1+R2));  
  Serial.println(vin);
  if (vin < 10.0) digitalWrite(ledPin, HIGH); 
  if (vin > 10.0) digitalWrite(ledPin, LOW);
  // sleep...
  delay(500);
}
if (vin < 10.0) digitalWrite(ledPin, HIGH); 
  if (vin > 10.0) digitalWrite(ledPin, LOW);

Code like this MUST be in a function.

PaulS:

if (vin < 10.0) digitalWrite(ledPin, HIGH); 

if (vin > 10.0) digitalWrite(ledPin, LOW);



Code like this MUST be in a function.

could you be more specific? to be coded proper, or to work correctly? i built the circuit and its working. no disrespect just trying to learn.

would this be more correct?

if (vin < 10.00)  
{
digitalWrite(ledPin, HIGH);
}
else 
{ 
digitalWrite(ledPin, LOW);
}

countryboyz:
could you be more specific? to be coded proper, or to work correctly? i built the circuit and its working. no disrespect just trying to learn.

No way will what you posted compile without errors. So please don't confuse everyone, I must assume the circuit is running a previous version of your code. I think you keep editing the code in the same post so nobody knows what to answer anymore.

Please upload the latest version you have to the Arduino and make a note of what it does or doesn't do. Re-post it here, and then we can help you.

PaulS:

if (vin < 10.0) digitalWrite(ledPin, HIGH); 

if (vin > 10.0) digitalWrite(ledPin, LOW);



Code like this MUST be in a function.

functions are setup(), loop(), and any others that you include or create. Code must be inside the {}.

aarg:
No way will what you posted compile without errors. So please don’t confuse everyone, I must assume the circuit is running a previous version of your code. I think you keep editing the code in the same post so nobody knows what to answer anymore.

Please upload the latest version you have to the Arduino and make a note of what it does or doesn’t do. Re-post it here, and then we can help you.

sorry i was not trying to confuse, just keep it simple with the part i was working on. here is the last full code i ran. this works, but i want to make sure im doing it proper.

with this code when the voltage is above 10.00 the built in led on pin 13 is off. if the voltage falls below 10.00 the light comes on.

/*
 Voltmeter
 Voltmeter base on voltage divider concept.
   Code based on: http://www.clarenceho.net:8123/blog/articles/2009/05/17/arduino-test-voltmeter
 Coded by: arduinoprojects101.com
*/

const int ledPin =  13;      // the number of the LED pin


// variables for input pin and control LED
 int analogInput = 3;
 float vout = 0.0;
 float vin = 0.0;
 float R1 = 15000.0;    // !! resistance of R1 !!
 float R2 = 1000.0;     // !! resistance of R2 !!

// variable to store the value 
 int value = 0;

void setup(){
 // declaration of pin modes
 Serial.begin(9600);
}

void loop(){
 // read the value on analog input
 value = analogRead(analogInput);

 vout = (value * 5.0) / 1024.0;
 vin = vout / (R2/(R1+R2));  
 Serial.println(vin);
if (vin < 10.00)  
{
digitalWrite(ledPin, HIGH);
}
else 
{ 
digitalWrite(ledPin, LOW);
}
 // sleep...
 delay(1000);
}