Go Down

Topic: Simple code not doing what i hoped. (Read 510 times) previous topic - next topic

_Kiwi_

Being new to arduino, ive tried an analogue input. My aim was to turn on the built in LED when the input voltage at pin A0 reaches X amount. Ive written the code below, and tried varying values for the threshold figure but cannot get the led to turn on. I have 1 volt going into A0 at the moment but have tried grounding the input and giving it 5 volts to no avail. Im sure theres something simple in my understanding that ive missed but would be graetefull for some pointers.



Code: [Select]
int analogPin = A0; //pin that voltage is attached to
int ledPin = 13; //led pin
int threshold = 100; // arbitrary value

void setup() {
  // put your setup code here, to run once:
pinMode(ledPin, OUTPUT);//led pin as output pin
}

void loop() {
  int analogValue = analogRead(analogPin);//read value at analog imput pin
 
  if (analogValue < threshold) {ledPin = 1;}//if value at analog pin is less than arbitary value turn led on
  else {ledPin = 0;}//otherwise turn led off.

DrAzzy

You're setting the variable ledPin to 0 or 1.

"ledPin" is just a variable, which *was* equal to the pin number of the pin with the LED on it. After your code starts running, it's 0 or 1.

Nowhere do you actually write a value to the LED pin.
To write a value to a pin, use digitalWrite(pin, value) - so instead of ledPin=0 or ledPin=1, you want digitalWrite(ledPin,0); or digitalWrite(ledPin,1);
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

_Kiwi_


vaj4088

And you can avoid this issue in the future by replacing

Code: [Select]
int ledPin = 13; //led pin


with

Code: [Select]
const int ledPin = 13; //led pin


or even

Code: [Select]
const byte ledPin = 13; //led pin


Good Luck!

OldSteve

And you can avoid this issue in the future by replacing

Code: [Select]
int ledPin = 13; //led pin


with

Code: [Select]
const int ledPin = 13; //led pin


or even

Code: [Select]
const byte ledPin = 13; //led pin


Good Luck!
This post is misleading.
While it's a good idea to use 'const', that won't "avoid the issue".
A 'digitalWrite()' is still needed, as pointed out by DrAzzy.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Delta_G

This post is misleading.
While it's a good idea to use 'const', that won't "avoid the issue".
A 'digitalWrite()' is still needed, as pointed out by DrAzzy.

It would totally point out the issue at least because with const in there this line:

Code: [Select]
else {ledPin = 0;}//otherwise turn led off.

would throw an error pointing out the problem instead of compiling and not doing what the OP expected.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

MorganS

Well, it helps the compiler point out the error instead of saying "That's weird but I'll do what I'm told."

The optimization to use a byte is not necessary. The pin numbers in Arduino are ints so it will be upgraded to an int everywhere you use it. Byte is also unsigned so you can't use -1 to indicate 'no pin'.  The compiler will see that this const is used as a constant and it will put it directly into the binary without using up any SRAM storage, like a normal variable.
"The problem is in the code you didn't post."

OldSteve

It would totally point out the issue at least because with const in there this line:

Code: [Select]
else {ledPin = 0;}//otherwise turn led off.

would throw an error pointing out the problem instead of compiling and not doing what the OP expected.
Fair point. I hadn't looked at it that way.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Go Up