j514:
When a new button is pressed, just do a FillRect instead of clearing the whole screen. It will look much cleaner.
Regards,
J
J, indeed it does. This is what the code looks like now.
/*
Joystick-uLCD by Negativ3
Outputs the direction of the thumb stick to uLCD-144
*/
//Include the displayshield Library
#include <displayshield4d.h>
DisplayShield4d oled;
//Define pin assignments
const int DOWNPin = 2; const int LEFTPin = 3; const int RIGHTPin = 4;
const int PRESSPin = 5; const int UPPin = 6;
void setup()
{
//Start serial comms
Serial.begin(57600);
//Initialise uLCD
oled.Init();
oled.Clear();
//Initialize the switch pins as a inputs:
pinMode(DOWNPin, INPUT); digitalWrite(DOWNPin, HIGH);
pinMode(LEFTPin, INPUT); digitalWrite(LEFTPin, HIGH);
pinMode(RIGHTPin, INPUT); digitalWrite(RIGHTPin, HIGH);
pinMode(PRESSPin, INPUT); digitalWrite(PRESSPin, HIGH);
pinMode(UPPin, INPUT); digitalWrite(UPPin, HIGH);
}
void loop()
{
//variables for reading switch status
int DOWNstate; int LEFTstate; int RIGHTstate; int PRESSstate; int UPstate;
//read state of DOWN switch
if (digitalRead(DOWNPin) == LOW)
{
// check if already pressed
if (DOWNstate == 0)
{
//write to LCD
oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
oled.setfont(OLED_FONT5x7);
oled.setfontmode(OLED_FONT_TRANSPARENT);
oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, "DOWN");
DOWNstate = 1;
}
} else {
DOWNstate = 0;
}
//read state of LEFT switch
if (digitalRead(LEFTPin) == LOW)
{
// check if already pressed
if (LEFTstate == 0)
{
//write to LCD
oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
oled.setfont(OLED_FONT5x7);
oled.setfontmode(OLED_FONT_TRANSPARENT);
oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, "LEFT");
// set pressed
LEFTstate = 1;
}
} else {
LEFTstate = 0;
}
//read state of RIGHT switch
if (digitalRead(RIGHTPin) == LOW)
{
// check if already pressed
if (RIGHTstate == 0)
{
//write to LCD
oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
oled.setfont(OLED_FONT5x7);
oled.setfontmode(OLED_FONT_TRANSPARENT);
oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, "RIGHT");
// set pressed
RIGHTstate = 1;
}
} else {
RIGHTstate = 0;
}
//read state of UP switch
if (digitalRead(UPPin) == LOW)
{
// check if already pressed
if (UPstate == 0)
{
//write to LCD
oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
oled.setfont(OLED_FONT5x7);
oled.setfontmode(OLED_FONT_TRANSPARENT);
oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, "UP");
// set pressed
UPstate = 1;
}
} else {
UPstate = 0;
}
//read state of PRESS switch
if (digitalRead(PRESSPin) == LOW)
{
// check if already pressed
if (PRESSstate == 0)
{
//write to LCD
oled.rectangle(0, 0, 128, 128, OLED_SOLID, oled.RGB(0, 0, 0));
oled.setfont(OLED_FONT5x7);
oled.setfontmode(OLED_FONT_TRANSPARENT);
oled.drawstringblock(5, 5, 0, oled.RGB(255, 255, 0), 4, 4, "PRESS");
// set pressed
PRESSstate = 1;
}
} else {
PRESSstate = 0;
}
}