Leonardo analog to digital

Hi

I'm having a bit of problem how to add to this sketch to convert analog pins to read as digital, so after looking around managed to figure out sketch that works and I can have 14 buttons on sketch below but not sure what and where I have to write to change the analog pins to work as the rest of digital, A0-A5 if someone could help please.

Thank you

panel switch.txt (1.11 KB)

https://forum.arduino.cc/index.php?topic=28720.0

.

thanks for link but still can't get it to work :frowning:

Sounds like a wiring problem.
Start with 1 switch.

Pin0 and pin1 can't be used for buttons, because those pins are already used by the USB<>Serial chip.
Pin13, with an unbuffered LED, could be problematic too when used as input. Don't use.

Answer #1 on the link from post#1 is wrong for a Leonardo.
Leonardo pin numbering for the analogue pins A0-A5 seems to be 18-23, not 14-19.
Could use a pin array for the button pins. See example sketch.
Leo..

const byte buttonPins[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 18, 19, 20};
const byte buttonCount = 14;

void setup() {
  for (int i = 0; i < buttonCount; i++)  pinMode(buttonPins[i], INPUT_PULLUP);
}

void loop() {
}

Hi,
Welcome to the forum.

Please read the post at the start of any forum , entitled "How to use this Forum".
OR
http://forum.arduino.cc/index.php/topic,148850.0.html.
Then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks... Tom... :slight_smile:

Wawa:
Pin0 and pin1 can't be used for buttons, because those pins are already used by the USB<>Serial chip.

OP is using a Leonardo.

As OP does not seem to be using Serial1, there should not be an issue.

I do apologise for not posting the code correctly, I am not experience in using much of the arduino range a specially code part, most of my projects is from video and try and error, this code has been used for something else and it works for me from Pin 0 - 13, now this give me 14 digital inputs, for my new project I have spare leonardo board in drawer so wanted to utilize this but need 19 inputs, I know is possible to use the A0-A5 analog pins to use it as digital input for switches ect. (toggle/rotary), maybe a bit cheeky of me asking what I should and where write it to have this working but if I don't ask I won't know :wink: as for wiring there isn't much, lets say there will be 19 toggle switches, on/off nothing fancy, I know there are other boards that would be lot easier for me to use but this is just laying around so with your help if I can get this working would be great.

Also only using 1 switch to try the individual pins, one at the time

#include <Joystick.h>

Joystick_ Joystick;
int led = 13;


void setup() {
  

  // Initialize Button Pins
  pinMode(0, INPUT_PULLUP);
  pinMode(1, INPUT_PULLUP);
  pinMode(2, INPUT_PULLUP);
  pinMode(3, INPUT_PULLUP);
  pinMode(4, INPUT_PULLUP);
  pinMode(5, INPUT_PULLUP);
  pinMode(6, INPUT_PULLUP);
  pinMode(7, INPUT_PULLUP);
  pinMode(8, INPUT_PULLUP);
  pinMode(9, INPUT_PULLUP);
  pinMode(10, INPUT_PULLUP);
  pinMode(11, INPUT_PULLUP);
  pinMode(12, INPUT_PULLUP);
  pinMode(13, INPUT_PULLUP);
  
  // Initialize Joystick Library
  Joystick.begin();
}

// Constant that maps the physical pin to the joystick button.
const int pinToButtonMap = 0;

// Last state of the button
int lastButtonState[14] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0};

void loop() {
  
  // Read pin values
  for (int index = 0; index < 14; index++)
  {
    int currentButtonState = !digitalRead(index + pinToButtonMap);
    if (currentButtonState != lastButtonState[index])
    {
      Joystick.setButton(index, currentButtonState);
      lastButtonState[index] = currentButtonState;
    }
    
  }

  delay(50);
}