hi,
im have setup 2 alone codes working one for gps another for the temperature, now im trying to put them together, but im failing to add the temperature loop. the code only run one time, and i need him to keep running getting gps info + temperature
void loop()
{
bool newdata = false;
unsigned long start = millis();
while (millis() - start < 500)
{
if (feedgps())
newdata = true;
}
gpsdump(gps);
// temperature sensor
float temperature = getTemp();
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
Serial.print(temperatureC); Serial.print("degreesC");Serial.print(" | ");Serial.print(temperature); Serial.println(" Waterproof ");
// temperature end
}
i cant find the error, and i'm not a pro coder.
The full code used his here
#include <SoftwareSerial.h>
#include <TinyGPS.h>
#include <SD.h>
#include <OneWire.h>
File myFile;
TinyGPS gps;
SoftwareSerial nss(2, 3);
int DS18S20_Pin = 10;
int sensorPin = 0;
OneWire ds(DS18S20_Pin);
void setup()
{
Serial.begin(115200);
nss.begin(115200);
Serial.println("Reading GPS");
// Initialize SD card
Serial.print("Initializing SD card...");
pinMode(8, OUTPUT);
if (!SD.begin(10)) {
Serial.println("initialization failed!");
return;
}
Serial.println("initialization done.");
}
void loop()
{
bool newdata = false;
unsigned long start = millis();
while (millis() - start < 500)
{
if (feedgps())
newdata = true;
}
gpsdump(gps);
float temperature = getTemp();
int reading = analogRead(sensorPin);
float voltage = reading * 5.0;
voltage /= 1024.0;
float temperatureC = (voltage - 0.5) * 100 ;
Serial.print(temperatureC); Serial.print(" degrees C - Pequeno");Serial.print(" ||||||| ");Serial.print(temperature); Serial.println(" degrees C - Waterproof");
}
// Get and process GPS data
void gpsdump(TinyGPS &gps) {
float flat, flon;
unsigned long age;
gps.f_get_position(&flat, &flon, &age);
Serial.print(flat, 4);
Serial.print(", ");
Serial.print(flon, 4);
/// And write it to SD card
myFile = SD.open("tracking.txt", FILE_WRITE);
// if the file opened okay, write to it:
if (myFile) {
Serial.print("Writing to tracking.txt...");
myFile.print(flat, 4);
myFile.print(", ");
myFile.println(flon, 4);
// close the file:
myFile.close();
Serial.println("done.");
} else {
// if the file didn't open, print an error:
Serial.println("error opening tracking.txt");
}
///
}
// Feed data as it becomes available
bool feedgps()
{ while (nss.available())
{
if (gps.encode(nss.read()))
return true;
}
return false;
}
float getTemp(){
byte data[12];
byte addr[8];
if ( !ds.search(addr)) {
ds.reset_search();
return -1000; }
if ( OneWire::crc8( addr, 7) != addr[7]) {
Serial.println("CRC is not valid!");
return -1001; }
if ( addr[0] != 0x10 && addr[0] != 0x28) {
Serial.print("Device is not recognized");
return -1002;}
ds.reset();
ds.select(addr);
ds.write(0x44,1);
byte present = ds.reset();
ds.select(addr);
ds.write(0xBE);
for (int i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read(); }
ds.reset_search();
byte MSB = data[1];
byte LSB = data[0];
float tempRead = ((MSB << 8) | LSB); //using two's compliment
float TemperatureSum = tempRead / 16;
return TemperatureSum;
}
thanks for the help
jGaf