#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <Encoder.h>
#include <SoftwareSerial.h>
SoftwareSerial mySerial1(-1, 13,true); // RX, TX Sounder
const int encoderPin1 = 2;
const int encoderPin2 = 3;
const int buttonPin = 4;
float vPow = 4.7;
int sensorPin1 = A0;
int sensorPin2 = A1;
int sensorValue1;
Encoder myEncoder(encoderPin1, encoderPin2);
float DHeading =0;
int buttonState = 0;
int lastButtonState = 0;
long encoderValue = 0;
#define I2C_ADDR 0x27
#define LCD_COLUMNS 16
#define LCD_ROWS 2
#define SCROLL_SPEED 50
LiquidCrystal_I2C lcd(I2C_ADDR, LCD_COLUMNS, LCD_ROWS);
char fixedText[] = " NSSLGlobal";
char scrollingText[] = " VSAT Heading Simmulator V0.9 ";
void setup() {
Serial.begin(9600);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(9, OUTPUT); // SW
pinMode(8, OUTPUT); // SW
pinMode(6, OUTPUT); // SW
pinMode(7, OUTPUT); // SW
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(fixedText);
delay(50); // Display fixed text for 2 seconds
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.backlight();
lcd.setCursor(0, 0);
lcd.print(fixedText);
delay(500); // Display fixed text for 2 seconds
lcd.clear(); // Clear the display before entering the loop
int textLength = strlen(scrollingText);
for (int i = 0; i < textLength + LCD_COLUMNS; i++) {
// Create a circular buffer to store the scrolling text
char circularBuffer[LCD_COLUMNS + 1];
circularBuffer[LCD_COLUMNS] = '\0'; // Null-terminate the buffer
// Copy a portion of the scrolling text into the circular buffer
strncpy(circularBuffer, scrollingText + i, LCD_COLUMNS);
// Print the fixed text on the first row
lcd.setCursor(0, 0);
lcd.print(fixedText);
// Print the scrolling text on the second row
lcd.setCursor(0, 1);
lcd.print(circularBuffer);
// Delay to control the scrolling speed
delay(SCROLL_SPEED);
// Clear the display at the end of scrolling
if (i == textLength - 1) {
lcd.clear();
break; // Exit the loop after scrolling is complete
}
}
}
void loop(){
float v = analogRead(0);
float w = analogRead(1);
Serial.println(v);
Serial.println(w);
if (digitalRead(4) == LOW && w >= 900) {
lcd.setCursor(0, 0);
lcd.print("Output baudrate");
lcd.setCursor(0, 1);
lcd.print("set to 4800");
delay(1000);
lcd.clear();
}
if (digitalRead(4) == LOW && w < 900) {
lcd.setCursor(0, 0);
lcd.print("Output baudrate ");
lcd.setCursor(0, 1);
lcd.print("set to 38400");
delay(1000);
lcd.clear();
}
if (digitalRead(4) == LOW) {
delay(10);
}
////SW
if (v >= 900){
//Serial.println("$GPGLL,5441.4532,N,00546.9955,W,150114.00,A,D*7D");
//Serial.println("$GPGGA,150115.00,5441.4518,N,00546.9999,W,2,10,1.5,21,M,,M,,*41");
//Serial.println("$GPZDA,150114.00,12,10,2015,-02,00*4D");
//Serial.println("$GPVTG,242.3,T,245.9,M,10.4,N,19.3,K,D*25");
delay(10);
// mySerial1.println("$GPDTM,W84,,00.0000,N,00.0000,E,,W84*41");
digitalWrite(8, LOW); //sw
digitalWrite(9, HIGH);
lcd.setCursor(0, 0);
lcd.print("GPSpos1 scheemda");
}
/// SW
if (v<= 900){
Serial.println("$GPGLL,2441.1532,S,00546.9955,E,150114.00,A,D*7D");
Serial.println("$GPGGA,150115.00,2441.1518,S,00546.9955,E,2,10,1.5,21,M,,M,,*41");
Serial.println("$GPZDA,150114.00,12,10,2015,-02,00*4D");
Serial.println("$GPVTG,242.3,T,245.9,M,10.4,N,19.3,K,D*25");
digitalWrite(9, LOW); //sw
digitalWrite(8, HIGH);
lcd.setCursor(0, 0);
lcd.print("GPSpos2 REDhill ");
}
{
if (w >= 900){
mySerial1.begin(4800);
digitalWrite(6, LOW); //sw
digitalWrite(7, HIGH);
}
if (w<= 900){
mySerial1.begin(38400);
digitalWrite(7, LOW); //sw
digitalWrite(6, HIGH);
}
}
CourseEncoder();
nmeaGYRO();
}
void CourseEncoder() {
long encoderReading = myEncoder.read();
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
encoderValue = 0;
Serial.println("Encoder value reset.");
}
delay(50);
}
lastButtonState = buttonState;
double decimalValue = fmod(encoderReading / 20.0, 359.9);
if (decimalValue < 0) {
decimalValue += 360.0;
delay(50);
}
Serial.print("Compass Course: ");
Serial.print(decimalValue, 1);
Serial.println(" degrees");
DHeading = decimalValue;
encoderValue = encoderReading;
lcd.setCursor(0, 1);
lcd.print("HDT");
lcd.setCursor(4, 1);
lcd.print(decimalValue, 1);
lcd.setCursor(9, 1);
lcd.print("deg");
delay(200);
}
void nmeaGYRO() {
float Heading = DHeading;
// Round Heading to one decimal place
Heading = round(Heading * 10) / 10.0;
// Create a HDT NMEA sentence
char hdtSentence[100];
// "$HEHDT,240.9,T*20" ref
// $HEHDT,261.0,T*2A out
snprintf(hdtSentence, sizeof(hdtSentence), "$HEHDT,%03d.%d,T*",
(int)Heading, int((Heading - (int)Heading) * 10));
// Calculate the checksum
byte checksum4 = 0;
for (int i4 = 1; hdtSentence[i4] != '*'; i4++) {
checksum4 ^= hdtSentence[i4];
}
// Print the hdt NMEA sentence with the checksum
Serial.print(hdtSentence);
Serial.println(checksum4, HEX);
delay(10);
}