Arduino Forum

Using Arduino => Programming Questions => Topic started by: _Kiwi_ on Dec 07, 2015, 10:59 pm

Title: Simple code not doing what i hoped.
Post by: _Kiwi_ on Dec 07, 2015, 10:59 pm
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.
Title: Re: Simple code not doing what i hoped.
Post by: DrAzzy on Dec 07, 2015, 11:09 pm
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);
Title: Re: Simple code not doing what i hoped.
Post by: _Kiwi_ on Dec 07, 2015, 11:44 pm
I see. I shall try it.

Thank you
Title: Re: Simple code not doing what i hoped.
Post by: vaj4088 on Dec 08, 2015, 12:48 am
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!
Title: Re: Simple code not doing what i hoped.
Post by: OldSteve on Dec 08, 2015, 02:29 am
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.
Title: Re: Simple code not doing what i hoped.
Post by: Delta_G on Dec 08, 2015, 02:46 am
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.
Title: Re: Simple code not doing what i hoped.
Post by: MorganS on Dec 08, 2015, 02:52 am
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.
Title: Re: Simple code not doing what i hoped.
Post by: OldSteve on Dec 08, 2015, 02:55 am
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.