Hi all , I'm currently using Arduino UNO R3 , Cytron's SMK53 GPS shield and DFRobot's L298P shield v1.2 for my project . Both its original written code can be found at http://tutorial.cytron.com.my/2015/03/11/using-cytron-skm53-gps-shield-with-ct-uno/ and Arduino_Motor_Shield__L298N___SKU_DRI0009_-DFRobot
I had a SLIGHT idea about PWM , but I'm have no clue for what PLL does for the motor shield part even after reading the wiki . However , on my mini project I'm using PLL .
#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
LiquidCrystal lcd(8,9,13,12,11,10); //Pin initialization for LCD
SoftwareSerial ss(2,3); //Software serial pin for SKM53 shield
TinyGPS gps;
//const int ButtonKey=A0; //Button pin for LCD keypad shield
boolean NewData=false;
boolean State=0;
float flat;
float flon;
float InitialLatitude=3.0635199546; //Latitude (horizontal line) above (North side;+ve) the equator
float InitialLongitude=101.61813354; //Longitde (vertical line) to the right (East side;+ve) of prime meridian
int E1=6; //Direction control for motor 1
int E2=5; //Direction control for motor 2
int M1=7; //Enable control for motor 1
int M2=4; //Enable control for motor 2
String IncomingCommand ;
void setup(){
//pinMode(ButtonKey,INPUT);
lcd.begin(16,2);
lcd.print("Acquiring GPS ......");
ss.begin(9600);
//Serial.begin(9600);
pinMode(E1,OUTPUT);
pinMode(E2,OUTPUT);
pinMode(M1,OUTPUT);
pinMode(M2,OUTPUT);
}
void loop(){
GetGPSPosition();
PrintPositionOnLCD();
EastSideGliding();
WestSideGliding();
}
void GetGPSPosition(){
while(ss.available()){
char c=ss.read();
if(gps.encode(c)){
NewData=true;
gps.f_get_position(&flat,&flon);
}
}
}
void PrintPositionOnLCD(){
if(NewData){
lcd.clear();
if(State==0){
lcd.setCursor(0,0);
lcd.print("Lat ");
lcd.setCursor(4,0); //Print position (x,y)
lcd.print(flat,10); //(......,precision)
lcd.setCursor(0,1);
lcd.print("Lon ");
lcd.setCursor(4,1);
lcd.print(flon,10);
}
//NewData=false;
delay(1000); //Refresh time
}
}
//Set parachute to glide to the East side
void EastSideGliding(){
if(NewData){
if(flon<InitialLongitude){
int i=1;
while(flon<InitialLongitude){
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E2,255);
delay(3000);
digitalWrite(M2,LOW);
i=i+1;
delay(10000); //Let parachute glide for 10s
GetGPSPosition();
PrintPositionOnLCD();
NewData=false;
if(flon==InitialLongitude){
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E2,0);
delay(i*3000);
digitalWrite(M2,LOW);
break;
}
}
}
}
}
//Set parachute to glide to the West side
void WestSideGliding(){
if(NewData){
if(flon>InitialLongitude){
int i=1;
while(flon>InitialLongitude){
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1,255);
delay(3000);
digitalWrite(M1,LOW);
i=i+1;
delay(10000); //Let parachute glide for 10s
GetGPSPosition();
PrintPositionOnLCD();
NewData=false;
if(flon==InitialLongitude){
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1,0);
delay(i*3000);
digitalWrite(M1,LOW);
break;
}
}
}
}
}
/*
void CommandFromSerialMonitorToMotor(){
while(Serial.available()==0){
}
IncomingCommand=Serial.readString();
//Forward movement for both motors
if(IncomingCommand=="Q"){
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1,255);
analogWrite(E2,255);
}
else if(IncomingCommand=="W"){
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1,255);
analogWrite(E2,255);
}
//Backward movement for both motors
if(IncomingCommand=="A"){
digitalWrite(M1,HIGH);
digitalWrite(M2,HIGH);
analogWrite(E1,0);
analogWrite(E2,0);
}
else if(IncomingCommand=="S"){
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1,0);
analogWrite(E2,0);
}
//Forward movement for motor 1
if(IncomingCommand=="E"){
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1,255);
analogWrite(E2,255);
}
else if(IncomingCommand=="R"){
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1,255);
analogWrite(E2,255);
}
//Backward movement for motor 1
if(IncomingCommand=="D"){
digitalWrite(M1,HIGH);
digitalWrite(M2,LOW);
analogWrite(E1,0);
analogWrite(E2,0);
}
else if(IncomingCommand=="F"){
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1,0);
analogWrite(E2,0);
}
//Forward movement for motor 2
if(IncomingCommand=="T"){
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1,255);
analogWrite(E2,255);
}
else if(IncomingCommand=="Y"){
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1,255);
analogWrite(E2,255);
}
//Backward movement for motor 2
if(IncomingCommand=="G"){
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E1,0);
analogWrite(E2,0);
}
else if(IncomingCommand=="H"){
digitalWrite(M1,LOW);
digitalWrite(M2,LOW);
analogWrite(E1,0);
analogWrite(E2,0);
}
}
*/
This is code I modified . Currently , I'm only working on longitude part with 1 Tamiya gearbox (My mini project will have 2 Tamiya gearbox with 2 motors in 1 gearbox operating on latitude part and 2 more motors in another gearbox operating on longitude part , I'm still in the testing phase .)
From the original codes , I understand FULLY WELL for the motor shield one but not really the GPS shield one as it include some library file which I'm not familiar with it . So , most of my GPS code I just followed the original one .
The overall of this written code will be like this (or at least planned to be like this) . Firstly , it will get the latitude and longitude position at my standing position . Then , it will show the latitude and longitude data on the LCD for my current position . IF ONLY the GPS position is present , the motor will move (for now is only on longitude side) base on the detected position and my initial position .
I still can't tell whether my motor rotated correctly based on my code as the data for the GPS position only shows the 1st detected position on my LCD . After that , even though I placed the when it is in the GetGPSPosition() and the PrintPositionOnLCD() functions inside the while loop , it doesn't overwrite the 1st data to the latest one .
I personally think that there is some error around here but still couldn't figure where and how to fix it . The NewData variable smells fishy to me .
//Set parachute to glide to the East side
void EastSideGliding(){
if(NewData){
if(flon<InitialLongitude){
int i=1;
while(flon<InitialLongitude){
digitalWrite(M1,LOW);
digitalWrite(M2,HIGH);
analogWrite(E2,255);
delay(3000);
digitalWrite(M2,LOW);
i=i+1;
delay(10000); //Let parachute glide for 10s
GetGPSPosition();
PrintPositionOnLCD();
NewData=false;
Help is really appreciated
. Thank you !