Hi everyone,
I'm trying to make the classic "snake-game" with the help of a LED matrix (https://www.kiwi-electronics.com/en/32x16-rgb-led-matrix-panel-6mm-pitch-3748?search=led%20matrix). It's the first time I'm working with an Arduino (Leonardo), but I have some code writing experience. I will post the information which I think might be helpful. If you would require more info, I'll post that too.
Overview of the pins I use:
A0, output --> LED matrix 'A'
A1, output --> LED matrix 'B'
A2, output --> LED matrix 'C'
A3, output --> LED matrix 'LAT'
A4, input_pullup --> joystick VRx (to move horizontal)
A5, input_pullup --> joystick VRy (to move vertical)
D0, input --> bistable switch (difficulty selector(1) together with D1, 4 positions possible)
D1, input --> bistable switch (difficulty selector(2) together with D0)
D2, output --> LED matrix 'R1'
D3, output --> LED matrix 'G1'
D4, output --> LED matrix 'B1'
D5, output --> LED matrix 'R2'
D6, output --> LED matrix 'G2'
D7, output --> LED matrix 'B2'
D8, output --> LED matrix 'CLK'
D9, output --> LED matrix 'OE'
D10, output --> mini speaker
D11, output --> 4x 7 segment display (with shifting registers: DS)
D12, output --> 4x 7 segment display (with shifting registers: SHCP)
D13, output --> 4x 7 segment display (with shifting registers: STCP)
SDA & SCL --> 1 'device' to get the input of 'a startbutton', 'a resetbutton' and 'a bistable powerswitch'
To explain my reasoning:
At first I thought of making the coding of the LED matrix myself (without a library), since I don't need all the functions and so I wouldn't require all the pins. It did kind of work, but the result wasn't so stable and I didn't really know how to make it better. At that point I had just enough pins, but since it didn't really work the way I wanted, I used a library (GitHub - adafruit/RGB-matrix-Panel: Arduino library and example code for the 16x32 RGB matrix panels in the shop). This meant I was 3 pins short, luckily the I2C protocol (SDA and SCL) exists so I could use that.
When I was testing, I always did it piece by piece, so my Arduino never had all the pins connected and it worked fine. When I then put everything together and tested every piece again, the I2C protocol wasn't working. So after searching for a bit I found that the SDA and SCL pins are connected to D2 and D3 respectively. After pulling my LED matrix out of these pins, my code worked perfectly.
I'm working on this project for a few months now and I have learned and researched a lot, but I have no idea how to solve the double use of the D2/D3 pins. Since I already have everything soldered on a general PCB, my best thought would be to simply drop the 'startbutton (= used to start a new game; I would use a timer instead)', 'restbutton (=used to reset highscore; manually by reuploading)' and 'powerswitch (=used to turn on apparatus; turn on instantly)'. If someone would know an elegant solution which wouldn't require too much adjustments, I would love to hear it.
I don't know if it is needed but here is the code I used to test the I2C protocol.
Code:
/*EEPROM memory is standard set on 255 (1 byte). Since I need a max int of 509 (16 * 32 - 3), I need to use 2 bytes (adresses 0 and 1).
This value gets compared to the highsore and if needed changed. Initially a reset is needed, so the comparing with the highscore is correct.*/
#include <EEPROM.h> //needed for reading/writing highscore (=permanent memory)
#include <Wire.h> //communication with the I2c protocol
int highscore;
bool powerOn = false; //To indicate when aparatus has been set on/off
bool gamePlaying = false; //To indicate when a game is being played
int dataI2c; //Data received from I2c-chip
void setup() {
//delay(1000); //for testing purpose
//while (!Serial); //for testing purpose
Wire.begin();
Serial.begin(9600);
//Serial.println("Setup"); //for testing purpose
//delay(5000); //for testing purpose
}
void loop() {
//asking data of I2c-device
Serial.println("New Loop");
Wire.requestFrom(0x38, 1);
if (Wire.available()) {dataI2c = Wire.read();}
else {Serial.println("Failed");}
//set buttons on true/false when activated
bool startButton = dataI2c & B00000100;
bool powerButton = dataI2c & B00000010;
bool resetButton = dataI2c & B00000001;
Serial.print("Status start:");
Serial.println(startButton);
Serial.print("Status reset:");
Serial.println(resetButton);
Serial.print("Status power:");
Serial.println(powerButton);
//gamecode gets executed here
if (powerButton) {
if (resetButton) {reset();} //WARNING: limited number of cycles, EEPROM-memory (set in // for testing)
if (startButton && !gamePlaying) {
//Start the game sequence
}
}
else {
//execute end code, standard off
}
}
//reset function will reset the highscore, which is stored in EEPROM
void reset(){
EEPROM.update(0, 0); //reset the 2 bytes (= highscore) to 0
EEPROM.update(1, 0);
}
I didn't upload the wiring, because it's a mess and I didn't really think it's useful for the question.