Code: Select all
#include <Wire.h>
#include <Adafruit_GPS.h>
#include <Adafruit_TSL2561.h>
#include <SoftwareSerial.h>
#include <Adafruit_Sensor.h>
#include <AFMotor.h>
Adafruit_GPS GPS(&Serial1);
Adafruit_TSL2561 tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);
AF_DCMotor motor(2);
#define GPSECHO false //RAW GPS SENTENCES
boolean usingInterrupt = false; //Keeps track of whether we are using the interrupt
void useInterrupt(boolean); // Func prototype keeps Arduino 0023 happy
float tempC, tempVal, tempF, tempVolt, depthVolt, depthPsi, depthFeet;
float GPSDepth = 0;
float testDepth = 100;
boolean contact1, contact2, contact3, contact4, contact5, contact6;
int tempSensor = 1;
int depthSensor = 6;
int contact_1 = 22;
int contact_2 = 23;
int contact_3 = 24;
int contact_4 = 25;
int contact_5 = 26;
int contact_6 = 27;
long waitUntilSensors = 5000; // Wait 5 seconds
long waitUntilMotor = 0;
long waitUntilGPS = 5000;
String lat = "";
String latDir = "";
String longit = "";
String longitDir = "";
const float PSI_TO_FEET= 2.30665873688;
void setup()
{
Wire.begin();
Serial.begin(115200);
GPS.begin(9600);
tsl.setGain(TSL2561_GAIN_16X); //Allows sensor to adapt to lowlight situation
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
motor.setSpeed(255);
motor.run(RELEASE);
//____________________________________________________________________________________GPS_______________________________________________________________________
//GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); // Recommended minimum and GGA(fix datta) including altitude
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCONLY); //Recommended minimum only
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate, can change to 5 or 10, for parsing need 1 Hz
//GPS.sendCommand(PGCMD_ANTENNA); //Sends data on antenna status
useInterrupt(true);
delay(1000);
// Ask for firmware version
Serial1.println(PMTK_Q_RELEASE);
}
// Interrupt is called once a millisecond, looks for any new GPS data, and stores it
SIGNAL(TIMER0_COMPA_vect) {
char c = GPS.read();
// if you want to debug, this is a good time to do it!
#ifdef UDR0
if (GPSECHO)
if (c) UDR0 = c;
// writing direct to UDR0 is much much faster than Serial.print
// but only one character can be written at a time.
#endif
}
void useInterrupt(boolean v) {
if (v) {
// Timer0 is already used for millis() - we'll just interrupt somewhere
// in the middle and call the "Compare A" function above
OCR0A = 0xAF; //GPS address
TIMSK0 |= _BV(OCIE0A);
usingInterrupt = true;
}
else {
// do not call the interrupt function COMPA anymore
TIMSK0 &= ~_BV(OCIE0A);
usingInterrupt = false;
}
}
uint32_t timer = millis();
void loop()
{
if (digitalRead(contact_1)== HIGH)
{
contact1 = true;
}
if (digitalRead(contact_2) == HIGH)
{
contact2 = true;
}
if (digitalRead(contact_3) == HIGH)
{
contact3 = true;
}
if (digitalRead(contact_4) == HIGH)
{
contact4 = true;
}
if (digitalRead(contact_5) == HIGH)
{
contact5 = true;
}
if (digitalRead(contact_6) == HIGH)
{
contact6 = true;
}
if (! usingInterrupt) {
// read data from the GPS in the 'main loop'
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if (GPSECHO)
if (c) Serial.print(c);
}
if (millis() >= waitUntilSensors) //Print analog sensors once every second
{
sensors_event_t event;
tsl.getEvent(&event);
tempVal= analogRead(tempSensor);
tempVolt = tempVal * 5.0;
tempVolt /= 1024;
tempC = (tempVolt - .5) * 100;
tempF = (tempC * 9.0/5.0) + 32.0;
depthVolt = analogRead(depthSensor) * 5; //Takes 0-1023 reading and converts to volts by finding volts per unit
depthVolt /= 1024;//FIXED WIERD MATH PROBLEMS!!
depthPsi = (depthVolt/0.04)-12.5; // Depth Sensor Honeywell A Calibration: 10%-90% of supply voltage
depthFeet = (depthPsi-14.7) * PSI_TO_FEET; //(PSI) * conversion factor
Serial.print(contact1);
Serial.print(",");
Serial.print(contact2);
Serial.print(",");
Serial.print(contact3);
Serial.print(",");
Serial.print(contact4);
Serial.print(",");
Serial.print(contact5);
Serial.print(",");
Serial.print(contact6);
Serial.print(",");
contact1 = false;
contact2 = false;
contact3 = false;
contact4 = false;
contact5 = false;
contact6 = false;
Serial.print(depthFeet);
Serial.print(",");
Serial.print(depthPsi);
Serial.print(",");
Serial.print(tempF);
Serial.print(",");
if (event.light)
{
Serial.print(event.light);
Serial.print(",");
}
if (GPS.newNMEAreceived() && millis() > waitUntilGPS) {
String sentence = GPS.lastNMEA();
Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
Serial.print(",");
Serial.print(GPS.longitude, 4); Serial.print(GPS.lon);
Serial.println(",");
//}
}
waitUntilSensors = millis()+ 1000;
}
/*if(GPSDepth < testDepth && millis() > timer)
{
motor.run(FORWARD);
timer += 500;
GPSDepth += 1.02;
}
else if(GPSDepth > testDepth && millis() > timer)
{
motor.run(BACKWARD);
timer += 500;
GPSDepth -= 1.02;
}*/
}