I have a sketch of an echo sounder that works perfectly but when I try to add another device such as an accelerometer, a magnetometer or a gps that I have the sounder fails, I send the sketch of the sounder
#include <TinyGPS++.h>
#include <SD.h>
int ent=3;
int sal=2;
// The TinyGPS++ object
TinyGPSPlus sounder;
TinyGPSCustom depth(sounder, "SDDBT", 3);
const int chipSelect = 4;
void setup()
{
Serial.begin(9600);
Serial1.begin(4800);
pinMode(ent,INPUT);
pinMode(sal,OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
File logFile = SD.open("Sounder.csv", FILE_WRITE);
if (logFile)
{
logFile.println(" "); //Just a leading blank line, incase there was previous data
String header = "DEPTH";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
}
uint32_t timer = millis();
void loop()
{
if(digitalRead(ent)==LOW){
digitalWrite(sal,HIGH);
}
else{
digitalWrite(sal,LOW);
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
// Every time anything is updated, print everything.
if (depth.isUpdated())
{
Serial.print(F("DEPTH="));
Serial.print(depth.value());
Serial.println(" M");
File logFile = SD.open("Sounder.csv", FILE_WRITE);
if(logFile){
logFile.println(depth.value());
logFile.close();
}
}
while (Serial1.available() > 0)
sounder.encode(Serial1.read());
}
}
And the code with an accelerometer adxl322
#include <TinyGPS++.h>
#include <SD.h>
int ent=3;
int sal=2;
// The TinyGPS++ object
TinyGPSPlus sounder;
TinyGPSCustom depth(sounder, "SDDBT", 3);
//Definimos as variables e os pins da parte do acelerometro
const int accelpinx = A3; // Definimos os pins do acelerometro X&Y
const int accelpiny = A2;
int xval = 0; // Variable usada para representar os valores do pin do acelerometro.
int yval = 0;
int xaval; //Variable do angulo do acelerometro respecto a X
float xaaval;
int xtilt;
int ytilt;
int yaval; //Variable do angulo do acelerometro respecto a Y
float yaaval;
const int chipSelect = 4;
void setup()
{
Serial.begin(9600);
Serial1.begin(4800);
pinMode(ent,INPUT);
pinMode(sal,OUTPUT);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.print("Initializing SD card...");
// make sure that the default chip select pin is set to
// output, even if you don't use it:
pinMode(10, OUTPUT);
// see if the card is present and can be initialized:
if (!SD.begin(chipSelect)) {
Serial.println("Card failed, or not present");
// don't do anything more:
return;
}
Serial.println("card initialized.");
File logFile = SD.open("Sounder.csv", FILE_WRITE);
if (logFile)
{
logFile.println(" "); //Just a leading blank line, incase there was previous data
String header = "DEPTH";
logFile.println(header);
logFile.close();
Serial.println(header);
}
else
{
Serial.println("Couldn't open log file");
}
}
uint32_t timer = millis();
void loop()
{
xval = analogRead(accelpinx); // Leemos os valores do accelerometro entre: 0 - 1023
yval = analogRead(accelpiny);
xval = constrain(xval,240,428); //Limite de erros e valores atipicos debido ao ruído
yval = constrain(yval,240,428); //Limite de erros e valores atipicos debido ao ruído
xaaval = (xval*-0.0116) + 3.883;
yaaval = (yval*-0.0116) + 3.883;
xtilt = (asin(xaaval/1)*180)/3.1416; //determina o ángulo de inclinación en graos
ytilt = (asin(yaaval/1)*180)/3.1416; //determina o ángulo de inclinación en graos
//xtilt = constrain(xtilt,-36,56); //Restricción debido a limites mecanicos na X
//ytilt = constrain(ytilt,-56,30); //Restricción debido a limites mecanicos na Y
if(digitalRead(ent)==LOW){
digitalWrite(sal,HIGH);
}
else{
digitalWrite(sal,LOW);
}
// if millis() or timer wraps around, we'll just reset it
if (timer > millis()) timer = millis();
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
// Every time anything is updated, print everything.
if (depth.isUpdated())
{
Serial.print(F("DEPTH="));
Serial.print(depth.value());
Serial.println(" M");
File logFile = SD.open("Sounder.csv", FILE_WRITE);
if(logFile){
logFile.println(depth.value());
logFile.close();
}
}
while (Serial1.available() > 0)
sounder.encode(Serial1.read());
}
}
my intention is to add a GPS(adafruit GPS breakout R3), one magnetometer(HMC6352) and one accelerometer to build a prototype to make bathymetric surveys.