I have a problem assembling two separate codes together, One of them is using a serial wireless module to control a DC motor from another Arduino which works pretty fine alone, the other one is a GPS module code to get data from it which works fine as well alone.
I want to mix both codes together but somehow when I do this, Arduino executes GPS code only and doesn't respond to the orders I give to the DC motor. I need your help to solve this programming problem.
DC motor code:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
const int pwm = 6;
char x;
void setup() {
pinMode(pwm, OUTPUT);
mySerial.begin(9600);
Serial.begin(57600);
analogWrite(pwm, 255);
}
void loop() {
if(mySerial.available()){
x=mySerial.read();
Serial.print(x);
if (x=='a') {
analogWrite(pwm, 128);
}
if (x=='b'){
analogWrite(pwm, 255);
}
if (x=='c'){
analogWrite(pwm, 0);
}
if (x=='d'){
analogWrite(pwm, 0);
}
delay(2);
}
}
GPS module code:
/*
This Sketch will run with the SkyNav SKM53 GPS
RXD Arduino Pin 3
TXD Arduino Pin 2
RST Leave Open
NC Leave Open
GND Ground
VCC +5
Make sure you download and save to your Arduino/Libraries folder TinyGPS.h
and NewSoftSerial.h files.
*/
#include <TinyGPS.h>
#include <SoftwareSerial.h>
unsigned long fix_age;
SoftwareSerial GPS(2,3);
TinyGPS gps;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
long lat, lon;
float LAT, LON;
void setup(){
GPS.begin(9600);
Serial.begin(9600);
}
void loop(){
long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
// retrieves +/- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);
getGPS();
Serial.write(Serial.print("Latitude : "));
Serial.write(Serial.print(LAT/100000,7));
Serial.write(Serial.print(" :: Longitude : "));
Serial.write(Serial.println(LON/100000,7));
}
void getGPS(){
bool newdata = false;
unsigned long start = millis();
// Every 1 seconds we print an update
while (millis() - start < 1000)
{
if (feedgps ()){
newdata = true;
}
}
if (newdata)
{
gpsdump(gps);
}
}
bool feedgps(){
while (GPS.available())
{
if (gps.encode(GPS.read()))
return true;
}
return 0;
}
void gpsdump(TinyGPS &gps)
{
//byte month, day, hour, minute, second, hundredths;
gps.get_position(&lat, &lon);
LAT = lat;
LON = lon;
{
feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors
}
}
Both code together:
#include <SoftwareSerial.h>
#include <TinyGPS.h>
unsigned long fix_age;
SoftwareSerial mySerial(10, 11); // RX, TX
SoftwareSerial GPS(2,3);
TinyGPS gps;
void gpsdump(TinyGPS &gps);
bool feedgps();
void getGPS();
long lat, lon;
float LAT, LON;
const int pwm = 6;
char x;
void setup() {
pinMode(pwm, OUTPUT);
mySerial.begin(9600);
GPS.begin(57600);
Serial.begin(57600);
analogWrite(pwm, 255);
}
void loop() {
if(mySerial.available()){
x=mySerial.read();
Serial.print(x);
if (x=='a') {
analogWrite(pwm, 128);
}
if (x=='b'){
analogWrite(pwm, 255);
}
if (x=='c'){
analogWrite(pwm, 0);
}
if (x=='d'){
analogWrite(pwm, 0);
}
delay(2);
}
long lat, lon;
unsigned long fix_age, time, date, speed, course;
unsigned long chars;
unsigned short sentences, failed_checksum;
// retrieves +/- lat/long in 100000ths of a degree
gps.get_position(&lat, &lon, &fix_age);
getGPS();
Serial.print("Latitude : ");
Serial.print(LAT/100000,7);
Serial.print(" :: Longitude : ");
Serial.println(LON/100000,7);
}
void getGPS(){
bool newdata = false;
unsigned long start = millis();
// Every 1 seconds we print an update
while (millis() - start < 1000)
{
if (feedgps ()){
newdata = true;
}
}
if (newdata)
{
gpsdump(gps);
}
}
bool feedgps(){
while (GPS.available())
{
if (gps.encode(GPS.read()))
return true;
}
return 0;
}
void gpsdump(TinyGPS &gps)
{
//byte month, day, hour, minute, second, hundredths;
gps.get_position(&lat, &lon);
LAT = lat;
LON = lon;
{
feedgps(); // If we don't feed the gps during this long routine, we may drop characters and get checksum errors
}
}