CANBUS RPM and SPEED array help

Hi im needing help with a sketch that ive modified that i cant get to work correctly.
What im trying to achieve is using a UNO, CANBUS shield and a LCD shield plugged into my car then pull the speed and RPM from the CANBUS line and display them onto LCD.
I can get it to work but not correctly the information displayed is randomly mixed up and irractic but the figures that do come up are correct but in the wrong place.
I think that its somthing to do with buffer array but im not sure??

#include <LiquidCrystal.h>
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8,9,4,5,6,7);

//Declare CAN variables for communication
int EngineRPM;
int VehicleSpeed;
char buffer[64];  //Data will be temporarily stored to this buffer before being written to the file

//********************************Setup Loop*********************************//
void setup() {
  //Initialize Serial communication for debugging
  Serial.begin(9600);
  Serial.println("ECU Demo");

  // set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.clear();
       
  //Initialize CAN Controller 
  if(Canbus.init(CANSPEED_500))  /* Initialize MCP2515 CAN controller at the specified speed */
  {
    lcd.clear();
    lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
    lcd.print("CAN Init ok");
    Serial.println("CAN Init Ok");
    delay(1500);
  } 
  else
  {
    lcd.clear();
    lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
    lcd.print("Can't init CAN");
    Serial.println("Can't init CAN");
    return;
  } 

    delay(1000); 

}

//********************************Main Loop*********************************//
void loop(){
        
      Canbus.ecu_req(ENGINE_RPM,buffer); //Request engine RPM
	  EngineRPM = buffer;
   Serial.print("RPM: "); //Uncomment for Serial debugging
      Serial.println(buffer);
      lcd.clear();
    lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
    lcd.print("RPM  ");
    lcd.print(buffer);
   Canbus.ecu_req(VEHICLE_SPEED,buffer); //Request engine RPM
   VehicleSpeed = buffer;
	  Serial.print("SPEED: "); //Uncomment for Serial debugging
      Serial.println(buffer);
        lcd.setCursor(0,1); // Sets the cursor to col 0 and row 0
    lcd.print("SPEED  ");
    lcd.print(buffer);
      delay(500);
	   //}
}

Ant help would be appreciated thanks.

It looks like to took the canbus example and then changed it a bit.
This line:

  Canbus.ecu_req(ENGINE_RPM, buffer); //Request engine RPM
  EngineRPM = buffer;

should be

  EngineRPM = Canbus.ecu_req(ENGINE_RPM, buffer); //Request engine RPM

just like the example code. Same for SPEED.

So I need to change the sketch from this

Canbus.ecu_req(ENGINE_RPM,buffer); //Request engine RPM
	 EngineRPM = buffer;
   Serial.print("RPM: "); //Uncomment for Serial debugging
      Serial.println(buffer);
      lcd.clear();
    lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
    lcd.print("RPM  ");
    lcd.print(buffer);

to this

EngineRPM = Canbus.ecu_req(ENGINE_RPM,buffer); //Request engine RPM
	Serial.print("RPM: "); //Uncomment for Serial debugging
      Serial.println(buffer);
      lcd.clear();
    lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
    lcd.print("RPM  ");
    lcd.print(buffer);

or this with Serial.println and the lcd.print changed from "buffer" to "EngineRPM"

EngineRPM = Canbus.ecu_req(ENGINE_RPM,buffer); //Request engine RPM
	 Serial.print("RPM: "); //Uncomment for Serial debugging
      Serial.println(EngineRPM);
      lcd.clear();
    lcd.setCursor(0,0); // Sets the cursor to col 0 and row 0
    lcd.print("RPM  ");
    lcd.print(EngineRPM);

Thanks

Ive tried the suggested mod to the sketch and it has made no difference it flashes between RPM and SPEED figures on both lines of the LCD is it somthing to do with “buffer”???

// include the library code:
#include <LiquidCrystal.h>
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>

// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

//Declare CAN variables for communication
int EngineRPM;
int VehicleSpeed;
char buffer[64];  //Data will be temporarily stored to this buffer before being written to the file

//********************************Setup Loop*********************************//
void setup() {
  //Initialize Serial communication for debugging
  Serial.begin(9600);
  Serial.println("ECU Demo");

  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  lcd.clear();

  //Initialize CAN Controller
  if (Canbus.init(CANSPEED_500)) /* Initialize MCP2515 CAN controller at the specified speed */
  {
    lcd.clear();
    lcd.setCursor(0, 0); // Sets the cursor to col 0 and row 0
    lcd.print("CAN Init ok");
    Serial.println("CAN Init Ok");
    delay(1500);
  }
  else
  {
    lcd.clear();
    lcd.setCursor(0, 0); // Sets the cursor to col 0 and row 0
    lcd.print("Can't init CAN");
    Serial.println("Can't init CAN");
    return;
  }

  delay(1000);

}

//********************************Main Loop*********************************//
void loop() {

  EngineRPM = Canbus.ecu_req(ENGINE_RPM, buffer); //Request engine RPM
  Serial.print("RPM: "); //Uncomment for Serial debugging
  Serial.println(buffer);
  lcd.clear();
  lcd.setCursor(0, 0); // Sets the cursor to col 0 and row 0
  lcd.print("RPM  ");
  lcd.print(buffer);

  VehicleSpeed = Canbus.ecu_req(VEHICLE_SPEED, buffer); //Request vehicle speed
  Serial.print("SPEED: "); //Uncomment for Serial debugging
  Serial.println(buffer);
  lcd.setCursor(0, 1); // Sets the cursor to col 0 and row 0
  lcd.print("SPEED  ");
  lcd.print(buffer);
  delay(100);

}

Thanks.

It is flashing because you are clearing the display every time through loop()

Why does it display the RPM reading on both lines???

Focusmad:
Why does it display the RPM reading on both lines???

You are displaying 'buffer' on both lines. I suspect you should be displaying EngineRPM and VehicleSpeed.

I’ve tried changing it from “buffer” to EngineRPM and Speed but all that does is create alarms on the car kills the RPM gauge and all the lights come up on the dash and the car revs up by itself. So maybe this isn’t the way to go??

Did you ever mention what CAN-BUS Shield you re using? Seeed 1.2? Seeed 2.0? Where did you get the library?

I’m using the Elecfreaks canbus shield and I’m using the spark fun canbus library and the linksprite LCD shield I got the original sketch from this link Arduino_CAN-Bus_ECU_reader/ecu_reader_logger_v5.ino at master · skpang/Arduino_CAN-Bus_ECU_reader · GitHub

and gutted it to try and make it work for my application. If I just have the sketch to only read the RPM or the SPEED it works fine just have problems when trying to do both at same time.

Focusmad:
I’ve tried changing it from “buffer” to EngineRPM and Speed but all that does is create alarms on the car kills the RPM gauge and all the lights come up on the dash and the car revs up by itself. So maybe this isn’t the way to go??

Sending data to the lcd display doesn't really affect your car. Try posting actual code since I am very dubious of our claims.

The original code does this:

char buffer[512]; //Data will be temporarily stored to this buffer before being written to the file

void loop()
{
  if (Canbus.ecu_req(ENGINE_RPM, buffer) == 1)        /* Request for engine RPM */
  {
    sLCD.write(COMMAND);                   /* Move LCD cursor to line 0 */
    sLCD.write(LINE0);
    sLCD.print(buffer);                         /* Display data on LCD */
  }

And you are doing:

char buffer[64];  //Data will be temporarily stored to this buffer before being written to the file

  EngineRPM = Canbus.ecu_req(ENGINE_RPM, buffer); //Request engine RPM
  Serial.print("RPM: "); //Uncomment for Serial debugging
  Serial.println(buffer);

Looks like the .ecu_req() function returns a 1 to indicate if the read was successful. You should check for that 1 before using the contents of 'buffer'.

Are you sure your buffer is large enough? You dropped it from 512 to 64.

Are your sure you are getting results? Without checking the return value you can't tell if the read was successful or not.

Are you sure your LCD is not using any pins used by the CAN-BUS Shield?

I’ve tried the buffer size both at 512 and 64 it makes no difference. I might try removing all the lcd parts in the sketch and just print on the serial connection on laptop, but I have checked all the pins on both the lcd and canbus shields and there is no conflicts but there is no harm in trying it without lcd because my end game is to send data to a nextion display.

Can someone tell me if I get this working properly will I be able to map the RPM value because I want to use the RPM figure to be displayed numerically and visually on a progress bar on a Nextion display if this wont be possible I will have to rethink my end project.