Serial Print chess key entry

Hi You can see this is my code for the project .What i want to acchieve is pass chess pieces movement using serial print and pyautogui .I am able to pass the serial print only once after that it continuosly print the same thing

lets say "e2e4" if i enter that move it keeeps on printing it what
I want to acchieve is that once it passes the entered text i need the whole thing to begin fresh and ready for the new input

/* the tutorial code for 3x4 Matrix Keypad with Arduino is as
This code prints the key pressed on the keypad to the serial port*/

// Include the Keypad library
#include <Keypad.h>
const byte Rows= 4; //number of rows on the keypad i.e. 4
const byte Cols= 4; //number of columns on the keypad i,e, 3

String key1;
String key2;
String key3;
String key4;
String key5;

//we will definne the key map as on the key pad:

char keymap[Rows][Cols]=
{
{'a', 'b', 'c', 'd'},
{'e', 'f', 'g', 'h'},
{'1', '2', '3', '4'},
{'5', '6', '7', '8'}
};

//  a char array is defined as it can be seen on the above


//keypad connections to the arduino terminals is given as:1
byte rPins[Rows]= {13,12,11,10}; //Rows 0 to 3
byte cPins[Cols]= {9,8,7,6}; //Columns 0 to 2

// command for library forkeypad
//initializes an instance of the Keypad class
Keypad customKeypad = Keypad(makeKeymap(keymap), rPins, cPins, Rows, Cols);

void setup()
{
   Serial.begin(9600);  // initializing serail monitor
//     Keyboard.begin();
}

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out
void loop() {
 
while( key1 == NO_KEY )
          { key1 = customKeypad.getKey() ;  }
          delay(3) ;
while( key2 == NO_KEY )
          { key2 = customKeypad.getKey() ;  }
           delay(3) ;
while( key3 == NO_KEY )
          { key3 = customKeypad.getKey() ;  }
           delay(3) ;
while( key4 == NO_KEY )
          { key4 = customKeypad.getKey() ;  }
           delay(3) ;
key5 = key1+key2+key3+key4;

    Serial.println(key5);
    delay(20);
   
    key1 == NO_KEY;
    key2 == NO_KEY;
    key3 == NO_KEY;
    key4 == NO_KEY;
    key5 == NO_KEY;
     
}

think you intended "=" instead of "==".

not sure why you chose to use String

might consider a different approach

/* the tutorial code for 3x4 Matrix Keypad with Arduino is as
This code prints the key pressed on the keypad to the serial port*/
#undef MyHW
#ifdef MyHW
#define NO_KEY   0

struct Keypad {
    char  getKey ()  {
        if (Serial.available ())
            return (char)Serial.read ();
        return NO_KEY;
    }
};
Keypad customKeypad;

#else
// Include the Keypad library
#include <Keypad.h>
const byte Rows= 4; //number of rows on the keypad i.e. 4
const byte Cols= 4; //number of columns on the keypad i,e, 3
//we will definne the key map as on the key pad:
char keymap[Rows][Cols]=
{
    {'a', 'b', 'c', 'd'},
    {'e', 'f', 'g', 'h'},
    {'1', '2', '3', '4'},
    {'5', '6', '7', '8'}
};
//  a char array is defined as it can be seen on the above
//keypad connections to the arduino terminals is given as:1
byte rPins[Rows]= {13,12,11,10}; //Rows 0 to 3
byte cPins[Cols]= {9,8,7,6}; //Columns 0 to 2
// command for library forkeypad
//initializes an instance of the Keypad class
Keypad customKeypad = Keypad(makeKeymap(keymap), rPins, cPins, Rows, Cols);
#endif

char key [5];

void setup()
{
    Serial.begin(9600);  // initializing serail monitor
    //     Keyboard.begin();
}

//If key is pressed, this key is stored in 'keypressed' variable
//If key is not equal to 'NO_KEY', then this key is printed out

void loop() {
    int i;
    for (i = 0; i < 4; i++)  {
        while (NO_KEY == (key [i] = customKeypad.getKey()))
            ;
    }
    key [i] = 0;
    Serial.println (key);
}

Thank you for your reply.I want to enter chess moves and it will be in SAN format like

e2e4 ,a1a6 etc...

For this i need to read the data entered from keyboard and store it till all 4 keys are entered,which is followed by posting it to raspberry pi using serial communication.
Then wait for return communication from raspberry pi showing system move on oled and then restart the entire process from scratch

OK, I understand what you want to do but the question still arises as to why you chose to use Strings

The getKey() returns a char in your sketch, so why not add each char to an array then send that to the Pi ?

Simple answer i dont know how to do it.To tell you the truth i am beginner

do you understand that the following does that?

It would be easier to use waitForKey() rather than getKey() . The fact that it is a blocking function is no problem as the sketch is doing nothing else

However, it is important to beware of feature creep such as suddenly being asked to add a move clock, for instance

i think there are other issues to consider such as accepting valid input symbols and handing errors

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