Go Down

Topic: Car on board computer. (Read 1 time) previous topic - next topic

Catalin_bn

Aug 24, 2016, 08:24 pm Last Edit: Aug 25, 2016, 12:56 pm by Catalin_bn
Hello guys, I'm trying to build myself a on board computer for my car. I use a arduino mega and a nextion display. For the start I want to display speed,max speed, average speed and distance.
I already have the code written but it doesn't work as it should.
For the example it displays speed correct, but max speed is not correct, it shows current speed and after I stop the car reads from eeprom incorrect max speed. Distance doesn't work at all.
Please guys help me with some tips. Here is the code:
Code: [Select]

#include "Nextion.h"
#include <EEPROMex.h>
#include <EEPROMVar.h>


volatile static double pulse_distance = 0.0000619502; //0.0002478007
volatile double traveled_distance;
volatile unsigned long counter, avg_speed_divider, dist_pulse_count, all_speed_values;
volatile unsigned int speed, avg_speed, max_speed;
NexText textNumber = NexText(0, 1, "textNumber");
NexText temp = NexText(0, 5, "temp");
NexText umid = NexText(0, 6, "umid");
char buffer[100] = {0};
NexTouch *nex_listen_list[] =
{
  &textNumber,
  &temp,
  &umid,
  NULL
};

void setup() {
  nexInit();
  Serial.begin(115200);
 // noInterrupts();
  TCCR1A = 0;
  TCCR1B |= ((1 << CS10) | (1 << CS11));
  TIMSK1 |= (1 << TOIE1);
  TCNT1 = 3036;

  pinMode(2, INPUT);//pin 5 pt semnal viteza

  attachInterrupt(digitalPinToInterrupt(2), distance_count, FALLING);
  //interrupts();
  loadData();
}
void loop()
{
  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(speed, buffer, 10);
  umid.setText(buffer);

  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(max_speed, buffer, 10);
  textNumber.setText(buffer);
 
  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(traveled_distance, buffer, 10);
  temp.setText(buffer);

  nexLoop(nex_listen_list);

}

ISR(TIMER1_OVF_vect) {
  counter++;
  if (counter > 3) {
    speed = pulse_distance * dist_pulse_count * 3600;
    if (speed > 3)
    { avg_speed_divider++;
      avgSpeed();
      maxSpeed();
    }
    dist_pulse_count = 0;

    counter = 0;
  }
  TCNT1 = 3036;
}

void maxSpeed()
{ if (speed > max_speed);
  max_speed = speed;
  saveData();
}
void avgSpeed()
{ all_speed_values += speed;
  avg_speed = all_speed_values / avg_speed_divider;
}
void distance_count()
{ dist_pulse_count++;
  traveled_distance += pulse_distance;
}

void saveData()
{ EEPROM.writeInt(0, max_speed);
  EEPROM.writeInt(1, traveled_distance);

}
void loadData()
{ max_speed = EEPROM.readInt(0);
  traveled_distance = EEPROM.readInt(1);

  if (isnan(EEPROM.readInt(0)))
    max_speed = 100;

  if (isnan(EEPROM.readInt(1)))
    traveled_distance = 100;
}


weedpharma

First thing to do is to add comments to your code so we can see what you are trying to do with the code.

The next is please put your code in its own window as seen in other posts. This can be done by placing     [code]  and [/code]   around the code or use the </> icon. This makes it easier for others to read.

Weedpharma

Delta_G

#2
Aug 25, 2016, 03:25 am Last Edit: Aug 25, 2016, 03:26 am by Delta_G
Code: [Select]

void saveData()
{ EEPROM.writeInt(0, max_speed);
  EEPROM.writeInt(1, traveled_distance);

}


max_speed is an int.  It takes two bytes.  So when you write it to address 0 and then you come back on the next line and write something else to address 1, you're overwriting half of it. 

traveled_distance is a double.  I'd think real hard before I used a function called writeInt to try to write it to EEPROM.  Maybe it works, but I wouldn't assume that.  I'd have to take a good look at the code before I tried it. 
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Catalin_bn

First of all sorry for posting like this, I'm a beginner with Arduino and Nextion too.

Function EEPROM.writeInt works good, but that means i should write distance on address 2 then? so it won't overwriting it?

But still, why does it display on Nextion same value on speed and max speed while I'm testing it on the car?
Code: [Select]

memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(speed, buffer, 10);
  umid.setText(buffer);

  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(max_speed, buffer, 10);
  textNumber.setText(buffer);
 
  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(traveled_distance, buffer, 10);
  temp.setText(buffer);



Is this part correct?

OldSteve

#4
Aug 25, 2016, 10:40 am Last Edit: Aug 25, 2016, 10:43 am by OldSteve
It's not too late to edit your opening post and add the code tags that were suggested.

This won't work with a double, (float):-
Code: [Select]
itoa(traveled_distance, buffer, 10);
You'll end up with it truncated to an int value.
Check out 'dtostrf()'.
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Catalin_bn

Can you help me with sending this values to nextion, I'm a beginner in those things.

OldSteve

#6
Aug 25, 2016, 01:24 pm Last Edit: Aug 25, 2016, 05:33 pm by OldSteve
Can you help me with sending this values to nextion
No

Quote
I'm a beginner in those things.
So am I, as far as Nextion displays and the related library are concerned. I've never used either.

You'll need to study the examples provided with the library, do some searching and further reading if necessary, then go from there.

Edit: And thanks for adding the code tags. :)
Please do not PM me for help. I am not a personal consultant.
And others will benefit as well if you post your question publicly on the forums.

Catalin_bn

Damn, this thing drives me crazy. No problem with the code tags, it was my fault I didn't use them from beginning. But please guys help me display the values presented above.

Delta_G

#8
Aug 25, 2016, 09:40 pm Last Edit: Aug 25, 2016, 09:41 pm by Delta_G
You've had two issues pointed out to you. That the EEPROM addresses were messed up and that itoa won't work for a floating point variable.  Have you fixed those things?  If you've made changes then post the updated code and the updated symptoms.

If you haven't fixed those then do that first. Then see what happens. Then post the new code if there's still problems.
|| | ||| | || | ||  ~Woodstock

Please do not PM with technical questions or comments.  Keep Arduino stuff out on the boards where it belongs.

Catalin_bn

#9
Aug 26, 2016, 09:38 pm Last Edit: Aug 26, 2016, 09:50 pm by Catalin_bn
Thank you for the answers, of course I did what you asked me for, same problem with max speed. Tried again to write EEproms just with traveled distance and still doesn't work. But it doesn't return 100 value.
Code: [Select]

#include "Nextion.h"
#include <EEPROMex.h>
#include <EEPROMVar.h>

volatile static double pulse_distance = 0.0000619502; //0.0002478007
volatile double traveled_distance;
volatile unsigned long counter, avg_speed_divider, dist_pulse_count, all_speed_values;
volatile unsigned int speed, avg_speed, max_speed;
NexText textNumber = NexText(1, 1, "textNumber");
NexText dist = NexText(1, 9, "dist");
NexText umid = NexText(1, 2, "umid");
NexText vit_medie = NexText(1, 8, "vit_medie");
NexPage page0 = NexPage(0, 0, "page0");
NexPage page1 = NexPage(0, 0, "page1");
char buffer [10] = {0};

void setup() {
  nexInit();
  Serial.begin(115200);
 noInterrupts();
  TCCR1A = 0;
  TCCR1B |= ((1 << CS10) | (1 << CS11));
  TIMSK1 |= (1 << TOIE1);
  TCNT1 = 3036;

  pinMode(2, INPUT);//pin 2 pt semnal viteza

  attachInterrupt(digitalPinToInterrupt(2), distance_count, FALLING);
 
  interrupts();
  loadData();
}
void loop()
{
  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(speed, buffer, 10);
  umid.setText(buffer);

  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(avg_speed, buffer, 10);
  vit_medie.setText(buffer);
 
  memset(buffer, 0, sizeof(buffer));  // clear buffer
  //itoa(traveled_distance, buffer, 10);
 dtostrf(traveled_distance,5, 2, buffer);
  dist.setText(buffer);
 
  memset(buffer, 0, sizeof(buffer));  // clear buffer
  itoa(max_speed, buffer, 5);
   textNumber.setText(buffer);

}

ISR(TIMER1_OVF_vect) {
  counter++;
  if (counter > 3) {
    speed = pulse_distance * dist_pulse_count * 3600;
    if (speed > 3)
    { avg_speed_divider++;
      avgSpeed();
      maxSpeed();
    }
    dist_pulse_count = 0;

    counter = 0;
  }
  TCNT1 = 3036;
}

void maxSpeed()
{ if (speed > max_speed);
  max_speed = speed;
  //saveData();
}
void avgSpeed()
{ all_speed_values += speed;
  avg_speed = all_speed_values / avg_speed_divider;
}
void distance_count()
{ dist_pulse_count++;
  traveled_distance += pulse_distance;
  saveData();
}

void saveData()
{ //EEPROM.writeInt(0, max_speed);
  EEPROM.writeInt(0, traveled_distance);

}
void loadData()
{ //max_speed = EEPROM.readInt(0);
  traveled_distance = EEPROM.readInt(0);

 // if (isnan(EEPROM.readInt(0)))
 //   max_speed = 100;

  if (isnan(EEPROM.readInt(0)))
    traveled_distance = 100;
}

Go Up