Poniżej załączam kod programu.
Kod: Zaznacz cały
#include <SPI.h>
#include <SD.h>
#include <TinyGPS++.h>
#include <DHT.h>
#define ARDUINO_USD_CS 8 // uSD card CS pin (pin 10 on SparkFun GPS Logger Shield)
#define DHTPIN 3 // what digital pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
/////////////////////////
// Log File Defintions //
/////////////////////////
// Keep in mind, the SD library has max file name lengths of 8.3 - 8 char prefix,
// and a 3 char suffix.
// Our log files are called "gpslogXX.csv, so "gpslog99.csv" is our max file.
#define LOG_FILE_PREFIX "gpslog" // Name of the log file.
#define MAX_LOG_FILES 100 // Number of log files that can be made
#define LOG_FILE_SUFFIX "csv" // Suffix of the log file
char logFileName[14]; // Char string to store the log file name
// Data to be logged:
#define LOG_COLUMN_COUNT 14
char * log_col_names[LOG_COLUMN_COUNT] = {
"longitude", "latitude", "altitude", "speed", "course", "date", "time", "satellites", "wilgotnosc", "temperatura", "wartosc uchylenia przepustnicy", "kat skretu kierownicy", "hamulec,olej,komputer", "obroty"
}; // log_col_names is printed at the top of the file
//////////////////////
// Log Rate Control //
//////////////////////
#define LOG_RATE 2000 // Log every 5 seconds
unsigned long lastLog = 0; // Global var to keep of last time we logged
/////////////////////////
// TinyGPS Definitions //
/////////////////////////
TinyGPSPlus tinyGPS; // tinyGPSPlus object to be used throughout
#define GPS_BAUD 9600 // GPS module's default baud rate
/////////////////////////////////
// GPS Serial Port Definitions //
/////////////////////////////////
// If you're using an Arduino Uno, Mega, RedBoard, or any board that uses the
// 0/1 UART for programming/Serial monitor-ing, use SoftwareSerial:
#include <SoftwareSerial.h>
#define ARDUINO_GPS_RX 10 // GPS TX, Arduino RX pin
#define ARDUINO_GPS_TX 11 // GPS RX, Arduino TX pin
SoftwareSerial ssGPS(ARDUINO_GPS_RX, ARDUINO_GPS_TX); // Create a SoftwareSerial
// Set gpsPort to either ssGPS if using SoftwareSerial or Serial1 if using an
// Arduino with a dedicated hardware serial port
#define gpsPort ssGPS // Alternatively, use Serial1 on the Leonardo
// Define the serial monitor port. On the Uno, Mega, and Leonardo this is 'Serial'
// on other boards this may be 'SerialUSB'
#define SerialMonitor Serial
DHT dht(DHTPIN, DHTTYPE);
int half_revolutions = 0;
int rpm = 0;
unsigned long lastmillis = 0;
int potencjometr = A0; // odczyt napiecia z potentcjometru pinem analogowym 0
int wartosc = 0; // zmienna przechowujaca wartosc napiecia odczytanego
int pin_przycisk1 = A1;
int pin_przycisk2 = A2;
int pin_przycisk3 = A3;
int potencjometrkol1 = A8;
int potencjometrkol2 = A9;
int wartosc1 = 0;
int wartosc2 = 0;
int roznica = 0;
void printrpm_fan() {
half_revolutions++;
}
void setup()
{
SerialMonitor.begin(9600);
attachInterrupt(digitalPinToInterrupt(2), printrpm_fan, FALLING);
gpsPort.begin(GPS_BAUD);
dht.begin();
pinMode(2, INPUT);
pinMode(potencjometr, INPUT); //pin A0 (pot) jako wejście
pinMode(pin_przycisk1, INPUT_PULLUP); //pracuje jako wejscie
pinMode(pin_przycisk2, INPUT_PULLUP); //pracuje jako wejscie
pinMode(pin_przycisk3, INPUT); //pracuje jako wejscie
SerialMonitor.println("Setting up SD card.");
// see if the card is present and can be initialized:
if (!SD.begin(ARDUINO_USD_CS))
{
SerialMonitor.println("Error initializing SD card.");
}
Obroty();
updateFileName(); // Each time we start, create a new file, increment the number
printHeader(); // Print a header at the top of the new file
}
void loop()
{
if ((lastLog + LOG_RATE) <= millis())
{ // If it's been LOG_RATE milliseconds since the last log:
if (tinyGPS.time.isUpdated()) // If the GPS data is vaild
{
if (logGPSData()) // Log the GPS data
{
SerialMonitor.println("GPS logged."); // Print a debug message
Obroty();
printDTH();
printPOT();
printKAT();
printButton();
printObroty();
lastLog = millis(); // Update the lastLog variable
}
else // If we failed to log GPS
{ // Print an error, don't update lastLog
SerialMonitor.println("Failed to log new GPS data.");
}
}
}
// If we're not logging, continue to "feed" the tinyGPS object:
while (gpsPort.available())
tinyGPS.encode(gpsPort.read());
}
byte logGPSData()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile)
{ // Print longitude, latitude, altitude (in feet), speed (in mph), course
// in (degrees), date, time, and number of satellites.
logFile.print(tinyGPS.location.lng(), 6);
logFile.print(',');
logFile.print(tinyGPS.location.lat(), 6);
logFile.print(',');
logFile.print(tinyGPS.altitude.meters(), 1);
logFile.print(',');
logFile.print(tinyGPS.speed.kmph(), 1);
logFile.print(',');
logFile.print(tinyGPS.course.deg(), 1);
logFile.print(',');
logFile.print(tinyGPS.date.day());
logFile.print("/");
logFile.print(tinyGPS.date.month());
logFile.print("/");
logFile.print(tinyGPS.date.year());
logFile.print(',');
logFile.print(tinyGPS.time.hour() + 1);
logFile.print(":");
if (tinyGPS.time.minute() < 10) SerialMonitor.print('0');
logFile.print(tinyGPS.time.minute());
logFile.print(":");
if (tinyGPS.time.second() < 10) SerialMonitor.print('0');
logFile.print(tinyGPS.time.second());
logFile.print(',');
logFile.print(tinyGPS.satellites.value());
logFile.print(',');
logFile.close();
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
// printHeader() - prints our eight column names to the top of our log file
void printHeader()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile) // If the log file opened, print our column names to the file
{
int i = 0;
for (; i < LOG_COLUMN_COUNT; i++)
{
logFile.print(log_col_names[i]);
if (i < LOG_COLUMN_COUNT - 1) // If it's anything but the last column
logFile.print(','); // print a comma
else // If it's the last column
logFile.println(); // print a new line
}
logFile.close(); // close the file
}
}
// updateFileName() - Looks through the log files already present on a card,
// and creates a new file with an incremented file index.
void updateFileName()
{
int i = 0;
for (; i < MAX_LOG_FILES; i++)
{
memset(logFileName, 0, strlen(logFileName)); // Clear logFileName string
// Set logFileName to "gpslogXX.csv":
sprintf(logFileName, "%s%d.%s", LOG_FILE_PREFIX, i, LOG_FILE_SUFFIX);
if (!SD.exists(logFileName)) // If a file doesn't exist
{
break; // Break out of this loop. We found our index
}
else // Otherwise:
{
SerialMonitor.print(logFileName);
SerialMonitor.println(" exists"); // Print a debug statement
}
}
SerialMonitor.print("File name: ");
SerialMonitor.println(logFileName); // Debug print the file name
}
// printDate() formats the date into dd/mm/yy.
/**************
void printDate()
{
int dateInt = "";
logFile.print(tinyGPS.date.day());
logFile.print("/");
logFile.print(tinyGPS.date.month());
logFile.print("/");
logFile.println(tinyGPS.date.year());
}
// printTime() formats the time into "hh:mm:ss", and prints leading 0's
// where they're called for.
void printTime()
{
logFile.print(tinyGPS.time.hour()+1);
logFile.print(":");
if (tinyGPS.time.minute() < 10) SerialMonitor.print('0');
logFile.print(tinyGPS.time.minute());
logFile.print(":");
if (tinyGPS.time.second() < 10) SerialMonitor.print('0');
logFile.println(tinyGPS.time.second());
}
************/
void printDTH()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile)
{
float h = dht.readHumidity();
// Read temperature as Celsius (the default)
float t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
logFile.print(h);
logFile.print(" %\t");
logFile.print(',');
logFile.print(t);
logFile.print(" *C ");
logFile.print(',');
logFile.close(); // close the file
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
void printPOT()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile)
{
wartosc = analogRead(potencjometr);
wartosc = map(wartosc, 0, 1023, 0, 100); // skalowanie od 0 do 100
logFile.print(wartosc);
logFile.print(" %");
logFile.print(',');
logFile.close(); // close the file
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
void printKAT()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile)
{
wartosc1 = analogRead(potencjometrkol1);
wartosc1 = map(wartosc1, 0, 1023, 0, 450);
wartosc2 = analogRead(potencjometrkol2);
wartosc2 = map(wartosc2, 0, 1023, 0, 450);
roznica = wartosc1 - wartosc2;
logFile.print(roznica);
logFile.print(',');
logFile.close(); // close the file
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
void printButton()
{
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile)
{
int przycisk1 = digitalRead(pin_przycisk1); //odczytujemy stan przycisku
int przycisk2 = digitalRead(pin_przycisk2); //odczytujemy stan przycisku
int przycisk3 = digitalRead(pin_przycisk3); //odczytujemy stan przycisku
if (przycisk1 == HIGH) { //jesli przycisk nie jest wcisniety
logFile.print("Hamulec zwolniony");
}
else //w przeciwnym wypadku
{
logFile.print("Hamulec wcisniety");
}
logFile.print(',');
if (przycisk2 == HIGH) { //jesli przycisk nie jest wcisniety
logFile.print("Olej OK");
}
else //w przeciwnym wypadku
{
logFile.print("Niskie cisnienie oleju");
}
logFile.print(',');
if (przycisk3 == LOW) { //jesli przycisk nie jest wcisniety
logFile.print("Komputer OK");
}
else //w przeciwnym wypadku
{
logFile.print("Awaria ukladu wtryskowego");
}
logFile.print(',');
logFile.close(); // close the file
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
void Obroty()
{
if (millis() - lastmillis == 1000) { //Uptade every one second, this will be equal to reading frecuency (Hz).
detachInterrupt(digitalPinToInterrupt(2));//Disable interrupt when calculating
rpm = half_revolutions * 30; // Convert frecuency to RPM, note: this works for one interruption per full rotation. For two interrups per full rotation use half_revolutions * 30.
//Serial.print("RPM =\t"); //print the word "RPM" and tab.
//logFile.print(rpm); // print the rpm value.
// logFile.println(); // print a new line
// SerialMonitor.println(rpm);
//Serial.print("\t Hz=\t"); //print the word "Hz".
//Serial.println(half_revolutions); //print revolutions per second or Hz. And print new line or enter.
//logFile.print(',');
half_revolutions = 0; // Restart the RPM counter
lastmillis = millis(); // Uptade lasmillis
attachInterrupt(digitalPinToInterrupt(2), printrpm_fan, FALLING); //enable interrupt
}
}
void printObroty(){
File logFile = SD.open(logFileName, FILE_WRITE); // Open the log file
if (logFile)
{
Obroty();
logFile.print(rpm);
logFile.println(); // print a new line
SerialMonitor.println(rpm);
logFile.close(); // close the file
return 1; // Return success
}
return 0; // If we failed to open the file, return fail
}
Pozdrawiam