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

@@ -5,8 +5,12 @@ BOARD_SUB = 8MHzatmega328
USER_LIB_PATH += /home/ricardo/Arduino/libraries USER_LIB_PATH += /home/ricardo/Arduino/libraries
ARDUINO_LIBS += LiquidCrystal \ ARDUINO_LIBS += LiquidCrystal \
SPI \ SPI \
SD SD \
ARDUINO_DIR = /home/ricardo/arduino-1.8.5 SoftwareSerial
MONITOR_PORT = /dev/ttyUSB0
include /usr/share/arduino/Arduino.mk # ARDUINO_DIR = /home/ricardo/arduino-1.8.5
ARDUINO_DIR = /home/ricardo/Installs/arduino-1.8.7
MONITOR_PORT = /dev/ttyUSB0
-include /usr/share/arduino/Arduino.mk
-include /home/ricardo/Installs/Arduino-Makefile-1.6.0/Arduino.mk

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 // 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 <LiquidCrystal.h> // Handles the connection and communications with the LCD display
#include <SPI.h> // Handles the SPI connection to the SD card #include <SPI.h> // Handles the SPI connection to the SD card
#include <SD.h> // Handles communication routines with 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)---------------------------- // Configuration of pins, insert here arduino pin numbers only(not fisical)----------------------------
#define i_load 6 // Analog pin for the load current #define i_load 6 // Analog pin for the load current
#define v_batt 7 // Analog pin for the battery voltage #define v_batt 7 // Analog pin for the battery voltage
#define volt_1 0 // Analog pin for voltage 1 #define volt_1 0 // Analog pin for voltage 1
#define volt_2 1 // Analog pin for voltage 2 #define volt_2 1 // Analog pin for voltage 2
#define load_line 17 // Line that turns load On or Off #define load_line 17 // Line that turns load On or Off
#define LEFT 16 // Button 1 will be function Left #define LEFT 16 // Button 1 will be function Left
#define RIGHT 18 // Button 2 will be function Right #define RIGHT 18 // Button 2 will be function Right
#define ACT 19 // Button 3 will be function Action #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) LiquidCrystal lcd(7, 6, 2, 3, 4, 5); // LCD screen pins, LiquidCrystal(rs, enable, d4, d5, d6, d7)
//----------------------------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------------------------
// Other configurations ------------------------------------------------------------------------------------ // Other configurations ------------------------------------------------------------------------------------
#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 curr 2 // Parameter position where load current 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 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 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 delay_loop 4000 // Value in miliseconds for the main loop routine
#define sd_not_in 0 // SD card is not inserted #define sd_not_in 0 // SD card is not inserted
#define sd_in 1 // SD card is inserted #define sd_in 1 // SD card is inserted
#define file_fail 2 // Can't write to file #define file_fail 2 // Can't write to file
#define writing 3 // Write on the sd card #define writing 3 // Write on the sd card
#define curr_limit 2 // Maximum current allowed #define curr_limit 2 // Maximum current allowed
#define off 0 // off is always zero #define off 0 // off is always zero
#define on 1 // on is always one #define on 1 // on is always one
#define no 0 // no is always zero #define no 0 // no is always zero
#define yes 1 // yes is alyays one #define yes 1 // yes is alyays one
File myFile; // Initiate the object File to write in the SD card File myFile; // Initiate the object File to write in the SD card
//---------------------------------------------------------------------------------------------------------- //----------------------------------------------------------------------------------------------------------
// Macros -------------------------------------------------------- // 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
//---------------------------------------------------------------- //----------------------------------------------------------------
// Variables -------------------------------------------------------------------------------------------------------------- // Variables --------------------------------------------------------------------------------------------------------------
bool sd_rw = off; // Holds the state of read/write of the 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 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 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 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 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", ""}, {"SD card", ""}}; {"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_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_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 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 String file_name = "measures"; // Initial file name only 8 characters allowed
float parameters[4]; // Parameters of the several voltages and current float parameters[4]; // Parameters of the several voltages and current
float denominator; // Denominator to calculate the voltage divider float denominator; // Denominator to calculate the voltage divider
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
// ------------------------------------------------------------------------------------------------------------------------ // ------------------------------------------------------------------------------------------------------------------------
// Function declarations -------------------------------------------------- // Function declarations --------------------------------------------------
void menuState(void); // Read buttons and update menu void menuState(void); // Read buttons and update menu
void printScreen (void); // Print menus and variables on screen void printScreen (void); // Print menus and variables on screen
float volt_pin (int pin); // Read and display voltage float volt_pin (int pin); // Read and display voltage
float amp_pin (int pin); // Read and display current 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 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(ACT, INPUT); // Button act is an input
pinMode(load_line, OUTPUT); // load_line is an output 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 // Voltage divider --> Vout = Vin * R2 / R1 + R2
denominator = (float)resistor2 / (resistor1 + resistor2); denominator = (float)resistor2 / (resistor1 + resistor2);
@@ -121,7 +124,7 @@ void setup()
sd_inserted = yes; // and change variable sd_inserted to yes sd_inserted = yes; // and change variable sd_inserted to yes
} }
else { 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 sd_inserted = no; // and change variable sd_inserted to no
} }
} }
@@ -130,10 +133,10 @@ void setup()
// Timer compare interrupt service routine -- // Timer compare interrupt service routine --
ISR(TIMER1_COMPA_vect) ISR(TIMER1_COMPA_vect)
{ {
menuState(); // Read buttons act accordingly and update menus menuState(); // Read buttons act accordingly and update menus
if (amp_pin(i_load) > curr_limit) // If load current more then current limit 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 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() void loop()
{ {
parameters[battery_volt] = volt_pin(v_batt); // Update Battery voltage value parameters[battery_volt] = volt_pin(v_batt); // Update Battery voltage value
parameters[voltage_1] = volt_pin(volt_1); // Update voltage one value parameters[voltage_1] = volt_pin(volt_1); // Update voltage one value
parameters[voltage_2] = volt_pin(volt_2); // Update voltage two value parameters[voltage_2] = volt_pin(volt_2); // Update voltage two value
parameters[curr] = amp_pin(i_load); // Update current load 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; sd_status = sd_not_in;
} }
else if (sd_rw == on) else if (sd_rw == on)
{ {
myFile = SD.open(file_name + ".TXT", FILE_WRITE); // Open file name and add ".TXT", for writing 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.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.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.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.print("Load current, ");
myFile.println(amp_pin(i_load)); // Write to card load current myFile.println(amp_pin(i_load)); // Write to card load current
myFile.println(""); // Write to card an empty line myFile.println(""); // Write to card an empty line
myFile.close(); // Close file myFile.close(); // Close file
sd_status = writing; sd_status = writing;
} }
else else
@@ -178,8 +181,8 @@ void loop()
sd_status = sd_in; sd_status = sd_in;
} }
printScreen(); // Refresh screen and print current menu and values printScreen(); // Refresh screen and print current menu and values
delay(delay_loop); // Repeat the main loop every delay_loop seconds delay(delay_loop); // Repeat the main loop every delay_loop seconds
} }
void menuState() void menuState()
@@ -195,8 +198,8 @@ 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(); // Refresh screen and print current menu and values printScreen(); // Refresh screen and print current menu and values
} }
if (!rLEFT) if (!rLEFT)
@@ -209,8 +212,8 @@ 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(); // Refresh screen and print current menu and values printScreen(); // Refresh screen and print current menu and values
} }
if (!rACT) if (!rACT)
@@ -223,8 +226,8 @@ void menuState()
{ {
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(); // Refresh screen and print current menu and values printScreen(); // Refresh screen and print current menu and values
} }
} }
@@ -260,8 +263,8 @@ float volt_pin(int pin)
{ {
float voltage; float voltage;
voltage = analogRead(pin); // Obtain RAW voltage data voltage = analogRead(pin); // Obtain RAW voltage data
voltage = (voltage / 1024) * 3.3; // Convert to actual voltage voltage = (voltage / 1024) * 3.3; // Convert to actual voltage
voltage = voltage / denominator; // Convert to voltage after divider voltage = voltage / denominator; // Convert to voltage after divider
return voltage; return voltage;
} }