Hello,
I am creating a pretty basic telemetry system for an electric car however I have come across some problems when I want to print it to the LCD screen for the driver to see.
I can get all of the telemetry system to print to the serial, SD card and screen (The data being printed all at once)
However for the screen I want to locate the data being printed on different sections on the screen, for example the screen is cleared and different data is displayed when a button is pressed which I cannot get to work. I have tried using code found on the internet with no success so I copied my code into the 'HelloWorld' example code but when a button is pressed the message is displayed for only the time it is pressed (I have been doing some research and have come to a conclusion that I have to set it either to high or low but not exactly sure how?)
As the race for the car is an hour long I will need to delay how often data is saved and displayed.
For example the temperature which I am measuring will not changed fast so the refresh rate will need to be 10 seconds whereas I will need regular amp draw readings at every 2 seconds.
How would I get it so that there is a delay of 10 seconds to display the temperature reading and save it to the SD card and only a delay of 2 seconds for amp draw.
Attached is the code for the current version of the telemetry system. (If it can be optimised please suggest any improvements or apply code to solve the problems above would be very much appreciated!)
Thank you.
#include <Wire.h>
#include <Adafruit_RGBLCDShield.h>
#include <utility/Adafruit_MCP23017.h>
#include <SD.h>
#include <SPI.h>
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
// These #defines make it easy to set the backlight color
#define RED 0x1
#define YELLOW 0x3
#define GREEN 0x2
#define TEAL 0x6
#define BLUE 0x4
#define VIOLET 0x5
#define WHITE 0x7
File myFile;
int sensorPin = 0;
int val = 0;
//Voltage
int VRaw; //This will store our raw ADC data
int IRaw;
float VFinal; //This will store the converted data
float IFinal;
void setup() {
Serial.begin(9600);
lcd.begin(16, 2);
Serial.begin(9600);
lcd.print("Initializing...");
Serial.println("Initializing SD Card...");
if (!SD.begin(10))
{Serial.println("Initialization Failed!");
return;
}
delay(1500);
lcd.clear();
Serial.println("Initialization Complete!");
lcd.print("Initialized!");
lcd.setBacklight(WHITE);
delay(2000);
lcd.clear();
}
uint8_t i=0;
void loop() {
myFile = SD.open("data.csv", FILE_WRITE);
if (myFile)
{
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
//Measurement
VRaw = analogRead(A1);
IRaw = analogRead(A5);
//Conversion
//VFinal = VRaw/49.44; //45 Amp board
//VFinal = VRaw/12.99; //90 Amp board
VFinal = VRaw/12.99; //180 Amp board
//IFinal = IRaw/14.9; //45 Amp board
//IFinal = IRaw/7.4; //90 Amp board
IFinal = IRaw/3.7; //180 Amp board
Serial.print(temperatureC);
Serial.println(" Degrees Celsius");
Serial.print(VFinal);
Serial.println(" Volts");
Serial.print(IFinal);
Serial.println(" Amps");
Serial.println(millis());
myFile.print(temperatureC);
myFile.print(", ");
myFile.print(VFinal);
myFile.print(", ");
myFile.print(IFinal);
myFile.print(", ");
myFile.println(millis());
myFile.close();
lcd.setCursor(0, 0);
lcd.print("T:");
lcd.print(temperatureC);
lcd.setCursor(0, 1);
lcd.print("V:");
lcd.print(VFinal);
lcd.print(" A:");
lcd.print(IFinal);
}
else
{
Serial.println("Error in opening file");
lcd.clear();
lcd.print("SD Card Error");
void setup();
delay(5000);
}
uint8_t buttons = lcd.readButtons();
if (buttons) {
lcd.clear();
lcd.setCursor(0,0);
if (buttons & BUTTON_UP) {
lcd.print("UP ");
lcd.setBacklight(RED);
}
if (buttons & BUTTON_DOWN) {
lcd.print("DOWN ");
lcd.setBacklight(YELLOW);
}
if (buttons & BUTTON_LEFT) {
lcd.print("LEFT ");
lcd.setBacklight(GREEN);
}
if (buttons & BUTTON_RIGHT) {
lcd.print("RIGHT ");
lcd.setBacklight(TEAL);
}
if (buttons & BUTTON_SELECT) {
lcd.print("SELECT ");
lcd.setBacklight(VIOLET);
}
}
}
LCD_Telemtry_System.ino (2.73 KB)