i want to Serial communicate my two boards together and the GPS is also communicating Serial can i have any example code
After that many posts you should know that is not how it works.
Make a start and ask what you are stuck on.
ok here are the two Programs one is the main that is printing the GPS data that code is
#include <math.h>
#include <nmea.h>
#include <LiquidCrystal.h>
#include <NewSoftSerial.h>
NewSoftSerial mygps(7, 8);
NewSoftSerial 2board(9, 10);
NMEA myGPS(GPRMC);
float dest_latitude = 25.37104;
float dest_longitude = 68.35567;
const int potPin = A0;
const int leftMotor = 12; //PWM pin to the L293
const int rightMotor = 11; //PWM pin to the L293
int motorA1=2;
int motorA2=3;
char Dir; //9,10,4
bool isLeft() {
return (analogRead(potPin)/4) <30;
}
bool isLeftOfCenter() {
return (analogRead(potPin)/4) < 108;
}
bool isRightOfCenter() {
return (analogRead(potPin)/4) > 110;
}
bool isRight() {
return (analogRead(potPin)/4) > 147;
}
void setup() {
2board.begin(4800);
mygps.begin(4800);
pinMode(13, OUTPUT); // diagnostic led on Wiring-board
pinMode(leftMotor,OUTPUT);
pinMode(rightMotor,OUTPUT);
pinMode(motorA1,OUTPUT);
pinMode(motorA2,OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
if (mygps.available() > 0 ) {
if (myGPS.decode(mygps.read())) {
float lat;
float lon;
lat=myGPS.gprmc_latitude();
lon = myGPS.gprmc_longitude();
float d=(round(myGPS.gprmc_distance_to(dest_latitude, dest_longitude, MTR)));
2board.print(d);
if (d < 5){
digitalWrite(13, HIGH);
carbreak();
}
if (d > 5){
digitalWrite(13, LOW);
float mydir= 0;
for (int x = 0; x<20; x=x+1){
mydir=mydir + calc_bearing (lat, lon, dest_latitude, dest_longitude);
}
mydir=mydir/20;
if (mydir < 0) {
mydir += 360;
}
if (mydir > 180) {
mydir -= 360;
}
// display relative direction to destination
if (mydir < 0) {
motorForward();
delay(1000);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
steering('L');
delay(1000); // destination is to your left
steering('C');
delay(1000);
}
else {
// destination is to your right
motorForward();
delay(1000);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
steering('R');
delay(1000); // destination is to your left
steering('C');
delay(1000);
}
}
}
}
}
void motorStop() {
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
}
void motorLeft() {
digitalWrite(leftMotor, HIGH);
}
void motorRight() {
digitalWrite(rightMotor, HIGH);
}
//back wheels
void motorForward()
{
digitalWrite(motorA1,HIGH);
digitalWrite(motorA2,LOW);
}
void motorBackward()
{
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,HIGH);
}
void carbreak()
{
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,LOW);
}
void steering(char Dir)
{
//if (Serial.available() == 0)
//return;
//Dir=toupper(Serial.read());
switch (Dir)
{
case 'L':
//Serial.println("New command: L");
if (isLeft())
{
}//Serial.println("Already Left");
else
{
//Serial.println("Heading Left");
motorLeft();
while (!isLeft()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Left");
}
break;
case 'C':
//Serial.println("New command: C");
if (isRightOfCenter())
{
//Serial.println("Heading Left toward Center");
motorLeft();
while (isRightOfCenter()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Centered");
}
else
if (isLeftOfCenter())
{
//Serial.println("Heading Right toward Center");
motorRight();
while (isLeftOfCenter()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Centered");
}
else
{
}
//Serial.println("Already Centered");
break;
case 'R':
//Serial.println("New command: R");
if (isRight())
{
}//Serial.println("Already Right");
else
{
//Serial.println("Heading Right");
motorRight();
while (!isRight()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Right");
}
break;
} // end of switch on direction
}
int calc_bearing(float flat1, float flon1, float flat2, float flon2)
{
float calc;
float bear_calc;
float x = 69.1 * (flat2 - flat1);
float y = 69.1 * (flon2 - flon1) * cos(flat1/57.3);
calc=atan2(y,x);
bear_calc= degrees(calc);
if(bear_calc<=1){
bear_calc=360+bear_calc;
}
return bear_calc;
}
and here is the code that is reading the data
#include <math.h>
#include <LiquidCrystal.h>
#include <NewSoftSerial.h>
NewSoftSerial newboard(7, 8);
LiquidCrystal myLCD = LiquidCrystal(12, 11, 5, 4, 3, 2);
// khali pin 6
void setup() {
newboard.begin(9600);
myLCD.begin(16,2);
pinMode(13, OUTPUT); // diagnostic led on Wiring-board
pinMode(6, OUTPUT);
myLCD.clear();
myLCD.home();
}
void loop() {
if (newboard.available()>0){
int d=newboard.read();
myLCD.setCursor(0,0);
myLCD.print("D:");
newboard.read(d);
myLCD.print(d);
myLCD.print("m");
float mydir;
myLCD.setCursor(1,1);
if (mydir < 0) {
myLCD.print("TURN LEFT");
}
else {
myLCD.print("TURN RIGHT");
}
myLCD.print(abs(mydir), DEC);
myLCD.print(223, BYTE); // print °-character
myLCD.setCursor(8,0);
if (d < 5)
{
myLCD.clear();
myLCD.home();
myLCD.setCursor(8,0);
myLCD.print("TRGET OK");
}
}
...and the second part of Mike's post?
2board
??
Braces need to match.
i mean 2 say 2nd board
the DATA is not displayed on the LCD
Does it even compile?
This won't help:
void setup() {
2board.begin(4800);
void setup() {
newboard.begin(9600);
This open brace is not closed:
if (newboard.available()>0){
int d=newboard.read();
they both are compiling but they are not communicating
Your code as posted cannot compile for a number of reasons, so why don't you stop this?
k then i have changed the code
#include <math.h>
#include <nmea.h>
#include <LiquidCrystal.h>
#include <NewSoftSerial.h>
NewSoftSerial mygps(7, 8);
NewSoftSerial board(9, 10);
NMEA myGPS(GPRMC);
float dest_latitude = 25.37104;
float dest_longitude = 68.35567;
const int potPin = A0;
const int leftMotor = 12; //PWM pin to the L293
const int rightMotor = 11; //PWM pin to the L293
int motorA1=2;
int motorA2=3;
char Dir; //9,10,4
bool isLeft() {
return (analogRead(potPin)/4) <30;
}
bool isLeftOfCenter() {
return (analogRead(potPin)/4) < 108;
}
bool isRightOfCenter() {
return (analogRead(potPin)/4) > 110;
}
bool isRight() {
return (analogRead(potPin)/4) > 147;
}
void setup() {
board.begin(4800);
mygps.begin(4800);
pinMode(13, OUTPUT); // diagnostic led on Wiring-board
pinMode(leftMotor,OUTPUT);
pinMode(rightMotor,OUTPUT);
pinMode(motorA1,OUTPUT);
pinMode(motorA2,OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);
}
void loop() {
if (mygps.available() > 0 ) {
if (myGPS.decode(mygps.read())) {
float lat;
float lon;
lat=myGPS.gprmc_latitude();
lon = myGPS.gprmc_longitude();
float d=(round(myGPS.gprmc_distance_to(dest_latitude, dest_longitude, MTR)));
board.print(d);
if (d < 5){
digitalWrite(13, HIGH);
carbreak();
}
if (d > 5){
digitalWrite(13, LOW);
float mydir= 0;
for (int x = 0; x<20; x=x+1){
mydir=mydir + calc_bearing (lat, lon, dest_latitude, dest_longitude);
}
mydir=mydir/20;
if (mydir < 0) {
mydir += 360;
}
if (mydir > 180) {
mydir -= 360;
}
// display relative direction to destination
if (mydir < 0) {
motorForward();
delay(1000);
digitalWrite(5,HIGH);
digitalWrite(6,LOW);
steering('L');
delay(1000); // destination is to your left
steering('C');
delay(1000);
}
else {
// destination is to your right
motorForward();
delay(1000);
digitalWrite(5,LOW);
digitalWrite(6,HIGH);
steering('R');
delay(1000); // destination is to your left
steering('C');
delay(1000);
}
}
}
}
}
void motorStop() {
digitalWrite(leftMotor, LOW);
digitalWrite(rightMotor, LOW);
}
void motorLeft() {
digitalWrite(leftMotor, HIGH);
}
void motorRight() {
digitalWrite(rightMotor, HIGH);
}
//back wheels
void motorForward()
{
digitalWrite(motorA1,HIGH);
digitalWrite(motorA2,LOW);
}
void motorBackward()
{
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,HIGH);
}
void carbreak()
{
digitalWrite(motorA1,LOW);
digitalWrite(motorA2,LOW);
}
void steering(char Dir)
{
//if (Serial.available() == 0)
//return;
//Dir=toupper(Serial.read());
switch (Dir)
{
case 'L':
//Serial.println("New command: L");
if (isLeft())
{
}//Serial.println("Already Left");
else
{
//Serial.println("Heading Left");
motorLeft();
while (!isLeft()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Left");
}
break;
case 'C':
//Serial.println("New command: C");
if (isRightOfCenter())
{
//Serial.println("Heading Left toward Center");
motorLeft();
while (isRightOfCenter()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Centered");
}
else
if (isLeftOfCenter())
{
//Serial.println("Heading Right toward Center");
motorRight();
while (isLeftOfCenter()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Centered");
}
else
{
}
//Serial.println("Already Centered");
break;
case 'R':
//Serial.println("New command: R");
if (isRight())
{
}//Serial.println("Already Right");
else
{
//Serial.println("Heading Right");
motorRight();
while (!isRight()) /* JUST WAITING */;
motorStop();
//Serial.println("Is now Right");
}
break;
} // end of switch on direction
}
int calc_bearing(float flat1, float flon1, float flat2, float flon2)
{
float calc;
float bear_calc;
float x = 69.1 * (flat2 - flat1);
float y = 69.1 * (flon2 - flon1) * cos(flat1/57.3);
calc=atan2(y,x);
bear_calc= degrees(calc);
if(bear_calc<=1){
bear_calc=360+bear_calc;
}
return bear_calc;
}
here is the other code for next board that gives me error
#include <math.h>
#include <LiquidCrystal.h>
#include <NewSoftSerial.h>
NewSoftSerial newboard(7, 8);
LiquidCrystal myLCD = LiquidCrystal(12, 11, 5, 4, 3, 2);
// khali pin 6
void setup() {
newboard.begin(9600);
myLCD.begin(16,2);
pinMode(13, OUTPUT); // diagnostic led on Wiring-board
pinMode(6, OUTPUT);
myLCD.clear();
myLCD.home();
}
void loop() {
if (newboard.available()>0){
float dest=newboard.read(d);
myLCD.setCursor(0,0);
myLCD.print("D:");
myLCD.print(dest);
myLCD.print("m");
float mydir;
myLCD.setCursor(1,1);
if (mydir < 0) {
myLCD.print("TURN LEFT");
}
else {
myLCD.print("TURN RIGHT");
}
myLCD.print(abs(mydir), DEC);
myLCD.print(223, BYTE); // print °-character
myLCD.setCursor(8,0);
if (d < 5)
{
myLCD.clear();
myLCD.home();
myLCD.setCursor(8,0);
myLCD.print("TRGET OK");
}
}
ERROR
lcd_only.cpp: In function 'void loop()':
lcd_only:24: error: no matching function for call to 'NewSoftSerial::read(float&)'
C:\Users\ENZEE\Desktop\FYP_DATA\Project_Fina\arduino-0022\arduino-0022\libraries\NewSoftSerial/NewSoftSerial.h:86: note: candidates are: int NewSoftSerial::read()
lcd_only:52: error: expected `}' at end of input
this the improvement that i have done but still show me error
#include <math.h>
#include <LiquidCrystal.h>
#include <NewSoftSerial.h>
NewSoftSerial newboard(7, 8);
LiquidCrystal myLCD = LiquidCrystal(12, 11, 5, 4, 3, 2);
// khali pin 6
void setup() {
newboard.begin(9600);
myLCD.begin(16,2);
pinMode(13, OUTPUT); // diagnostic led on Wiring-board
pinMode(6, OUTPUT);
myLCD.clear();
myLCD.home();
}
void loop() {
if (newboard.available()>0){
float dest=newboard.read(float d);
myLCD.setCursor(0,0);
myLCD.print("D:");
myLCD.print(dest);
myLCD.print("m");
float mydir;
myLCD.setCursor(1,1);
if (mydir < 0) {
myLCD.print("TURN LEFT");
}
else {
myLCD.print("TURN RIGHT");
}
myLCD.print(abs(mydir), DEC);
myLCD.print(223, BYTE); // print °-character
myLCD.setCursor(8,0);
if (d < 5)
{
myLCD.clear();
myLCD.home();
myLCD.setCursor(8,0);
myLCD.print("TRGET OK");
}
}
}
The compiler is telling you that it cannot generate code to read a float
can you tell me what i have done wrong
float dest=newboard.read(float d);
candidates are: int NewSoftSerial::read()
i.e. NewSoftSerial.read() doesn't take a parameter, and you are passing it a float.
ok
i want my data that is coming from Board No.1 should send data to board no. 2 that is d or my DIR etc
So, you need to concentrate on transferring floating point numbers between processors.
Forget all else.
How?
Just put all the GPS stuff out of your mind and concentrate.
Look you just tell me how to receive that data through Serial port on another board please
You need to develop a protocol.
Put your floating point number between distinctive, non-numeric delimeters.