Hi all, I have already browsed the forums and unfortunately I couldn't find a way to use any other code for help. I am very new to this, and this is my first project. What I am attempting to do is use a 3 way joystick as a gear shifter. I am using the 3 way joystick and an Adafruit 1.44" 128x128 TFT display to show the gears. I am currently trying to use the Y-axis value over 950 to show an up-shift and one under 150 to show a downshift (not coded yet as I don't know how). I can display the first gear, as seen below, but I don't know how to "stack" or add onto it so that it does not just override the first one. Here is the code:
// Arduino pin numbers
const int SW_pin = 3; // input for detecting whether the joystick/button is pressed
const int X_pin = A0; // analog pin connected to X output
const int Y_pin = A1; // analog pin connected to Y output
int val0 = 0; //variable to store the value read of a0
int val1 = 0; //variable to store the value read of a1
#include <Adafruit_GFX.h> // Core graphics library
#include <Adafruit_ST7735.h> // Hardware-specific library
#include <SdFat.h> // SD card & FAT filesystem library
#include <Adafruit_SPIFlash.h> // SPI / QSPI flash library
#include <Adafruit_ImageReader.h> // Image-reading functions
#define USE_SD_CARD
#define SD_CS 4 // SD card select pin
#define TFT_CS 10 // TFT select pin
#define TFT_DC 8 // TFT display/command pin
#define TFT_RST 9 // Or set to -1 and connect to Arduino RESET pin
#if defined(USE_SD_CARD)
SdFat SD; // SD card filesystem
Adafruit_ImageReader reader(SD); // Image-reader object, pass in SD filesys
#else
// SPI or QSPI flash filesystem (i.e. CIRCUITPY drive)
#if defined(__SAMD51__) || defined(NRF52840_XXAA)
Adafruit_FlashTransport_QSPI flashTransport(PIN_QSPI_SCK, PIN_QSPI_CS,
PIN_QSPI_IO0, PIN_QSPI_IO1, PIN_QSPI_IO2, PIN_QSPI_IO3);
#else
#if (SPI_INTERFACES_COUNT == 1)
Adafruit_FlashTransport_SPI flashTransport(SS, &SPI);
#else
Adafruit_FlashTransport_SPI flashTransport(SS1, &SPI1);
#endif
#endif
Adafruit_SPIFlash flash(&flashTransport);
FatFileSystem filesys;
Adafruit_ImageReader reader(filesys); // Image-reader, pass in flash filesys
#endif
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
Adafruit_Image img; // An image loaded into RAM
int32_t width = 0, // BMP image dimensions
height = 0;
void setup() {
pinMode(SW_pin, INPUT); //setup SW input
digitalWrite(SW_pin, HIGH); //reading button state:1=not pressed,0=pressed
Serial.begin(9600); //Seput serical connection for print out to console
#if !defined(ESP32)
while(!Serial); // Wait for Serial Monitor before continuing
#endif
tft.initR(INITR_144GREENTAB); // Initialize screen
// The Adafruit_ImageReader constructor call (above, before setup())
// accepts an uninitialized SdFat or FatFileSystem object. This MUST
// BE INITIALIZED before using any of the image reader functions!
Serial.print(F("Initializing filesystem..."));
#if defined(USE_SD_CARD)
// SD card is pretty straightforward, a single call...
if(!SD.begin(SD_CS, SD_SCK_MHZ(10))) { // Breakouts require 10 MHz limit due to longer wires
Serial.println(F("SD begin() failed"));
for(;;); // Fatal error, do not continue
}
#else
// SPI or QSPI flash requires two steps, one to access the bare flash
// memory itself, then the second to access the filesystem within...
if(!flash.begin()) {
Serial.println(F("flash begin() failed"));
for(;;);
}
if(!filesys.begin(&flash)) {
Serial.println(F("filesys begin() failed"));
for(;;);
}
#endif
Serial.println(F("OK!"));
tft.fillScreen(ST7735_BLACK);
ImageReturnCode stat; // Status from image-reading functions
}
//print out values
void loop() {
Serial.print("Switch: ");
Serial.print(digitalRead(SW_pin));
Serial.print("\n");
Serial.print("X-axis: ");
Serial.print(analogRead(X_pin));
Serial.print("\n");
Serial.print("Y-axis: ");
Serial.println(analogRead(Y_pin));
Serial.print("\n\n");
delay(2000);
ImageReturnCode stat; // Status from image-reading functions
val0 = analogRead(X_pin); // read the input a0 pin
val1 = analogRead(Y_pin); // read the input a1 pin
if (val1 > 1000)
{
Serial.print(F("Loading 1stgear.bmp to screen..."));
stat = reader.drawBMP("/1stgear.bmp", tft, 0, 0);
reader.printStatus(stat); // How'd we do?
}
if (val1>900)
{
Serial.print(F("Loading 2ndgear.bmp to screen...."));
stat = reader.drawBMP("/2ndgear.bmp", tft, 0, 0);
reader.printStatus(stat); //how'd we do??
}
}
Thanks in advance!