Implemented SD card writing, updated TODO.

This commit is contained in:
2018-06-19 13:47:42 +00:00
parent 3ee4fb04c2
commit 62738ec4a0
3 changed files with 80 additions and 38 deletions

2
TODO
View File

@@ -18,7 +18,7 @@ Buttons DONE
Voltage measurement DONE Voltage measurement DONE
Load line on/off DONE Load line on/off DONE
Current measurement Current measurement
SD card SD card DONE
Bluetooth Bluetooth

View File

@@ -3,7 +3,9 @@
BOARD_TAG = pro BOARD_TAG = pro
BOARD_SUB = 8MHzatmega328 BOARD_SUB = 8MHzatmega328
USER_LIB_PATH += /home/ricardo/Arduino/libraries USER_LIB_PATH += /home/ricardo/Arduino/libraries
ARDUINO_LIBS += LiquidCrystal ARDUINO_LIBS += LiquidCrystal \
SPI \
SD
ARDUINO_DIR = /home/ricardo/arduino-1.8.5 ARDUINO_DIR = /home/ricardo/arduino-1.8.5
MONITOR_PORT = /dev/ttyUSB0 MONITOR_PORT = /dev/ttyUSB0
include /usr/share/arduino/Arduino.mk include /usr/share/arduino/Arduino.mk

View File

@@ -8,8 +8,10 @@ By Jony Silva
Tue Jun 12 14:56:12 GMT 2018 Tue Jun 12 14:56:12 GMT 2018
*********************************************************************/ *********************************************************************/
#include <avr/sleep.h> // Libraries to be included, non native libraries need to be included in the Makefile
#include <LiquidCrystal.h> #include <LiquidCrystal.h> // Handles the connection and communications with the LCD display
#include <SPI.h> // Handles the SPI connection to the SD card
#include <SD.h> // Handles communication routines with the SD card
// Configuration of pins, insert here arduino pin numbers only(not fisical)---------------------------- // Configuration of pins, insert here arduino pin numbers only(not fisical)----------------------------
@@ -28,32 +30,45 @@ LiquidCrystal lcd(7, 6, 2, 3, 4, 5); // LCD screen pins, LiquidCrystal(rs, enabl
#define battery_volt 3 // Parameter position where battery voltage value is hold #define battery_volt 3 // Parameter position where battery voltage value is hold
#define voltage_1 0 // Parameter position where voltage one value is hold #define voltage_1 0 // Parameter position where voltage one value is hold
#define voltage_2 1 // Parameter position where voltage two value is hold #define voltage_2 1 // Parameter position where voltage two value is hold
#define numOfScreens 6 // Number of screen menus, select here and rearrange String screens #define numOfScreens 7 // Number of screen menus, select here and rearrange String screens
File myFile;
//---------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------
// Macros to read the buttons -------------------------------- // Macros --------------------------------------------------------
#define rLEFT digitalRead(LEFT) // Read button Left #define rLEFT digitalRead(LEFT) // Read button Left
#define rRIGHT digitalRead(RIGHT) // Read button Right #define rRIGHT digitalRead(RIGHT) // Read button Right
#define rACT digitalRead(ACT) // Read button Action #define rACT digitalRead(ACT) // Read button Action
//------------------------------------------------------------ #define sd_not_in 0 // SD card is not inserted
#define sd_in 1 // SD card is inserted
#define file_fail 2 // Can't write to file
#define load_off 0 // Turn off the load
#define load_on 1 // Turn on the load
#define off 0 // off is always zero
#define on 1 // on is always one
//----------------------------------------------------------------
// Variables ------------------------------------------------------------------------------ // Variables --------------------------------------------------------------------------------------------------------------
bool sd_status = 0; bool sd_rw = off; // Holds the state of read/write of the card
bool load_status = 0; bool load_status = load_off; // Holds the state of on/off of the load
int sd_status = sd_not_in; // Holds information about the sd card
int currentScreen = 0; int currentScreen = 0; // Current display menu
// String with the menus to be displayed
String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"}, String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"},
{"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}, {"Load", ""}}; {"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}, {"Load", ""}, {"SD card", ""}};
String parameters_sd[2] = {"No","Yes"};
String parameters_load[2] = {"Off","On"}; String parameters_sd[2] = {"No","Yes"}; // String to display the sd card logging
float parameters[4]; String parameters_load[2] = {"Off","On"}; // String to display the load state
String sd_card[3] = {"Card missing","Card inserted","File Write fail"}; // String to display the sd card state
float parameters[4]; // Parameters of the several voltages and current
float denominator; float denominator;
int resistor1 = 22000; // Resistor one of voltage divider int resistor1 = 22000; // Resistor one of voltage divider
int resistor2 = 2200; // Resistor two of voltage divider int resistor2 = 2200; // Resistor two of voltage divider
// ---------------------------------------------------------------------------------------- // ------------------------------------------------------------------------------------------------------------------------
@@ -90,26 +105,51 @@ void setup()
TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt TIMSK1 |= (1 << OCIE1A); // enable timer compare interrupt
interrupts(); // enable all interrupts interrupts(); // enable all interrupts
// ---------------------------------------------------------- // ----------------------------------------------------------
if (SD.begin()) // Initiate SD card
{
sd_status = sd_in; // If sd card is initiated successfully change variable status to sd_in
}
else {
sd_status = sd_not_in; // If sd card is not initiated change variable status to sd_not_in
}
} }
// Timer compare interrupt service routine -- // Timer compare interrupt service routine --
ISR(TIMER1_COMPA_vect) ISR(TIMER1_COMPA_vect)
{ {
menuState(); menuState(); // Read buttons act accordingly and update menus
digitalWrite(load_line, load_status); digitalWrite(load_line, load_status); // Turn load on or off depending on the load_status
} }
//------------------------------------------- //-------------------------------------------
void loop() void loop()
{ {
parameters[battery_volt] = volt_pin(v_batt); parameters[battery_volt] = volt_pin(v_batt); // Update Battery voltage value
parameters[voltage_1] = volt_pin(volt_1); parameters[voltage_1] = volt_pin(volt_1); // Update voltage one value
parameters[voltage_2] = volt_pin(volt_2); parameters[voltage_2] = volt_pin(volt_2); // Update voltage two value
printScreen();
delay(4000); if (sd_rw == on) // If sd_rw value is turned on
{
myFile = SD.open("measure.txt", FILE_WRITE); // Open file measure.txt for writing
if (myFile) // if succeds
{
myFile.println(volt_pin(v_batt)); // Write to card battery voltage
myFile.println(volt_pin(volt_1)); // Write to card voltage one
myFile.println(volt_pin(volt_2)); // Write to card voltage two
myFile.println(""); // Write to card an empty line
myFile.close(); // Close file
}
else
{
sd_status = file_fail; //
}
}
printScreen(); // Refresh screen and print current menu and values
delay(5000); // Repeat the main loop every 5 seconds
} }
void menuState() void menuState()
@@ -126,7 +166,7 @@ void menuState()
currentScreen++; currentScreen++;
} }
while (!rRIGHT); // Check if button is still pressed do nothing while (!rRIGHT); // Check if button is still pressed do nothing
printScreen(); printScreen(); // Refresh screen and print current menu and values
} }
if (!rLEFT) if (!rLEFT)
@@ -140,21 +180,21 @@ void menuState()
currentScreen--; currentScreen--;
} }
while (!rLEFT); // Check if button is still pressed do nothing while (!rLEFT); // Check if button is still pressed do nothing
printScreen(); printScreen(); // Refresh screen and print current menu and values
} }
if (!rACT) if (!rACT)
{ {
if (currentScreen == 4) if (currentScreen == 4)
{ {
sd_status = !sd_status; sd_rw = !sd_rw;
} }
if (currentScreen == 5) if (currentScreen == 5)
{ {
load_status = !load_status; load_status = !load_status;
} }
while (!rACT); // Check if button is still pressed do nothing while (!rACT); // Check if button is still pressed do nothing
printScreen(); printScreen(); // Refresh screen and print current menu and values
} }
} }
@@ -167,7 +207,7 @@ void printScreen()
if (currentScreen == 4) if (currentScreen == 4)
{ {
lcd.print(parameters_sd[sd_status]); lcd.print(parameters_sd[sd_rw]);
} }
else if (currentScreen == 5) else if (currentScreen == 5)
{ {
@@ -175,7 +215,7 @@ void printScreen()
} }
else if (currentScreen == 6) else if (currentScreen == 6)
{ {
lcd.print(screens[currentScreen][1]); lcd.print(sd_card[sd_status]);
} }
else else
{ {