Hey guys.
I recently made a electronic lock operated by knocking in a specific pattern. The circuit uses a piezoelectric buzzer connected to A0 as a vibration sensor. I copied someone else's (open source) code as I do not have the expertise to program something like that.
On reading the code I noticed that the programmer defines the analog 0 pin as "sensorPin = 0". He uses only the number for the digital pins as well. And it works perfectly.
So, my question is that do we define analog and digital pins the same way, using only the number? What happens if there is a conflict?
Thanks.
AnalogRead knows what the analog PIN numbers are. If you try to use it with pin zero, it realizes that you mean A0 and adjusts it accordingly.
The digitalRead(), digitalWrite(), and pinMode() functions, on the other hand, will always use Pin 0 and never use Pin A0 when you use the value zero. Whenever you use the analog input pins (A0, A1, A2...) as digital pins, always use the name. In fact, it is best to always use the name whenever you are using those pins for any purpose.
Note: Don't use pinMode() with analogRead() or analogWrite(). Always use pinMode() with digitalRead() and digitalWrite(). You can change mode at any time but most commonly you will not need to set the mode more than once so you would do it in setup().