The goal here is to have an analog (adjustable) readanalogvoltage of 4.0-4.5V on (r3 uno board)A5pin and to then turn on an led on digital output pin 13.

How do I go about combining these two examples? (Baby steps please!)

void setup () { Serial.begin(9600); } void loop() { int led = 13; int sensorValue = analogRead(A5); float voltage = sensorValue * (5.0 / 1023.0); //can i replace sensorValue with voltage here? Serial.printIn(voltage); If (voltage > 4.0) digitalWrite (ledpin, HIGH); //can I use 4.0V here or do i need to 4*(5/1023)? Math of 5/1023 = 0.00488x4= 0.01952 which I have no idea what to do with..

// Pin 13 has an LED connected on most Arduino boards. // give it a name: int led = 13;

// the setup routine runs once when you press reset: void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); }

// the loop routine runs over and over again forever: void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second } /* ReadAnalogVoltage Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.

This example code is in the public domain. */

// the setup routine runs once when you press reset: void setup() { // initialize serial communication at 9600 bits per second: Serial.begin(9600); }

// the loop routine runs over and over again forever: void loop() { // read the input on analog pin 0: int sensorValue = analogRead(A0); // Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); // print out the value you read: Serial.println(voltage); }

Is the analog reading supposed to affect the LED in some way? If so, you have to define how you want it to change it. If not, just combine the setup functions, the loop functions and anything outside of them (global).

Yes I was hoping if the analog voltage is great than 4V It would make the led pin13 come on...

SuperNewb: Yes I was hoping if the analog voltage is great than 4V It would make the led pin13 come on...

Figure out what 4V means in terms of analogRead value (so you can avoid pointless floating point calculations) and write a simple if statement:

``````if (analogRead(somePin) > someValue)
digitalWrite(someLED, HIGH);
else
digitalWrite(someLED, LOW);
``````

If (voltage > 4.0) digitalWrite (ledpin, HIGH); //can I use 4.0V here or do i need to 4*(5/1023)? Math of 5/1023 = 0.00488x4= 0.01952 which I have no idea what to do with..

SuperNewb:
Yes I was hoping if the analog voltage is great than 4V It would make the led pin13 come on…

Do you mean that you want the LED to blink on/off when the voltage is greater than 4V ?

If so you will need to consider using a method that does not involve delay() because that blocks the execution of other code such as checking the value of the voltage. Look at the BinkWithoutDelay example to see how it can be done using the millis() function. Using that technique you can test the voltage as well as manage the LED flash timing.

``````float voltage = sensorValue * (5.0 / 1023.0)
``````

Set voltage to 4.0

``````4.0 = sensorValue * (5.0 / 1023.0)
``````

Solve for sensorValue.

Arrch: ``` float voltage = sensorValue * (5.0 / 1023.0) ```

Set voltage to 4.0

``````4.0 = sensorValue * (5.0 / 1023.0)
``````

Solve for sensorValue.

This where someone usually come in and posts that it really should be 5.0 / 1024.0 (not 1023.0). I'm never sure myself :D

Lefty

I want the LED to stay on between the preset voltages in this case it being 4v and 4.5V If my math is correct then sv @4v= 0.00122 and sv @ 4.5v= 0.00108?

retrolefty: This where someone usually come in and posts that it really should be 5.0 / 1024.0 (not 1023.0). I'm never sure myself :D

Lefty

It's almost a religious kinda thing, there are arguments for both. Here is a pretty good treatise on it, there is a 3-bit ADC represented that helps clear some things up. Neither is really perfect.

http://www.microchip.com/forums/m120602-print.aspx

If my math is correct then sv @4v= 0.00122 and sv @ 4.5v= 0.00108?

embarassing! I meant 1/0.0012= 833.33 which is the sensor voltage at 4v.
1/0.00108=925.92 for sensor voltage at 4.5v.
Hope thats the correct math this time lol.

If (sensorValue <= 833.33) digitalWrite (ledpin, LOW)
if (sensorValue >= 925.92) digitalWrite (ledpin, LOW)
If (sensorValue >= 833,33) digitalWrite (ledpin, HIGH)
If (sensorValue <= 925.92) digitalWrite (ledpin,HIGH)

Is there a simpler way of doing this to just say if sensorvalue (is in the range of 833.33 to 925.92) digitalWrite (ledpin, HIGH)
And it would just be off outside of this range? Or do I need to use 4 lines…

SuperNewb:
I want the LED to stay on between the preset voltages in this case it being 4v and 4.5V
If my math is correct then sv @4v= 0.00122 and sv @ 4.5v= 0.00108?

Then where does the LED Blink program come into it ?

SuperNewb:
embarassing! I meant 1/0.0012= 833.33 which is the sensor voltage at 4v.
1/0.00108=925.92 for sensor voltage at 4.5v.
Hope thats the correct math this time lol.

If (sensorValue <= 833.33) digitalWrite (ledpin, LOW)
if (sensorValue >= 925.92) digitalWrite (ledpin, LOW)
If (sensorValue >= 833,33) digitalWrite (ledpin, HIGH)
If (sensorValue <= 925.92) digitalWrite (ledpin,HIGH)

Is there a simpler way of doing this to just say if sensorvalue (is in the range of 833.33 to 925.92) digitalWrite (ledpin, HIGH)
And it would just be off outside of this range? Or do I need to use 4 lines…

Well first off, ditch the decimals. sensorValue is an int, it doesn’t recognize the .33 or .92.

Then, use the && operator:

``````If ( (condition 1 is true) && (condition 2 is true) )
{
// turn the led on
}
else
{
// turn it off
}
``````

I havn’t yet done the blink yet because there are instances where the voltage might fluctuate back and forth between the trigger on and off point for a sustained period of time (less than a couple of seconds i’d imagine) However I did not want it fluctuating back and forth 1000 times a second as that might blow the board… so still trying to decide on using the millis vs delay commands.

So far this is what I have:
void setup () {
Serial.begin(9600);
}
void loop() {
int led = 13;
If ( (sensorValue > 833) && (sensorValue <925) )
{
digitalWrite (ledpin, HIGH)
}
delay (50);
else
{
digitalWrite (ledpin, LOW)
}
delay (50);

What else do I need to compile this to make it work? For now I will attempt a 50 millisec delay not sure if it’s better than using milisec yet

You can't have that delay there, else has to be right after the if block { }. There are also two semicolons missing after the digitalWrites, the if is capitalized (should be lowercase). You should also set the ledpin as an output in setup.

Hehe your pretty good for a newbie rating! Thanks! Hope this is better. How can I stick the delays in there? Or am I limited to millisec instead?
void setup () {
Serial.begin(9600);
}
void loop() {
int led = 13;
int ledpin = output
if ( (sensorValue > 833) && (sensorValue <925) )
{
digitalWrite (ledpin, HIGH);
}
else
{
digitalWrite (ledpin, LOW);
}

Try it first with delay()

``````if ( (sensorValue > 833) && (sensorValue <925) )
//code to turn the LED on or off
}
``````

substitute

``````if ( (sensorValue > 833) && (sensorValue <925) )
//code to turn the LED on, delay(), then off, then delay()
}
else
{
//code to turn the LED off
}
``````

The LED will not respond quickly to sensor value changes because of the delays which can be fixed by using millis() but I suggest getting tht simple solution working first.

UkHeliBob, what i'm trying to accomplish is to have the led stay on steady while it's in the preset voltage range, not flash on and off. Will millis still work for this if set for super fast looping will i see the led as a steady on instead of a flashing on?

If you don't want the LED to flash then you don't need delay() or millis(). Why do you think that you need them ?

loop() runs continuously as its name suggests. Each time through loop() check the voltage on the sensor and turn the LED on or off as required. Turning it on when it is already on or vice versa will do no harm and will not be noticeable. What does your latest code do/not do that you want ?

The mention of the Blink example is what made me think that you wanted to blink the LED