Help with wiring and programming (beginner)

Hello, I would like to make a setup for a joystick with two potentiometers and 15 buttons. I have made a wiring. I would like to know if this will work? And I would like some help with the code. I am an Arduino amateur and I plan to use a nano that I already have.

I'm not putting the photo, I'm French, sorry for the translation

I made two sets of 7 buttons plus a single button

hi @tfrshop - welcome to the forum.

I can't read your drawing very well. It looks plausible.

What is the project? It makes a difference as to the selection of the arduino board.

You might like to try building it in the wokwi simulator. That's a great tool to confirm the basic operation of a hardware design and for some projects a way to get the software nearly perfect before you touch any real parts.

HTH

a7

The button wiring looks as if you have pairs of buttons wired in parallel, with no way to tell which of the two buttons have been pressed. That may be your intent, but seems unusual.

if there are enough pins, but another way to wire that many buttons is as a 4x4 matrix using just 8 pins

image

So yes, that's about it. I wanted to follow a wiring like this because I'm going to use an Arduino Nano and therefore I don't have a lot of output, especially with 17 buttons and 2 potentiometers.

Ah yes I see!! This wiring could be good I'll try it

I did the assembly on Wokwi. Does that seem more accurate to you? A 4x4 keyboard-style assembly with the addition of two potentiometers.

However, for the sketch.ino code section, do I need to add something?

1 Like

diagram.json

{
  "version": 1,
  "author": "TFRShop",
  "editor": "wokwi",
  "parts": [
    { "type": "wokwi-arduino-nano", "id": "nano", "top": -158.4, "left": -48.5, "attrs": {} },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn1",
      "top": 10.6,
      "left": 13.6,
      "rotate": 90,
      "attrs": { "color": "green", "xray": "1", "key": "4" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn2",
      "top": 45.8,
      "left": -19.2,
      "attrs": { "color": "green", "xray": "1", "key": "3" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn3",
      "top": 45.8,
      "left": 48,
      "attrs": { "color": "green", "xray": "1", "key": "5" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn4",
      "top": 77.8,
      "left": 13.6,
      "rotate": 90,
      "attrs": { "color": "green", "xray": "1", "key": "6" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn5",
      "top": 135.4,
      "left": 13.6,
      "rotate": 90,
      "attrs": { "color": "green", "xray": "1", "key": "7" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn6",
      "top": 170.6,
      "left": -19.2,
      "attrs": { "color": "green", "xray": "1", "key": "8" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn7",
      "top": 170.6,
      "left": 48,
      "attrs": { "color": "green", "xray": "1", "key": "9" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn8",
      "top": 202.6,
      "left": 13.6,
      "rotate": 90,
      "attrs": { "color": "green", "xray": "1", "key": "0" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn9",
      "top": 93.8,
      "left": -105.6,
      "attrs": { "color": "green", "xray": "1", "key": "2" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn10",
      "top": 26.6,
      "left": 134.4,
      "attrs": { "color": "green", "xray": "1", "key": "a" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn11",
      "top": 26.6,
      "left": 201.6,
      "attrs": { "color": "green", "xray": "1", "key": "b" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn12",
      "top": 93.8,
      "left": 201.6,
      "attrs": { "color": "green", "xray": "1", "key": "c" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn13",
      "top": 26.6,
      "left": 326.4,
      "attrs": { "color": "green", "xray": "1", "key": "d" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn14",
      "top": 74.6,
      "left": 326.4,
      "attrs": { "color": "green", "xray": "1", "key": "e" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn15",
      "top": 122.6,
      "left": 326.4,
      "attrs": { "color": "green", "xray": "1", "key": "f" }
    },
    {
      "type": "wokwi-pushbutton-6mm",
      "id": "btn16",
      "top": 55.4,
      "left": -105.6,
      "attrs": { "color": "green", "xray": "1", "key": "1" }
    },
    { "type": "wokwi-potentiometer", "id": "pot1", "top": -222.1, "left": 460.6, "attrs": {} },
    { "type": "wokwi-potentiometer", "id": "pot2", "top": -97.3, "left": 460.6, "attrs": {} }
  ],
  "connections": [
    [ "btn15:2.r", "btn14:2.r", "red", [ "h20", "v-47.6" ] ],
    [ "btn14:2.r", "btn13:2.r", "red", [ "h20", "v-47.6" ] ],
    [ "btn12:2.r", "btn15:2.r", "red", [ "h20", "v58", "h124.8", "v-28.8" ] ],
    [ "btn13:2.r", "nano:2", "red", [ "h20", "v-258.8", "h-307.2" ] ],
    [ "btn3:2.r", "btn1:1.l", "orange", [ "h10.4", "v-66.8", "h-48" ] ],
    [ "btn2:2.l", "btn1:1.l", "orange", [ "h-9.6", "v-66.8", "h67.2" ] ],
    [ "btn4:1.r", "btn2:2.l", "green", [ "v10.4", "h-67.2", "v-48" ] ],
    [ "btn3:2.r", "nano:3", "orange", [ "h20", "v-66.8", "h268.8", "v-201.6", "h-307.2" ] ],
    [ "btn7:2.r", "btn5:1.l", "green", [ "h10.4", "v-66.8", "h-48" ] ],
    [ "btn6:1.l", "btn5:1.l", "green", [ "h-9.6", "v-48", "h67.2" ] ],
    [ "btn8:1.r", "btn6:1.l", "green", [ "v10.4", "h-67.2", "v-67.2" ] ],
    [
      "btn7:2.r",
      "nano:4",
      "green",
      [ "h10.4", "v67.6", "h-124.8", "v-268.8", "h393.6", "v-182.4", "h-307.2" ]
    ],
    [ "btn11:2.r", "btn10:2.r", "blue", [ "h0.8", "v10", "h-67.2" ] ],
    [ "btn10:2.r", "btn9:2.r", "blue", [ "h0.8", "v230.8", "h-240.8" ] ],
    [ "btn9:2.r", "btn16:2.r", "blue", [ "h10.4", "v-38" ] ],
    [ "btn16:2.r", "nano:5", "blue", [ "h10.4", "v-95.6", "h393.6", "v-163.2", "h-307.2" ] ],
    [ "btn12:1.r", "btn15:1.r", "purple", [ "v9.6", "h29.6", "v9.6", "h96" ] ],
    [ "btn15:1.r", "btn14:1.r", "purple", [ "v-19.2", "h-37.6", "v-38.4", "h28.8" ] ],
    [ "btn14:1.r", "btn13:1.r", "purple", [ "v-19.2", "h-37.6", "v-38.4", "h28.8" ] ],
    [ "btn13:1.r", "nano:6", "purple", [ "v0", "h29.6", "v-249.6", "h-355.2" ] ],
    [ "btn11:1.r", "btn10:1.r", "yellow", [ "v-9.6", "h-66.4" ] ],
    [
      "btn10:1.r",
      "btn9:1.r",
      "yellow",
      [ "v-9.6", "h-37.6", "v38.4", "h28.8", "v211.2", "h-201.6", "v-172.8" ]
    ],
    [ "btn9:1.r", "btn16:1.r", "yellow", [ "v0", "h29.6", "v-38.4" ] ],
    [ "btn16:1.r", "nano:7", "yellow", [ "v0", "h29.6", "v-278.4", "h67.2" ] ],
    [ "btn2:1.l", "btn1:2.l", "cyan", [ "h0", "v-38.4" ] ],
    [ "btn3:1.r", "btn1:2.l", "cyan", [ "v-38.4", "h0.8" ] ],
    [ "btn2:1.l", "btn4:2.r", "cyan", [ "h0", "v57.6" ] ],
    [
      "btn3:1.r",
      "nano:8",
      "cyan",
      [ "v-9.6", "h-37.6", "v38.4", "h67.2", "v-105.6", "h-163.2", "v-182.4", "h67.2" ]
    ],
    [ "btn7:1.r", "btn5:2.l", "gray", [ "v-38.4", "h-8.8" ] ],
    [ "btn5:2.l", "btn6:2.l", "gray", [ "v0", "h-38.8" ] ],
    [ "btn6:2.l", "btn8:2.r", "gray", [ "h0", "v38.8" ] ],
    [
      "btn8:2.r",
      "nano:9",
      "gray",
      [ "v0.8", "h-10", "v-28.8", "h48", "v48", "h-172.8", "v-451.2", "h115.2" ]
    ],
    [ "nano:GND.2", "pot1:GND", "black", [ "v-76.8", "h355.2", "v105.6", "h57.6" ] ],
    [ "pot1:GND", "pot2:GND", "black", [ "v28.8", "h-57.6", "v134.4", "h57.6" ] ],
    [ "pot1:VCC", "nano:5V", "magenta", [ "v38.4", "h-96.8", "v48", "h-336" ] ],
    [ "pot1:VCC", "pot2:VCC", "magenta", [ "v38.4", "h47.2", "v124.8", "h-48" ] ],
    [ "pot1:SIG", "nano:A0", "white", [ "v19.2", "h-365.2", "v57.6", "h-134.4" ] ],
    [
      "pot2:SIG",
      "nano:A1",
      "#8f4814",
      [ "v48", "h-77.2", "v-144", "h-268.8", "v67.2", "h-144" ]
    ]
  ],
  "dependencies": {}
}

Um, yes. Code.

It starts with an empty setup() and loop(), just like a new sketch in the IDE.

Type or paste your code there. Hit the arrow in the diagram window to execute it.

Check out the wokwi docs for the LCD: select the part and hit the ? and you will get a page with some details and some examples.

a7

So I made my core, one part for the X & Y joystick, it works fine
and another for the push button (keyboard) part.

It works, I don't have any errors in the code, but when I simulate it, it doesn't give me the desired keys, meaning that no matter which button I press, I only get 4 different keys, not 16.

How do I fix this?

Do you see an error in the code?

I downloaded the keypad.h library.

/////////////////////////////////////////////////////////////////////
///////////////// BOUTON POUSSOIR ///////////////////////////////////
////////////////////////////////////////////////////////////////////


#include <Keypad.h>

const byte ROWS = 4; 
const byte COLS = 4; 

char hexaKeys[ROWS][COLS] = {
  {'1', '2', '3', 'c'},
  {'4', '5', '6', 'd'},
  {'7', '8', '9', 'e'},
  {'a', '0', 'b', 'f'}
};

byte rowPins[ROWS] = {9, 8, 7, 6}; 
byte colPins[COLS] = {5, 4, 3, 2}; 

Keypad customKeypad = Keypad(makeKeymap(hexaKeys), rowPins, colPins, ROWS, COLS); 

/////////////////////////////////////////////////////////////////////
///////////////// JOYSTICK//////////////////////////////////////////
////////////////////////////////////////////////////////////////////

const int SW_pin = 10; // Pin digital pour indiquer la position du bouton pousssoir
const int X_pin = 0; // Pin analogique pour l'axe X
const int Y_pin = 1; // Pin analogique pour l'axe Y

void setup() {
  pinMode(SW_pin, INPUT); // Configure SW comme une entré
  digitalWrite(SW_pin, HIGH); // Met de la tension dans SW pour dire au joystock que c'est une entré
  Serial.begin(9600); // Configure le moniteur série
}

void loop() {
  Serial.print("Coordonnée en X ");
  Serial.print(analogRead(X_pin)); // Valeur en X
  Serial.print("\n");
  Serial.print("Coordonnée en Y ");
  Serial.println(analogRead(Y_pin)); // Valeur en Y
  Serial.print("\n\n");
  delay(500); // // Une petite pause pour ne pas surcharger le moniteur série
  char customKey = customKeypad.getKey(); // partie bouton poussoir
  
  if (customKey){
    Serial.println(customKey);
}
}


Do you have the right library?
It seems that the library expects a resistor network on your buttons.

Are your buttons placed correctly?
It is not easy to see, but 2 sets of 2 pins are internally connected. A button push will connect both sets...
To be sure: always connect along diagonal...
Or check with multi-meter.

You set the pin as input and then you use it as output...

Did this code come with the library?

If you want decent help you should post a detailed description of the functionality that you want.

You should write this description with normal words by avoiding code.
Post the link to your WOKWI simulation. The simulation can be used as a shared base for helping you

+1.

After posting the link, post the code that is exactly the code form the simulation or say it is 100 percent identical to something you have posted.

Do any further work in the simulation on a copy, and do not touch the one you posted.

In that way, what we all see and perhaps say about what you linked will be talking about the same sketch and hardware.

Nothing is harder than talking about a moving target!

a7

Your wiring is completely wrong. You do not have the buttons connected in a grid, as shown in post#5, you instead have groups of four buttons connected in parallel, making it impossible to tell which of the four buttons have been pressed.

1 Like

@tfrshop - each switch in a scanned keypad must be connected across a row and a column.

There will be the electrical layout, see @gcjr's diagram in #5 above for how the circuit looks.

Put your switches on that grid.

In case it isn't obvious, any switch can physically be located wherever you want, but its wiring will always logically and electrically in a position to connect one row to one column.

a7

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.