Have done a bit of work on my project and all seems to be working as planned
hoping to be able to load on a pro mini but all is working on an UNO R3 plus
Just wondering if someone would be willing to look over my code and let me know how it looks
#include <SoftwareSerial.h>
#include "LedControl.h"
SoftwareSerial NMEA(8, 9); // RX, TX
int v = 0;
const int delaytime = 100;
int offset = 0; //side of 8 digit display 0 left, 4 right
boolean dp = true; //Decimal point for led
int anglePin = A0; // wiper on angle sensor connected to analog pin 0
int speedPin = A1; // speed sensor connected to analog pin 1
float WindAngle = 0.0;
float WindSpeed = 0.0;
String windir="";
float appangle= 0.0;
String InNMEA = "";
String Checksum ="";
int angleValue = 0.0;
int speedValue = 0.0;
int Speed=0.0;
int angle=100;
String Wind = "";
const String WindSpeedUnit = "N,"; // unit for display N = Nautical Miles
const long interval = 100; // interval at which to send NMEA Wind (milliseconds)
unsigned long previousMillis = 0; // will store last time NMEA Wind sent
const int AngleCal = 0; // Calibration of wind angle sensor *10
const int SpeedCal = 0; // Calibration of wind speed sensor *10
int led=1;
int ones=0;
int tens=0;
int hundreds=0;
int tenths=0;
/* Bring in analog values 0-5vdc from wind sensors and output nmea 0183
Wind direction and speed sentence NMEA 0183
$WIMWV
$ String Start
WI Weather Instrument
MWV Wind Speed and Angle
1 2 3 4 5
| | | | |
$WIMWV,x.x,a,x.x,a*hh
1) Wind Angle, 0 to 360 degrees
2) Reference, R = Relative, T = True
3) Wind Speed
4) Wind Speed Units, K/M/N
5) Status, A = Data Valid
6) Checksum
*/
LedControl lc=LedControl(12,11,10,2);
/*
Now we need a LedControl to work with.
***** These pin numbers will probably not work with your hardware *****
pin 12 is connected to the DataIn
pin 11 is connected to the CLK
pin 10 is connected to LOAD
We have only a single MAX72XX.
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(4800);
NMEA.begin(4800);
/*
The MAX72XX is in power-saving mode on startup,
we have to do a wakeup call
*/
lc.shutdown(0,false);
lc.shutdown(1,false);
/* Set the brightness to a medium values */
lc.setIntensity(0,6);
lc.setIntensity(1,6);
/* and clear the display */
lc.clearDisplay(0);
lc.clearDisplay(1);
}
// the loop routine runs over and over again forever:
void loop() {
if (Serial.available()) { // If anything comes in Serial (USB),
InNMEA = Serial.readStringUntil('\n'); // read nmea in and output string
NMEA.print(InNMEA); //Print Incoming NMEA put to NMEA port
} //if (Serial.available() close
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) { //Checks time past since last send of Wind NMEA and if > interval sends annother NMEA string
previousMillis = currentMillis; //sets time to new value
Speed = ReadSpeed();
angle = ReadAngle();
// build the nmea 0183 string for wind instrument and add checksum
Wind = "WIMWV,"; // WI = Weather instrument MWV wind data
WindAngle = (angle+AngleCal)/10.0; //Apply Angle Calibration Variable and /10 to get 1 dec place
WindSpeed = (Speed+SpeedCal)/10.0; //Apply Speed Calibration Variable and /10 to get 1 dec place
Wind=Wind+String(WindAngle, 1)+",R,"+String(WindSpeed, 1)+","+WindSpeedUnit+"A"; // Builds string data without line start, end, or Checksum to send to NMEA out port
Checksum = Checksumcalc();
Wind="$" + Wind + "*" + Checksum; // add line start and end with checksum
// test value
//WindAngle=190.0;
if (WindAngle > 185) {
windir="Port";
offset=6;
appangle=360-WindAngle;
if (WindAngle > 240) {
offset=7;
}
}
else if (WindAngle < 175) {
windir="Stbd";
appangle = WindAngle;
offset=4;
if (WindAngle < 120) {
offset=3;
}
}
else {
windir="DDW";
appangle = WindAngle;
offset=5;
// delay (500);
}
NMEA.println(Wind); // print string to NMEA Output port
} //if (currentMillis - previousMillis >= interval) close
/* Serial.print("Wind Speed\t");
Serial.print(String(WindSpeed, 1));
Serial.print("\tKnots\t");
Serial.print("Wind Angle\t");
Serial.print(String(appangle, 1));
Serial.print("\t");
Serial.println(windir);
*/
lc.clearDisplay(0);
led=0;
v = appangle*10;
printNumber(v);
led=1;
offset=5;
v = WindSpeed*10;
printNumber(v);
delay(delaytime);
} //loop close
int ReadSpeed(){
speedValue = analogRead(speedPin); // Reads Value from Analog pin 1 for wind speed
Speed = map(speedValue, 0, 1023, 0.0, 833.334); // remaps speed value to 0 - 833.334 Knots*10
/*if (Speed < 833) {
Speed = Speed + 1;
}
else {
Speed = 1;
}
*/
return Speed;
}
int ReadAngle(){
angleValue =analogRead(anglePin); // Reads Value from Analog pin 0 for wind angle
angle = map(angleValue, 0, 1023, 3500.0, 100.0); // remaps angle value to 100 - 3500 Degrees*10
/*
if (angle < 3500) {
angle = angle + 5;
}
else {
angle = 100;
}
*/
return angle;
}
String Checksumcalc(){
String data = Wind;
byte crc = 0;
for(int i=0;i<data.length();i++){ // creates Checksum for string
crc=crc^data[i];
Checksum=String(crc,HEX);
}//checksum calc close
return Checksum;
}
int printNumber(int v) {
if(v < -9999 || v > 9999)
return;
tenths=v%10;
v=v/10;
ones=v%10;
v=v/10;
tens=v%10;
v=v/10;
hundreds=v;
//Now print the number digit by digit
lc.setDigit(led,7-offset+3,(byte)hundreds,false);
lc.setDigit(led,7-offset+2,(byte)tens,false);
lc.setDigit(led,7-offset+1,(byte)ones,true);
lc.setDigit(led,7-offset+0,(byte)tenths,false);
}