Hi
First let me say that I am pretty new to programming for the Arduino, but not in general.
I have run into a problem...
I have made a windows program that turns selected pin on the Arduino on or off.
The data I send is like... for digital pins and for analog pins.
For the digital pins I have a small rutine that convert the char data from the serial into int.
It takes each character and multiply and adds them.
This works great.
For the analog pins I have a problem...
I can read the individual characters and put them in an array... but I can´t use that in the analogwrite(A1, 0) as it require int as input and I need to call it with "A1" or "A7".
I have tried quiet a few of the search results in this forum, but I can´t get any to work.
Here are the code I am trying to get to work….
if (receivedChars[1] == 'A')
{
char buffer[2];
buffer[0] = receivedChars[2];
buffer[1] = receivedChars[3];
if (receivedChars[4] == 'O' && receivedChars[5] == 'N') { digitalWrite(buffer, HIGH); }
if (receivedChars[4] == 'O' && receivedChars[5] == 'F') { digitalWrite(buffer, LOW); }
}
Any help is much appreciated.
The analog inputs can also be accessed by their pin numbers. A0 is pin 14, A1 is 15 and so on. So analogWrite(14) is the same as analogWrite(A0). Maybe that will make it easier.
On an uno, A0 equals 14. So you can add an offset of 14 to the received pin number and use that number in your code
digitalWrite(number, HIGH);
If needed, you can simply print A0 etc for other boards to find the offset.
I have tried that but it seems to only work up to A5 and not for A6 & A7.
I have a nano.
You tried 20 for A6 and 21 for A7? When you Serial.print(A6) or Serial.print(A7), like sterretje suggested, what do you get?
analogWrite() works correctly with the numbers 0....
int analogRead(uint8_t pin)
{
uint8_t low, high;
#if defined(analogPinToChannel)
#if defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#endif
pin = analogPinToChannel(pin);
#elif defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
if (pin >= 54) pin -= 54; // allow for channel or pin numbers
#elif defined(__AVR_ATmega32U4__)
if (pin >= 18) pin -= 18; // allow for channel or pin numbers
#elif defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__)
if (pin >= 24) pin -= 24; // allow for channel or pin numbers
#else
if (pin >= 14) pin -= 14; // allow for channel or pin numbers
#endif
You can't use A6 and A7 as digital pins
Thanks for the replies.
I have figured it out now after reading your input
and this post...
https://forum.arduino.cc/index.php?topic=412850.0
which tell that A6 and A7 are only input.
Thank you for helping a beginner 
JonKlose:
which tell that A6 and A7 are only input.
A6 and A7 are only analog inputs. (on an UNO or similar)