2

I have a Leonardo board and I want to set up my pins to input in one shot using a for loop but the problem is with the analog pins I can't see how to set up a mixed alphanumeric array it gives me some errors.

Code:

// including keyboard.h library for the keyboard functions
#include <Keyboard.h>


// setting Arrays for looping later
   int myIntPins[12] = {2,3,4,5,6,7,8,9,10,11,12,13};
   int myChar[12] = {97, 98, 99, 100, 101,102,103,104,105,106,107,108};
  int myChar2[6] = {109,110,111,112,113,114};
 myCharPins[6] = {"A0","A1","A2","A3","A4","A5"};
  int a;
  int x;
  int i;



void setup() {
  // put your setup code here, to run once:



 //Setting up Digital Pins 
for (int i=0; i <= 11; i=i+a){
  pinMode(myIntPins[i],INPUT);
  }

//pinMode(2, INPUT);
//pinMode(3, INPUT);
//pinMode(4, INPUT);
//pinMode(5, INPUT);
//pinMode(6, INPUT);
//pinMode(7, INPUT);
//pinMode(8, INPUT);
//pinMode(9, INPUT);
//pinMode(10, INPUT);
//pinMode(11, INPUT);
//pinMode(12, INPUT);
//pinMode(13, INPUT);

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

// setting up Analog Pins to Digital
for (int x=0; x <= 5; x=x+a){
  pinMode(myCharPins[x],INPUT);
  }
//pinMode(A0,INPUT);
//pinMode(A1,INPUT);
//pinMode(A2,INPUT);
//pinMode(A3,INPUT);
//pinMode(A4,INPUT);
//pinMode(A5,INPUT);

// starting keyboard 
Keyboard.begin();

}


void loop() {
  // put your main code here, to run repeatedly:

  //if pin 2 is pressed then send the a key/char  
 // if (digitalRead(2) == LOW) {Keyboard.write(97);}

// instead of doing an if one by one doing a loop all in one shot
// this loop for regular digital pins
a=1;
  for (int i=0; i <= 11; i=i+a){
    if (digitalRead(myIntPins[i]) == LOW){
      Keyboard.write(myChar[i]);
      }
      }
a=1;
  // this loop for the analog pins now working as digital pins
  for (int x=0; x <= 5; x=x+a){
    if (digitalRead(myCharPins[x]) == LOW){
      Keyboard.write(myChar2[x]);
      }
      }

  delay(10);

  }

Error:

    Arduino: 1.8.7 (Windows 10), Board: "Arduino Leonardo"

Mandar_Keyboard_Key_1:10:2: error: 'myCharPins' does not name a type

  myCharPins[6] = {"A0","A1","A2","A3","A4","A5"};

  ^

C:\Users\manue\OneDrive\Documents\Arduino\Mandar_Keyboard_Key_1\Mandar_Keyboard_Key_1.ino: In function 'void setup()':

Mandar_Keyboard_Key_1:45:11: error: 'myCharPins' was not declared in this scope

   pinMode(myCharPins[x],INPUT);

           ^

C:\Users\manue\OneDrive\Documents\Arduino\Mandar_Keyboard_Key_1\Mandar_Keyboard_Key_1.ino: In function 'void loop()':

Mandar_Keyboard_Key_1:77:21: error: 'myCharPins' was not declared in this scope

     if (digitalRead(myCharPins[x]) == LOW){

                     ^

exit status 1
'myCharPins' does not name a type

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

If I change my code I to this, I get a different error.

Code:

// including keyboard.h library for the keyboard functions
#include <Keyboard.h>


// setting Arrays for looping later
   int myIntPins[12] = {2,3,4,5,6,7,8,9,10,11,12,13};
   int myChar[12] = {97, 98, 99, 100, 101,102,103,104,105,106,107,108};
  int myChar2[6] = {109,110,111,112,113,114};
  char myCharPins[6] = {"A0","A1","A2","A3","A4","A5"};
  int a;
  int x;
  int i;



void setup() {
  // put your setup code here, to run once:



 //Setting up Digital Pins 
 a=1;
for (i=0; i <= 11; i+=a){pinMode(myIntPins[i],INPUT);}

//pinMode(2, INPUT);
//pinMode(3, INPUT);
//pinMode(4, INPUT);
//pinMode(5, INPUT);
//pinMode(6, INPUT);
//pinMode(7, INPUT);
//pinMode(8, INPUT);
//pinMode(9, INPUT);
//pinMode(10, INPUT);
//pinMode(11, INPUT);
//pinMode(12, INPUT);
//pinMode(13, INPUT);

//------------------------------------------------------------------------------
//------------------------------------------------------------------------------

// setting up Analog Pins to Digital
for (x=0; x <= 5; x+=a){pinMode(myCharPins[x],INPUT);}
//pinMode(A0,INPUT);
//pinMode(A1,INPUT);
//pinMode(A2,INPUT);
//pinMode(A3,INPUT);
//pinMode(A4,INPUT);
//pinMode(A5,INPUT);

// starting keyboard 
Keyboard.begin();

}


void loop() {
  // put your main code here, to run repeatedly:

  //if pin 2 is pressed then send the a key/char  
 // if (digitalRead(2) == LOW) {Keyboard.write(97);}

// instead of doing an if one by one doing a loop all in one shot
// this loop for regular digital pins
for (i=0; i <= 11; i+=a){if (digitalRead(myIntPins[i]) == LOW){Keyboard.write(myChar[i]);}}

  // this loop for the analog pins now working as digital pins
  for (x=0; x <= 5; x+=a){if (digitalRead(myCharPins[x]) == LOW){Keyboard.write(myChar2[x]);}}

  delay(10);

  }

Error:

    Arduino: 1.8.7 (Windows 10), Board: "Arduino Leonardo"

Mandar_Keyboard_Key_1:10:54: error: too many initializers for 'char [6]'

   char myCharPins[6] = {"A0","A1","A2","A3","A4","A5"};

                                                      ^

exit status 1
too many initializers for 'char [6]'

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.
3
  • If you show what the difference is between the two sets of code, it would be helpful Oct 20, 2018 at 6:16
  • why did you think it should be string "A0"? you have pinMode(A0, INPUT), not pinMode("A0", INPUT)
    – Juraj
    Oct 20, 2018 at 11:28
  • @Juraj Idk I thought that int was for integer numbers and a mix of letters and and number would be a string that's all don't blame me I didn't go to school for programming it's just a hobby that I like and I'm learning you know Oct 22, 2018 at 0:19

1 Answer 1

5

All of those "A1" "A2" pins have other numbers that go with them. The A1 or A2 is #defined in the core to be some number. You can just use A1 or A2 and it will work.

For example, try this line and see what it prints:

Serial.println(A0);

And for your pins, just put A0, A1, etc. in your int array and leave the quotes off. Those symbols are being replaced by numbers because they're defined constants.

int myIntPins[12] = {2,3,4,5,6,7,8,9,10,11,12,13, A0, A1, A2};
1
  • That worked just fine 😊 Thanks a lot!!! Oct 20, 2018 at 4:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.