In my project that I am doing, I have a GPS module logging the longitude and latitude to an SD card, while I have an IR reciever controlling 2 motors. I have a test GPS program that works well and logs the lat/long to the SD card, but when i combine that with the IR code, the serial monitor shows no fix, even though the fix light is off on the module (no light means there is a fix). is there compatibility issues with these parts? Any help is appreciated!
#include <Arduino.h>
#include <IRremote.h> //including the remote library
// SD card setup on the ethernet sheild
#include <Adafruit_GPS.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <SD.h>
#define sdpin 4
File logfile;
//---------------------------------------------------------------------------------
// these are the IR codes that I got from the IR revieve dump example code that is given in the IR remote library
#define up_button 3877175040 // code received from up button
#define left_button 4144561920 // code received from left button
#define ok_button 3810328320 // code received from ok button
#define right_button 2774204160 // code received from right button
#define down_button 2907897600 // code received from down button
//---------------------------------------------------------------------------------
// Pin definitions for the direction and pwm pins that go to my motor driver
#define pwm1 10 // digital pin 10
#define pwm2 11 // digital pin 11
#define dir1 12 // digital pin 12
#define dir2 13 // digital pin 13
//---------------------------------------------------------------------------------
SoftwareSerial mySerial(8, 7);// Serial Ports defined as pin 8 and 7
Adafruit_GPS GPS(&mySerial);
// Define the GPS Echo as True
#define GPSECHO true
//---------------------------------------------------------------------------------
// unsigned int maxpwm = 2.55; // maxpwm * %dutycycle (relate to speed at 100% = 255)
float speed = 0; //initialize speed variable to zero
int previoust = 0;
int reciever_pin = 2; //output pin of IR receiver to pin 2 of arduino
IRrecv receiver(reciever_pin); //Arduino will take output of IR receiver from pin 2
decode_results output;
void setup()
{
Serial.begin(115200); // start the serial Monitor at 9600 Baud
pinMode(pwm1, OUTPUT); // set pwm1 as an output
pinMode(pwm2, OUTPUT); // set pwm2 as an output
pinMode(dir1, OUTPUT); // set dir1 as an output
pinMode(dir2, OUTPUT); // set dir2 as an output
IrReceiver.begin(reciever_pin, ENABLE_LED_FEEDBACK); // begin the Infared reciever
GPS.begin(9600); // Start the GPS at 9600 Baud
GPS.sendCommand(PMTK_SET_NMEA_OUTPUT_RMCGGA); // Includes the reccomended minimum and fix data
GPS.sendCommand(PMTK_SET_NMEA_UPDATE_1HZ); // 1 Hz update rate
GPS.sendCommand(PGCMD_ANTENNA); // request updates on the antenna status
delay(1000);
// Ask for firmware version
mySerial.println(PMTK_Q_RELEASE);
}
uint32_t timer = millis();
void loop(){
IRREAD();
char c = GPS.read();
// if you want to debug, this is a good time to do it!
if ((c) && (GPSECHO))
Serial.write(c);
// if a sentence is received, we can check the checksum, parse it...
if (GPS.newNMEAreceived()) {
// a tricky thing here is if we print the NMEA sentence, or data
// we end up not listening and catching other sentences!
// so be very wary if using OUTPUT_ALLDATA and trytng to print out data
//Serial.println(GPS.lastNMEA()); // this also sets the newNMEAreceived() flag to false
if (!GPS.parse(GPS.lastNMEA())) // this also sets the newNMEAreceived() flag to false
return; // we can fail to parse a sentence in which case we should just wait for another
}
// approximately every 2 seconds or so, print out the current stats
if (millis() - timer > 2000) {
timer = millis(); // reset the timer
Serial.print("\nTime: ");
if (GPS.hour < 10) { Serial.print('0'); }
Serial.print(GPS.hour, DEC); Serial.print(':');
if (GPS.minute < 10) { Serial.print('0'); }
Serial.print(GPS.minute, DEC); Serial.print(':');
if (GPS.seconds < 10) { Serial.print('0'); }
Serial.print(GPS.seconds, DEC); Serial.print('.');
if (GPS.milliseconds < 10) {
Serial.print("00");
} else if (GPS.milliseconds > 9 && GPS.milliseconds < 100) {
Serial.print("0");
}
Serial.println(GPS.milliseconds);
Serial.print("Date: ");
Serial.print(GPS.day, DEC); Serial.print('/');
Serial.print(GPS.month, DEC); Serial.print("/20");
Serial.println(GPS.year, DEC);
Serial.print("Fix: "); Serial.print((int)GPS.fix);
Serial.print(" quality: "); Serial.println((int)GPS.fixquality);
if (GPS.fix) {
Serial.print("Location: ");
Serial.print(GPS.latitude, 4); Serial.print(GPS.lat);
Serial.print(", ");
Serial.print(GPS.longitude, 4); Serial.println(GPS.lon);
// the following gives coordinates that you can use to plug into google maps (degrees)
Serial.println("location in degrees");
Serial.print(GPS.latitudeDegrees,8);
Serial.print(",");
Serial.print(GPS.longitudeDegrees,8);
Serial.print("Speed (knots): "); Serial.println(GPS.speed);
Serial.print("Angle: "); Serial.println(GPS.angle);
Serial.print("Altitude: "); Serial.println(GPS.altitude);
Serial.print("Satellites: "); Serial.println((int)GPS.satellites);
Serial.print("Antenna status: "); Serial.println((int)GPS.antenna);
// speed calculation in km/h => 1 knot = 1.852 km/h
float kph;
kph = (GPS.speed) * 1.852;
Serial.print("Speed: ");
Serial.println(kph);
Serial.print("Latitude: ");
Serial.println(GPS.latitudeDegrees,8);
Serial.print("Longitude: ");
Serial.println(GPS.longitudeDegrees,8);
// write to SD card
logfile = SD.open("gpslog.txt", FILE_WRITE);
String timeStamp = String(GPS.day, DEC) + (",") + String(GPS.month, DEC) + (",") + String(GPS.year, DEC) + (" - ")+ String(GPS.hour, DEC) + (":") + String(GPS.minute, DEC) + (":") + String(GPS.seconds, DEC);
if (logfile) {
// logfile.print("Time Stamp: ");
// logfile.print(timeStamp);
// logfile.print(" Speed: ");
// logfile.print(kph);
// logfile.print(" Lat/long: ");
logfile.print(GPS.latitudeDegrees, 8);
logfile.print(", ");
logfile.println(GPS.longitudeDegrees, 8);
logfile.close();
}
}
}
}
// user created functions
// Code for reading the signals for the Infared Sensor from the remote
void IRREAD()
{
if (IrReceiver.decode()){
Serial.println(IrReceiver.decodedIRData.decodedRawData);
if(IrReceiver.decodedIRData.decodedRawData == ok_button)
{
speed = 0;
}
if(IrReceiver.decodedIRData.decodedRawData == up_button)
{
speed += 25.5; // increase speed by 10% when the user presses the up button on the remote
digitalWrite(dir1,LOW);
digitalWrite(dir2,LOW);
}
if(IrReceiver.decodedIRData.decodedRawData == down_button)
{
speed += 25.5; // increase speed by 10% when the user presses the up button on the remote
digitalWrite(dir1,HIGH);
digitalWrite(dir2,HIGH);
}
if(IrReceiver.decodedIRData.decodedRawData == left_button)
{
speed = 191.25; // 75% of the max pwm
digitalWrite(dir1,HIGH);
digitalWrite(dir2,LOW);
}
if(IrReceiver.decodedIRData.decodedRawData == right_button)
{
speed = 191.25;
digitalWrite(dir1,LOW);
digitalWrite(dir2,HIGH);
}
Serial.print(speed/2.55);
analogWrite(pwm1,speed);
analogWrite(pwm2,speed);
IrReceiver.resume();
}
}