Second thought on an arduino nano + Nextion display dashboard

Hey everyone,

I've been working for some days now on a project for my father's boat. It consists on a Nextion display(NX4024K032_011) and an Arduino nano rv3, used for display some basic things and control 2 led strips in cabin.

The dashboard displays:

-Engine RPM's (hall effect sensor) and Propeller RPM's (Engine RPMs/2)
-Battery voltage
-Sea and engine temperatures
-GPS coordinates, number of satellites, course, speed, time and date
-Controls via several Nextion buttons, 2 rgb led strips

Everything works if you use one sensor at a time.

I go to the battery page on the nextion, the nextion sends the string to the arduino asking for a particular sensor readings and the arduino starts to send data. Until here everthing looks fine.

BUT, when I go back to the index in the nextion display to check another page and start recieving data from another sensor, it just keep throwing the data from the sensor I first visited and not listening to the nextion anymore. If I want to check any other sensor page I have to reset the arduino and start again.

I believe it has something to do with the serial communication, because if I put a timer on the first sensor I've asked readings of, it stops sending data after a while and the arduino listens to the nextion again.

It's my first arduino project ever and I'm sure that there is a better way to code it

I don't know if I explain myself well enough.

Everything it's written by me but the RPM code that I "copy-pasted" it from the internet
I would love a second thought on whats happening and a possible way to achieve it:
I split the code in to posts, exceeds 9000 characters.

Part 1 / 2:

/*

#include <FastLED.h>
#include <SoftwareSerial.h>
#include <DallasTemperature.h>
#include <OneWire.h>
#include <TinyGPS++.h>
#include <TimeLib.h>
//////////////
//GPS
//////////////
TinyGPSPlus gps;
SoftwareSerial mySerial(4, 3) ;
static const uint32_t GPSBaud = 9600;
double Lat;
double Long;
int dia, mes, any;
int hora, minut, segon;
int num_sat;
float gps_speed;
int nussos;
int Lat_nextion;
int Long_nextion;
String heading;
#define time_offset   7200
/////////////
//TACÒMETRE
/////////////
int encoder = 2;
int rps;
int rpm;
int rpm_eix;
volatile byte pulses;
unsigned long timeold;
float s;
/////////////
//TERMÒMETRES
/////////////
#define ONE_WIRE_BUS 12
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
int deviceCount = 0;
float temp1;
float temp2;
uint8_t sensor1[8] = { 0x28, 0x36, 0x39, 0x94, 0x97, 0x08, 0x03, 0x1F };
uint8_t sensor2[8] = { 0x28, 0xE9, 0x55, 0x94, 0x97, 0x04, 0x03, 0x82 };
/////////////
//LLUMS
////////////
#define ledPin 6
#define ledPin1 9
#define COLOR_ORDER GRB
#define LED_TYPE WS2812B
#define NUM_LEDS 8
uint8_t max_bright = 255;
int manco = 192;
int mitju = 128;
int casi_res = 68;
struct CRGB leds[NUM_LEDS];


void counter()
{
  pulses++; 
  s = s + 20.42;
}

void setup() {
  Serial.begin(9600);
  mySerial.begin(GPSBaud);
  setTime(hora, minut, segon, dia, mes, any);
  adjustTime(time_offset);
  pinMode(A1, INPUT);
  pinMode(encoder, INPUT);
  attachInterrupt(0, counter, RISING);
  pulses = 0;
  rps = 0;
  rpm = 0;
  rpm_eix = 0;
  timeold = 0;
  s = 0;
  sensors.begin();
  deviceCount = sensors.getDeviceCount();
  delay(1000);
  FastLED.addLeds<LED_TYPE, ledPin, COLOR_ORDER>(leds, NUM_LEDS);
  FastLED.addLeds<LED_TYPE, ledPin1, COLOR_ORDER>(leds, NUM_LEDS);
}
void loop() {
  if (Serial.available()) {
    String data_from_display = "";
    delay(30);
    while (Serial.available()) {
      data_from_display += char(Serial.read());
    }
    Serial.println(data_from_display);
    receiveData(data_from_display);
    sendData(data_from_display);
  }
}

void receiveData (String data_from_display) {

  if (data_from_display == "1") {
    
  }
  if (data_from_display == "2") {
    for (int i = 1; i < 500; i++) {
      tacometre();
    }
  }
  if (data_from_display == "4") {
    for (int i = 1; i < 30; i++) {
      bateria();
    }
  }
  if (data_from_display == "5") {
    for (int i = 1; i < 10; i++) {
      Get_GPS();
      delay(1000);
      pag_gps();
    }
  }
  if (data_from_display == "6") {
    for (int i = 1; i < 15; i++){
    Get_GPS();
    delay(1000);
    pag_gps();
    }
  }
  if (data_from_display == "7") {
    for (int i = 1; i < 15; i++) {
    Get_GPS();
    delay(1000);
    pag_gps();
    }
  }
  if (data_from_display == "3") {
    for (int i = 1; i < 7; i++) {
      termometres();
    }
  }
  if (data_from_display == "9") {
    llums();
  }
  
}

void bateria() {
  float voltimetre_val = analogRead(A1);
  int val_barra = (voltimetre_val / 9);
  int val_voltimetre_correc = (voltimetre_val / 42) * 100;
  delay(200);

  Serial.print("j0.val=" + String(val_barra));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  if (val_barra < 51) {
    Serial.print("j0.pco=63488");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);

  } else {
    Serial.print("j0.pco=2047");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }

  Serial.print("x6.val=" + String(val_voltimetre_correc));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  if (val_voltimetre_correc < 1100) {
    Serial.print("x6.pco=63488");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  } else {
    Serial.print("x6.pco=2047");
    Serial.write(0xff);
    Serial.write(0xff);
    Serial.write(0xff);
  }
}
void tacometre() {
  if (pulses >= 1) {
    detachInterrupt(0);
    rpm = 60000.0 / (millis() - timeold) * pulses;
    rps = rpm / 60.0;
    rpm_eix = rpm / 2.0;
    timeold = millis();
    pulses = 0;
    attachInterrupt(0, counter, RISING);
  }

  Serial.print("x5.val=" + String(rpm));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  Serial.print("x4.val=" + String(rpm_eix));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);


}
void pag_gps() {

  
  nussos = (gps_speed * 100);
  Lat_nextion = (Lat * 100);
  Long_nextion = (Long * 100);

  Serial.print("x8.val=" + String(Lat_nextion));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("x9.val=" + String(Long_nextion));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("x7.val=" + String(nussos));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n0.val=" + String(hora));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n1.val=" + String(minut));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n2.val=" + String(segon));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n3.val=" + String(dia));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n4.val=" + String(mes));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n5.val=" + String(any));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n7.val=" + String(heading));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.print("n6.val=" + String(num_sat));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}
static void smartDelay(unsigned long ms)
{
  unsigned long start = millis();
  do
  {
    while (mySerial.available())
      gps.encode(mySerial.read());
  } while (millis() - start < ms);
}

static void Get_GPS()
{
  num_sat = gps.satellites.value();

  if (gps.location.isValid() == 1)
  {
    Lat = gps.location.lat();
    Long = gps.location.lng();
    gps_speed = gps.speed.knots();
    heading = gps.course.value();
    //heading = gps.cardinal(gps.course.value());

  }

  if (gps.date.isValid())
  {
    dia = gps.date.day();
    mes = gps.date.month();
    any = gps.date.year();
  }

  if (gps.time.isValid())
  {

    hora = gps.time.hour();
    minut = gps.time.minute();
    segon = gps.time.second();
  }



  smartDelay(1000);
}


void termometres() {
  sensors.requestTemperatures();
  temp1 = sensors.getTempCByIndex(0);
  temp2 = sensors.getTempCByIndex(1);
  int temp11 = temp1 * 100;
  int temp22 = temp2 * 100;
  delay(1000);

  Serial.print("x0.val=" + String(temp11));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);

  delay(1000);

  Serial.print("x1.val=" + String(temp22));
  Serial.write(0xff);
  Serial.write(0xff);
  Serial.write(0xff);
}

Part 2 / 2

void llums () {
  if (Serial.available()) {
    String data_from_display = "";
    delay(30);
    while (Serial.available()) {
      data_from_display += char(Serial.read());
    }
    Serial.println(data_from_display);
    sendData(data_from_display);
  }
}


void sendData(String data_from_display) {

  if (data_from_display == "100BLANC") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 255, 255);
      delay(50);
      FastLED.setBrightness(max_bright);
      FastLED.show();
    }
  } if (data_from_display == "75BLANC") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 255, 255);
      delay(50);
      FastLED.setBrightness(manco);
      FastLED.show();
    }
  } if (data_from_display == "50BLANC") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 255, 255);
      delay(50);
      FastLED.setBrightness(mitju);
      FastLED.show();
    }
  }
  if (data_from_display == "25BLANC") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 255, 255);
      delay(50);
      FastLED.setBrightness(casi_res);
      FastLED.show();
    }
  }
  if (data_from_display == "100BLAU") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 0, 0, 255);
      delay(50);
      FastLED.setBrightness(max_bright);
      FastLED.show();
    }
  }
  if (data_from_display == "75BLAU") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 0, 0, 255);
      delay(50);
      FastLED.setBrightness(manco);
      FastLED.show();
    }
  }
  if (data_from_display == "50BLAU") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 0, 0, 255);
      delay(50);
      FastLED.setBrightness(mitju);
      FastLED.show();
    }
  }
  if (data_from_display == "25BLAU") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 0, 0, 255);
      delay(50);
      FastLED.setBrightness(casi_res);
      FastLED.show();
    }
  }
  if (data_from_display == "100VERMELL") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 0, 0);
      delay(50);
      FastLED.setBrightness(max_bright);
      FastLED.show();
    }
  }
  if (data_from_display == "75VERMELL") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 0, 0);
      delay(50);
      FastLED.setBrightness(manco);
      FastLED.show();
    }
  }
  if (data_from_display == "50VERMELL") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 0, 0);
      delay(50);
      FastLED.setBrightness(mitju);
      FastLED.show();
    }
  }
  if (data_from_display == "25VERMELL") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 255, 0, 0);
      delay(50);
      FastLED.setBrightness(casi_res);
      FastLED.show();
    }
  }
  if (data_from_display == "OFF") {
    for (int i = 0; i < NUM_LEDS; i++) {
      leds[i].setRGB( 0, 0, 0);
      delay(50);
      FastLED.show();
    }
  }
}
1 Like