Added SoftwareSerial header and in Makefile.

This commit is contained in:
2018-10-16 19:56:10 +01:00
parent 59ed7d153a
commit d8a2a31596
2 changed files with 81 additions and 74 deletions

View File

@@ -9,81 +9,84 @@ Tue Jun 12 14:56:12 GMT 2018
*********************************************************************/
// Libraries to be included, non native libraries need to be included in the Makefile
#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
#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
#include <SoftwareSerial.h> // Handles serial communications thru software
// Configuration of pins, insert here arduino pin numbers only(not fisical)----------------------------
#define i_load 6 // Analog pin for the load current
#define v_batt 7 // Analog pin for the battery voltage
#define volt_1 0 // Analog pin for voltage 1
#define volt_2 1 // Analog pin for voltage 2
#define load_line 17 // Line that turns load On or Off
#define LEFT 16 // Button 1 will be function Left
#define RIGHT 18 // Button 2 will be function Right
#define ACT 19 // Button 3 will be function Action
#define i_load 6 // Analog pin for the load current
#define v_batt 7 // Analog pin for the battery voltage
#define volt_1 0 // Analog pin for voltage 1
#define volt_2 1 // Analog pin for voltage 2
#define load_line 17 // Line that turns load On or Off
#define LEFT 16 // Button 1 will be function Left
#define RIGHT 18 // Button 2 will be function Right
#define ACT 19 // Button 3 will be function Action
#define BLUE_RX 8 // Arduino rx serial line to bluetooth
#define BLUE_TX 9 // Arduino tx serial line to bluetooth
LiquidCrystal lcd(7, 6, 2, 3, 4, 5); // LCD screen pins, LiquidCrystal(rs, enable, d4, d5, d6, d7)
//-----------------------------------------------------------------------------------------------------
// Other configurations ------------------------------------------------------------------------------------
#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 curr 2 // Parameter position where load current value is hold
#define battery_volt 3 // Parameter position where battery voltage value is hold
#define numOfScreens 7 // Number of screen menus, select here and rearrange String screens
#define delay_loop 4000 // Value in miliseconds for the main loop routine
#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 writing 3 // Write on the sd card
#define curr_limit 2 // Maximum current allowed
#define off 0 // off is always zero
#define on 1 // on is always one
#define no 0 // no is always zero
#define yes 1 // yes is alyays one
File myFile; // Initiate the object File to write in the SD card
#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 curr 2 // Parameter position where load current value is hold
#define battery_volt 3 // Parameter position where battery voltage value is hold
#define numOfScreens 7 // Number of screen menus, select here and rearrange String screens
#define delay_loop 4000 // Value in miliseconds for the main loop routine
#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 writing 3 // Write on the sd card
#define curr_limit 2 // Maximum current allowed
#define off 0 // off is always zero
#define on 1 // on is always one
#define no 0 // no is always zero
#define yes 1 // yes is alyays one
File myFile; // Initiate the object File to write in the SD card
//----------------------------------------------------------------------------------------------------------
// Macros --------------------------------------------------------
#define rLEFT digitalRead(LEFT) // Read button Left
#define rRIGHT digitalRead(RIGHT) // Read button Right
#define rLEFT digitalRead(LEFT) // Read button Left
#define rRIGHT digitalRead(RIGHT) // Read button Right
#define rACT digitalRead(ACT) // Read button Action
//----------------------------------------------------------------
// Variables --------------------------------------------------------------------------------------------------------------
bool sd_rw = off; // Holds the state of read/write of the card
bool load_status = off; // Holds the state of on/off of the load
bool sd_inserted = no; // Holds the state if sd card is in or not
int sd_status = sd_not_in; // Holds information about the sd card
bool sd_rw = off; // Holds the state of read/write of the card
bool load_status = off; // Holds the state of on/off of the load
bool sd_inserted = no; // Holds the state if sd card is in or not
int sd_status = sd_not_in; // Holds information about the sd card
int currentScreen = 0; // Current display menu
int currentScreen = 0; // Current display menu
// String with the menus to be displayed
String screens[numOfScreens][2] = {{"Voltage 1","Volts"}, {"Voltage 2","Volts"},
{"Load current","Amps"},{"Batt Voltage","volts"}, {"SD Logging", ""}, {"Load", ""}, {"SD card", ""}};
String parameters_sd[2] = {"No","Yes"}; // String to display the sd card logging
String parameters_load[2] = {"Off","On"}; // String to display the load state
String parameters_sd[2] = {"No","Yes"}; // String to display the sd card logging
String parameters_load[2] = {"Off","On"}; // String to display the load state
String sd_card[4] = {"Card missing","Card inserted","File Write fail","Writing..."}; // String to display the sd card state
String file_name = "measures"; // Initial file name only 8 characters allowed
float parameters[4]; // Parameters of the several voltages and current
float denominator; // Denominator to calculate the voltage divider
int resistor1 = 22000; // Resistor one of voltage divider
int resistor2 = 2200; // Resistor two of voltage divider
float denominator; // Denominator to calculate the voltage divider
int resistor1 = 22000; // Resistor one of voltage divider
int resistor2 = 2200; // Resistor two of voltage divider
// ------------------------------------------------------------------------------------------------------------------------
// Function declarations --------------------------------------------------
void menuState(void); // Read buttons and update menu
void printScreen (void); // Print menus and variables on screen
float volt_pin (int pin); // Read and display voltage
float amp_pin (int pin); // Read and display current
void menuState(void); // Read buttons and update menu
void printScreen (void); // Print menus and variables on screen
float volt_pin (int pin); // Read and display voltage
float amp_pin (int pin); // Read and display current
float fmap(float x, float in_min, float in_max, float out_min, float out_max); // Calculate current opamp
//-------------------------------------------------------------------------
@@ -96,7 +99,7 @@ void setup()
pinMode(ACT, INPUT); // Button act is an input
pinMode(load_line, OUTPUT); // load_line is an output
lcd.begin(16, 2); // Start LCD display with 16 characters and 2 lines
lcd.begin(16, 2); // Start LCD display with 16 characters and 2 lines
// Voltage divider --> Vout = Vin * R2 / R1 + R2
denominator = (float)resistor2 / (resistor1 + resistor2);
@@ -121,7 +124,7 @@ void setup()
sd_inserted = yes; // and change variable sd_inserted to yes
}
else {
sd_status = sd_not_in; // If sd card is not initiated change variable to sd_not_in
sd_status = sd_not_in; // If sd card is not initiated change variable to sd_not_in
sd_inserted = no; // and change variable sd_inserted to no
}
}
@@ -130,10 +133,10 @@ void setup()
// Timer compare interrupt service routine --
ISR(TIMER1_COMPA_vect)
{
menuState(); // Read buttons act accordingly and update menus
if (amp_pin(i_load) > curr_limit) // If load current more then current limit
menuState(); // Read buttons act accordingly and update menus
if (amp_pin(i_load) > curr_limit) // If load current more then current limit
{
load_status = off; // Turn off load
load_status = off; // Turn off load
}
digitalWrite(load_line, load_status); // Turn load on or off depending on the load_status
}
@@ -142,30 +145,30 @@ ISR(TIMER1_COMPA_vect)
void loop()
{
parameters[battery_volt] = volt_pin(v_batt); // Update Battery voltage value
parameters[voltage_1] = volt_pin(volt_1); // Update voltage one value
parameters[voltage_2] = volt_pin(volt_2); // Update voltage two value
parameters[curr] = amp_pin(i_load); // Update current load
parameters[battery_volt] = volt_pin(v_batt); // Update Battery voltage value
parameters[voltage_1] = volt_pin(volt_1); // Update voltage one value
parameters[voltage_2] = volt_pin(volt_2); // Update voltage two value
parameters[curr] = amp_pin(i_load); // Update current load
if (sd_inserted == no) // If sd_rw value is turned on
if (sd_inserted == no) // If sd_rw value is turned on
{
sd_status = sd_not_in;
}
else if (sd_rw == on)
{
myFile = SD.open(file_name + ".TXT", FILE_WRITE); // Open file name and add ".TXT", for writing
if (myFile) // if succeds
if (myFile) // if succeds
{
myFile.print("Batt voltage, ");
myFile.println(volt_pin(v_batt)); // Write to card battery voltage
myFile.println(volt_pin(v_batt)); // Write to card battery voltage
myFile.print("Voltage one, ");
myFile.println(volt_pin(volt_1)); // Write to card voltage one
myFile.println(volt_pin(volt_1)); // Write to card voltage one
myFile.print("Voltage two, ");
myFile.println(volt_pin(volt_2)); // Write to card voltage two
myFile.println(volt_pin(volt_2)); // Write to card voltage two
myFile.print("Load current, ");
myFile.println(amp_pin(i_load)); // Write to card load current
myFile.println(""); // Write to card an empty line
myFile.close(); // Close file
myFile.println(amp_pin(i_load)); // Write to card load current
myFile.println(""); // Write to card an empty line
myFile.close(); // Close file
sd_status = writing;
}
else
@@ -178,8 +181,8 @@ void loop()
sd_status = sd_in;
}
printScreen(); // Refresh screen and print current menu and values
delay(delay_loop); // Repeat the main loop every delay_loop seconds
printScreen(); // Refresh screen and print current menu and values
delay(delay_loop); // Repeat the main loop every delay_loop seconds
}
void menuState()
@@ -195,8 +198,8 @@ void menuState()
{
currentScreen++;
}
while (!rRIGHT); // Check if button is still pressed do nothing
printScreen(); // Refresh screen and print current menu and values
while (!rRIGHT); // Check if button is still pressed do nothing
printScreen(); // Refresh screen and print current menu and values
}
if (!rLEFT)
@@ -209,8 +212,8 @@ void menuState()
{
currentScreen--;
}
while (!rLEFT); // Check if button is still pressed do nothing
printScreen(); // Refresh screen and print current menu and values
while (!rLEFT); // Check if button is still pressed do nothing
printScreen(); // Refresh screen and print current menu and values
}
if (!rACT)
@@ -223,8 +226,8 @@ void menuState()
{
load_status = !load_status;
}
while (!rACT); // Check if button is still pressed do nothing
printScreen(); // Refresh screen and print current menu and values
while (!rACT); // Check if button is still pressed do nothing
printScreen(); // Refresh screen and print current menu and values
}
}
@@ -260,8 +263,8 @@ float volt_pin(int pin)
{
float voltage;
voltage = analogRead(pin); // Obtain RAW voltage data
voltage = (voltage / 1024) * 3.3; // Convert to actual voltage
voltage = voltage / denominator; // Convert to voltage after divider
voltage = (voltage / 1024) * 3.3; // Convert to actual voltage
voltage = voltage / denominator; // Convert to voltage after divider
return voltage;
}