Dear members!
I want to save some information on an SD card and show it on a display. I am using one Arduino Nano card, keypad and Adafruit ILI9341 display. The problem so far is that the character "#" saves in the array and shows in the display on the front on liters. The output should be just "65", not "#65". So I will be very grateful if you can help me to fix that.
Thanks in advance!
Here is the code so far:
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include "Adafruit_ILI9341.h"
//#include "Adafruit_ILI9340.h"
#include <SD.h>
#include <Keypad.h>
#define TFT_DC 9
#define TFT_CS 10
#define PAGE_MENU 0
#define PAGE_ENTER_DATE 1
#define PAGE_CONFIRM_DATE 2
#define PAGE_ENTER_DIESEL_LITRES 3
#define PAGE_CONFIRM_REPORT 4
#define PAGE_CONFIRM_MESSAGE 5
// Use hardware SPI (on Mega, #52, #51, #50)
Adafruit_ILI9341 tft = Adafruit_ILI9341(TFT_CS, TFT_DC);
// Configure keypad
// Four Rows
const byte ROWS = 4;
// Three Columns
const byte COLS = 3;
// Key layout
char keys[ROWS][COLS] = {
{'1', '2', '3'},
{'4', '5', '6'},
{'7', '8', '9'},
{'*', '0', '#'}
};
// Row pinouts
byte rowPins[ROWS] = {14, 5, 3, 2};
// Columns pinouts
byte colPins[COLS] = {17, 16, 15};
Keypad keypad = Keypad( makeKeymap(keys), rowPins, colPins, ROWS, COLS );
// Max length of input
char values[9];
// Variables for Tanklog
char diesel[5];
char date[9];
// Count to fill the array with characters from keypad
int count = 0;
// Switch between pages
static int page;
void setup() {
// Add an event listener for this keypad
keypad.addEventListener(keypadEvent);
keypad.setHoldTime(500);
tft.begin();
tft.setRotation(1);
tft.setTextColor(ILI9341_WHITE);
Menu();
}
void loop() {
char key = keypad.getKey();
if (key != NO_KEY) {
tft_update(key);
count ++;
}
}
void tft_update(char key) {
if (page == PAGE_ENTER_DATE && count < 9) {
tft.print(key);
values[count] = key;
}
if (page == PAGE_ENTER_DIESEL_LITRES && count < 4) {
tft.print(key);
values[count] = key;
}
}
void SDwrite() {
File myFile;
Serial.print("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("initialization failed!");
while (1);
}
Serial.println("initialization done.");
// open the file. note that only one file can be open at a time,
// so you have to close this one before opening another.
myFile = SD.open("test.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to test.txt...");
for (int i = 0; i < 8; i++) {
myFile.print(date[i]);
}
myFile.print(";");
for (int i = 0; i < 5; i++) {
myFile.print(diesel[i]);
}
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening test.txt");
}
}
void Menu() {
//Make something to switch to page 11 or somewhat to catch date and time and flip back to Menupage 1 if ready
// Serial.println("Did you tank now menu");
// Serial.println(page);
tft.fillScreen(ILI9341_BLACK);
tft.setCursor(100, 20);
tft.setTextSize(3);
tft.println("<<Menu>>");
tft.setCursor(30, 80);
tft.setTextSize(2);
tft.println(F("Did you tank right now?"));
tft.setCursor(110, 100);
tft.println(F("(Hold 1)"));
tft.setCursor(30, 140);
tft.println(F("or another date?"));
tft.setCursor(110, 160);
tft.println(F("(Hold 2)"));
}
void EnterDate() { //-----------------------Tese DataPage--------------------
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(40, 60);
tft.println("Write date <<YYYYMMDD>>");
tft.setCursor(40, 80);
tft.println("after that (press #)");
tft.setCursor(140, 120);
}
void ConfirmDate() { //-----------------------Tese DataPage--------------------
memcpy(date, values, 8);
date[8] = '\0';
memset (values, 0, 9);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(40, 80);
tft.println("If correct (press #)");
tft.setCursor(60, 100);
tft.print ("Date: ");
for (int i = 0; i < 4; i++) {
tft.print(date[i]);
}
tft.print ("-");
for (int i = 4; i < 6; i++) {
tft.print (date[i]);
}
tft.print ("-");
for (int i = 6; i < 8; i++) {
tft.print (date[i]);
}
tft.setCursor(60, 120);
}
void EnterDieselLitres() {
memset (values, 0, 9);
// Serial.println("Diesel menu..");
// Serial.println(page);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 100);
tft.println(F("How many liters of Diesel?"));
tft.setCursor(40, 120);
tft.println("after that (press #)");
tft.setCursor(140, 160);
}
void ConfirmReport() {
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(0, 40);
tft.println(F("Send this report (Press #)"));
tft.setCursor(0, 60);
tft.println(F("Back to Menu (Press 1)"));
tft.setCursor(0, 80);
tft.println(F("otherwise turn device off:"));
tft.setCursor(100, 100);
tft.print ("Diesel: ");
for (int i = 0; i < 4; i++) {
tft.print(diesel[i]);
}
tft.setCursor(100, 160);
tft.print ("Date: ");
for (int i = 0; i < 4; i++) {
tft.print(date[i]);
}
tft.print ("-");
for (int i = 4; i < 6; i++) {
tft.print (date[i]);
}
tft.print ("-");
for (int i = 6; i < 8; i++) {
tft.print (date[i]);
}
tft.setCursor(60, 120);
SDwrite();
}
void ConfirmMessage() {
//digitalWrite(TFT_CS,HIGH);//---------------------------------
// To land again on page 0 after the loop
page = -1; // This will make next page = 0 (PAGE_MENU)
//Write to user that it is ok
// Serial.println("Report sent menu..");
// Serial.println(page);
tft.fillScreen(ILI9341_BLACK);
tft.setTextSize(2);
tft.setCursor(40, 80);
tft.println("Report has been sent!");
tft.setCursor(40, 100);
tft.println("You can shut down this");
tft.setCursor(40, 120);
tft.println("device now or (press #)");
memset (values, 0, 9);
memset (diesel, 0, 5);
memset (date, 0, 9);
}
void executeAction() {
switch (page) {
case PAGE_MENU:
Menu();
break;
case PAGE_ENTER_DATE:
EnterDate();
break;
case PAGE_CONFIRM_DATE:
ConfirmDate();
break;
case PAGE_ENTER_DIESEL_LITRES:
EnterDieselLitres();
break;
case PAGE_CONFIRM_REPORT:
ConfirmReport();
break;
case PAGE_CONFIRM_MESSAGE:
ConfirmMessage();
break;
}
count = 0;
//Resetting landing Array
memset (values, 0, 9);
}
// Taking care of some special events.
void keypadEvent(KeypadEvent key) {
switch (keypad.getState()) {
case PRESSED:
if (key == '#') {
page++; // page is set to -1 after last page = PAGE_CONFIRM_MESSAGE
executeAction();
}
else if (key == '*') {
memset (values, 0, 9);
memset (diesel, 0, 5);
memset (date, 0, 9);
page = PAGE_MENU;
executeAction();
}
break;
case HOLD:
if (key == '1') {
memset (values, 0, 9);
memset (diesel, 0, 5);
page = PAGE_ENTER_DIESEL_LITRES;
executeAction();
}
if (key == '2') {
memset (values, 0, 9);
memset (diesel, 0, 5);
memset (date, 0, 9);
page = PAGE_ENTER_DATE;
executeAction();
}
break;
case RELEASED:
break;
}
}