Does this code work for the given schematic

hello i am not a programmer and i am trying to build FA-18 UFC for Digital Combat Simulator and i built it and its ready, I tested the code for matrix for 7x7 for a total of 49 push buttons and i am trying to add 2 ON-ON toggle switches to the Arduino Pro Micro but i am afraid to upload the below code as it was modified by me to include the 2 on-on toggle switches.

kindly help me please.

#include <Keypad.h>
#include <Joystick.h>

#define ENABLE_PULLUPS 
#define NUMBUTTONS 49
#define NUMROWS 7
#define NUMCOLS 7
#define joyButton49 A2
#define joyButton50 A3


byte buttons[NUMROWS][NUMCOLS] = {
  {0,1,2,3,4,5,6},
  {7,8,9,10,11,12,13},
  {14,15,16,17,18,19,20},
  {21,22,23,24,25,26,27},
  {28,29,30,31,32,33,34},
  {35,36,37,38,39,40,41},
  {42,43,44,45,46,47,48}

};
byte rows[] = {2,3,4,5,6,7,8};
const int rowCount = sizeof(rows)/sizeof(rows[0]);

// JP2 and JP3 are outputs
byte cols[] = {9,10,16,14,15,A0,A1};
const int colCount = sizeof(cols)/sizeof(cols[0]);

byte keys[colCount][rowCount];

void setup() {
  Serial.begin(115200);

  for(int x=0; x<rowCount; x++) {
    Serial.print(rows[x]); Serial.println(" as input");
    pinMode(rows[x], INPUT);
  }

  for (int x=0; x<colCount; x++) {
    Serial.print(cols[x]); Serial.println(" as input-pullup");
    pinMode(cols[x], INPUT_PULLUP);
  }
    
}

void readMatrix() {
  // iterate the columns
  for (int colIndex=0; colIndex < colCount; colIndex++) {
    // col: set to output to low
    byte curCol = cols[colIndex];
    pinMode(curCol, OUTPUT);
    digitalWrite(curCol, LOW);

    // row: interate through the rows
    for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
      byte rowCol = rows[rowIndex];
      pinMode(rowCol, INPUT_PULLUP);
      keys[colIndex][rowIndex] = digitalRead(rowCol);
      pinMode(rowCol, INPUT);
    }
    // disable the column
    pinMode(curCol, INPUT);
  }
}

void printMatrix() {
  for (int rowIndex=0; rowIndex < rowCount; rowIndex++) {
    if (rowIndex < 10)
      Serial.print(F("0"));
    Serial.print(rowIndex); Serial.print(F(": "));

    for (int colIndex=0; colIndex < colCount; colIndex++) { 
      Serial.print(keys[colIndex][rowIndex]);
      if (colIndex < colCount)
        Serial.print(F(", "));
    } 
    Serial.println("");
  }
  Serial.println("");
}

  int lastButton49State = 0;
  int lastButton50State = 0;

Joystick_ Joystick(0x12, JOYSTICK_TYPE_JOYSTICK, 2, 0,true,true,false,false,false,true,false,true,false,false,false);

  
void loop() {
  readMatrix();
  if (Serial.read()=='!')
    printMatrix();
    
int currentButton49State = !digitalRead(joyButton49);
    if (currentButton49State != lastButton49State){
      Joystick.setButton(0, currentButton49State);
       lastButton49State = currentButton49State;
    }
int currentButton50State = !digitalRead(joyButton50);
    if (currentButton50State != lastButton50State){
    Joystick.setButton(1, currentButton50State);
    lastButton50State = currentButton50State;
  }
  }

Rule of thumb - Do not use TX, RX for inputs - you need those for Serial Monitor, for debugging your code.
Alternative - you easily can use one analog in to monitor each switch, if you have a few resistors available. The switches could even be ON-OFF-ON toggles, if you want. Ask for more info, if interested.

You can simulate the circuit without joystick functions here.

i am intrested and i have a few 33 ohm resistors

getting this error and i need to upload to po micro not uno

I'm talking about simulate buttons matrix. Joystick functions will be important just to pass the commands to USB port.

You can simulate the circuit with joystick functions…

… by simply using two slide faders in place of the potentiometers on a joystick.

a7

matrix is working fine but the toggle ones are not in that emulator

You can't use 0 for a Keypad key code. It is reserved for "NO_KEY" to indicate that no new key has been pressed.

Nevermind. I see now that you include the Keypad library but don't actually use it.

As @camsysca told RX-TX is used to serial port, so if you are using it to debug it won't work.

0 there is for button 1 and it is working like that

The matrix is working fine my main problem is for adding the 2 toggle buttons to the board as shown in the schematic (i connected the buttons to First toggle button{A2,Gnd,A3}& Second Toggle{removed it }) and its code

Please don't edit my message while quoting.

1 Like

It's labeled as an Arduino Pro Micro so it has native USB. The hardware serial pins (Serial1) are not being used.

1 Like

Thanks for the correction!

This detail is not clear to me in the specification.

What I can find is:

Serial: 0 (RX) and 1 (TX). Used to receive (RX) and transmit (TX) TTL serial data using the ATmega32U4 hardware serial capability. Note that on the Micro, the Serial class refers to USB (CDC) communication; for TTL serial on pins 0 and 1, use the Serial1 class.

The Micro has a number of facilities for communicating with a computer, another board of the Arduino & Genuino family, or other microcontrollers. The 32U4 provides UART TTL (5V) serial communication, which is available on digital pins 0 (RX) and 1 (TX). The ATmega32U4 also allows for serial (CDC) communication over USB and appears as a virtual com port to software on the computer. The chip also acts as a full speed USB 2.0 device, using standard USB COM drivers. On Windows, a .inf file is required . The Arduino Software (IDE) includes a serial monitor which allows simple textual data to be sent to and from the board. The RX and TX LEDs on the board will flash when data is being transmitted via the USB connection to the computer (but not for serial communication on pins 0 and 1).

EDIT: The mention for Serial1 is the last part of the first quote. :frowning:

In Nano 33 BLE documentation it's more clear.

UART is the communication protocol we use to communicate the PC to the board through the USB cable. In some older boards, TX and RX pins are used for communication with the computer, which means connecting anything to these pins can interfere with that communication, including causing failed uploads to the board. This is not the case for the Nano or MKR families, since these ones have two separate channels, using Serial for the communication with the computer and Serial1 for the communication with any other device through UART.

You'll want something in the 1K to 10K range, I'm afraid, but here's the sketch:

image

Along with this, you'll need a few lines of code to determine which of 3 modes is selected, of course.
For a 10-bit analog in like the Nano has, something like:

byte checkswitch(int pin) {                         //analog pin # as variable
  byte mode = 1;                                       //default to middle
  unsigned int Val = analogRead(pin);    //could do a 2nd read here if noisy
  if (Val < 340) mode = 0;                         //check for "down"
  if (Val > 680) mode = 2;                         //check for "up"
  return(mode);                                        //we're done
}

Throw that in a subroutine, return the mode, and you're done. Add some pretty frills if you want.
Untested, but it shows the intent.
<edit: fixed logic, changed val to Val, added subroutine wrapper, added comments>

1 Like

will try

You can save lots of pins by using a 74x164 and a 74x165. By placing the SIPO shift register you could scan the keyboard using only two GPIO pins and by placing the PISO shift register you could retrieve the results using three pins. With this kind of savings you may try to interface the displays. Remember that to avoid ghosting and other issues you need a diode on each key.

Regards

i changed the code and works now the problem was with the number of buttons in kept in the code i had 51 buttons but in code i was mentioning 2 in the joystick instead of 51

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