I am already using the A0 for a different sensor.. can I change the const int potPin to = A1???
//Constants:
const int ledPin = 9; //pin 9 has PWM funtion
const int potPin = A0; //pin A0 to read analog input
//Variables:
int value; //save analog value
void setup(){
//Input or output?
pinMode(ledPin, OUTPUT);
pinMode(potPin, INPUT); //Optional
}
void loop(){
value = analogRead(potPin); //Read and save analog value from potentiometer
value = map(value, 0, 1023, 0, 255); //Map value 0-1023 to 0-255 (PWM)
analogWrite(ledPin, value); //Send PWM value to led
delay(100); //Small delay
}
So do you have two sensors or pots coming to the same pin 0?
Is it that you only have one analog in? (the A0 syntax made me think of the esp8266 which sadly only has one) - I have a solution for that if that's the case.
And (without trying) can you even say int potPin = A0? Doesnt whats on the right of the equal sign have to be a number? I think this is syntactically correct but if you wrote int potPin=A2 then potPin would always equal zero or the compiler would throw an undefined error since the variable A2, with nothing in it, equals zero or at least the int version of an unfilled holder. (the int version of False =0)
Benny_Leonard, using 'const' for pin numbers is the preferred way. If you have an Arduino Uno, you can use A0 to A5. You don't have to set it as input, just analogRead() will work.
This is an example:
const int ledPin = 13;
const int potPin = A0;
const int lightSensorPin = A1;
const int currentSensorPin = A2;
Some say that using a 'const' or 'int' will be slow or will use memory, but the compiler should solve that. Often a "const int" does not require memory at all, since the compiler can use the number directly in the code.
Just to clarify (and not to add confusion), turns out you can say int potPin=A0; (or A2 or A3). And have it map to analog pin 0 (or pin 2 or 3)
Been doing this for years and have always defined my pins with numbers: int potPin=2; which also works. So hey, you learn something everyday. "A0 - A-whatever" must have predefined values - they are variables in themselves even without you defining them.
I agree with Koepel. const is the way to go in defining numbers that won't change - but as you say, you want to change the const potPin to A1. Once you define a const variable you cannot change it.
And with an UNO having only so much memory, as your sketches get bigger you may be desperate for bytes - so just grab the analog value on the fly as you need it without ever declaring that it's an input.
someVariable=analogRead(potPin);
ardshark:
Just to clarify (and not to add confusion), turns out you can say int potPin=A0; (or A2 or A3). And have it map to analog pin 0 (or pin 2 or 3)
Been doing this for years and have always defined my pins with numbers: int potPin=2; which also works. So hey, you learn something everyday. "A0 - A-whatever" must have predefined values - they are variables in themselves even without you defining them.
You can also use 14~19 instead of A0~A5 (on the 328p - A0~Awhatever start where the digital pins leave off - they're just #defines, analogRead() has logic in it that if it's passed a pin number greater than last digital pin, it subtracts the number of digital pins from it).
Of course, you probably shouldn't do that, since it makes the code harder to read and port to different boards later on!
You can use any of the analog pins with analogRead(). They all work the same, they just measure the voltage on a different pin.