# LED ON/OFF

This program should turn on the LED when voltage on PB0 is between 1V and 1.3V but is not.
What is missing ?

``````int volt;
int i;
//int volt, avolt[50];
int LED =  PB14;
//int volt = 0;
#include <LiquidCrystal.h>
LiquidCrystal lcd(PA0, PA1, PA2, PA3, PA4, PA5);
void setup() {
pinMode(PA7, INPUT_ANALOG);
pinMode(PB14, OUTPUT);
lcd.begin(16, 2);
}

void loop() {
volt = (volt * 3.3) / 4095.0;
lcd.setCursor(0, 0);
lcd.print(volt);
delay(100);

for (int i = 0; i < 50; i++)
if (volt <= 1300 && volt >= 1000)
{
digitalWrite(PB14, HIGH);
LED = 1;

}
else
{
digitalWrite(PB14, LOW);
LED = 0;

}

}
``````

What is PB14?

Pin #

Which board are you compiling for? Other than the PB14 thing, it looks like you're trying to use AVR port/bit pin notation with Arduino functions. With 519 posts I have to believe you're more experienced than that.

pert:
Which board are you compiling for? Other than the PB14 thing, it looks like you're trying to use AVR port/bit pin notation with Arduino functions. With 519 posts I have to believe you're more experienced than that.

You haven't seen many of Ted's posts have you?

``````  for (int i = 0; i < 50; i++)
if (volt <= 1300 && volt >= 1000)
{
digitalWrite(PB14, HIGH);
LED = 1;

}
else
{
digitalWrite(PB14, LOW);
LED = 0;

}
``````

Since volt can’t change between iterations of this for loop, what is the point of writing the same value to the same pin 50 times over. Do you think you have to write it multiple times to make it “stick” or something. If you write the pin once then it will hold its value until you write something different to it. There’s no need to write the same value over and over to the same pin if there’s no possibility it could change.

That was a question about why you do it this way. So your reply should include an answer to that question, not just a claim that everyone’s comments are useless.

analogRead returns a value between 0 and 1024. You’re multiplying that by 3.3, then dividing by 4095. That will result in a maximuum value of 0.82 and a minimum value of 0.

I think you need to get rid of the division and collapse your read statement to:

``````float volt = analogRead(PB0) * 3.3;
``````

That should result in the scale you seem to be looking for, where 1000 == 1 volt with a 3.3v reference or 1000 == 1v with a 5v reference.

And as to the rest…

The for loop is completely unnecessary in this context, unless you’ve snipped out other code we’re not aware of.

And from a style perspective… please fix that if() statement…

``````if (volt >= 1000 && volt <= 1300)
``````

Doing it the other way makes it hard to understand your intent.

Delta_G:
You haven’t seen many of Ted’s posts have you?

don’t you think that there was reason for that ?

Once again, help is offered and you choose to focus on something else. What a waste of time trying to help you becomes. Why don't you focus on the part about your code?

pert:
Which board are you compiling for? Other than the PB14 thing, it looks like you're trying to use AVR port/bit pin notation with Arduino functions. With 519 posts I have to believe you're more experienced than that.

That is for ARM, the Voltmeter part is working ok just LED part of the program is not.

Delta_G:

``````  for (int i = 0; i < 50; i++)
``````

if (volt <= 1300 && volt >= 1000)
{
digitalWrite(PB14, HIGH);
LED = 1;

}
else
{
digitalWrite(PB14, LOW);
LED = 0;

}

``````

Since volt can't change between iterations of this for loop, what is the point of writing the same value to the same pin 50 times over. Do you think you have to write it multiple times to make it "stick" or something. If you write the pin once then it will hold its value until you write something different to it. There's no need to write the same value over and over to the same pin if there's no possibility it could change.

That was a question about why you do it this way. So your reply should include an answer to that question, not just a claim that everyone's comments are useless.
``````

If I will know the answer, I would not open this thread.

ted:
That is for ARM

That's a very general statement. There are many ARM microcontrollers that can be used with the Arduino IDE.

For all I know you are using some hardware package which does have the Arduino pin numbers defined using PBn notation. It's not unusual for beginners to assume they can use that pin notation with the Arduino functions for an AVR board and that certainly doesn't work as intended. As I said, it appears that you do have some experience and may just be working with a platform I'm not familiar with where that code is actually valid.

ted:
If I will know the answer, I would not open this thread.

You wrote it, I would assume that you would know why you wrote it. Or could at least say what you thought it would do. You don't know why you wrote it? You really are just writing random garbage and then expecting us to tell you why it doesn't work? Is that what's really going on?

``````float volt = analogRead(PB0) * 3.3; = incorrect voltage value on LCD display, for 1V on LCD is 3.44V
``````
``````if (volt >= 1000 && volt <= 1300) = I changed it, did not help
``````

pert:
That's a very general statement. There are many ARM microcontrollers that can be used with the Arduino IDE.

For all I know you are using some hardware package which does have the Arduino pin numbers defined using PBn notation. It's not unusual for beginners to assume they can use that pin notation with the Arduino functions for an AVR board and that certainly doesn't work as intended. As I said, it appears that you do have some experience and may just be working with a platform I'm not familiar with where that code is actually valid.

ted: