Gauge for Transmission J1939

Hi, first up, I'm very much a novice at C++ but I am learning lots.

I've been playing with getting a fairly simple gauge to work for my transmission TCU.
It's sending J1939 extended frames over CAN.

I have a Leonardo board with a MCP2515 connected.

I also have a similar board as another gauge for my Link ECU which works well enough.

With the TCU gauge, I'm trying to get it to show the Shifter Position, Current Gear, Transmission Temp and if Lock up is enabled. These are all transmitted on CAN.

I'm using Adafruit_GFX and SSD1306 libraries for the Display. I can get the void setup() display section to display but the void loop () just goes to a blank screen. I'm not sure what I've done wrong here, I copied the screen code from my work project.

Any advice is very welcome.

#include <mcp_can_cory.h>
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <Fonts/FreeSansBoldOblique9pt7b.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels


#define OLED_DC     9
#define OLED_CS     11
#define OLED_RESET  10
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT,
  &SPI, OLED_DC, OLED_RESET, OLED_CS);


//variables
  unsigned char rxLen = 0;
  unsigned char rxBuf[8];
  word ATTEMP;
  byte GEAR;
  byte GEARPOS;
  byte GEARDISPLAY;
  boolean LOCKUP;
  long unsigned int ID;
  const byte led = 23;
  unsigned long canId;
//CAN Setup
MCP_CAN CAN0(17); //set PIN for MCP2515

void setup() {
  Serial.begin(115200);
  delay(2000);
  Serial.println("115200");
  pinMode(led, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(7, INPUT);  //D7 for CAN /int
  digitalWrite(6, LOW);
// CAN Setup

  while (CAN_OK != CAN0.begin(MCP_STDEXT, CAN_500KBPS, MCP_16MHZ))                     // initiate CAN bus : baudrate = 1000k for Haltech
  {
    Serial.println("CAN BUS Shield init fail");               // :(
    Serial.println(" Init CAN BUS Shield again");             // Yes please
  }
    Serial.println("CAN BUS Shield init ok!"); 

  CAN0.init_Mask(0,1,0x1FFFFFFF);                // Init first mask
  CAN0.init_Filt(0,1,0x18FE4A03);               // Init first filter
  CAN0.init_Filt(1,1,0x0CF00203);                // Init second filter...
  
  CAN0.init_Mask(1,1,0x1FFFFFFF);                // Init second mask... 
  CAN0.init_Filt(2,1,0x18FEF803);                // Init third filter...
  CAN0.init_Filt(3,1,0x18F00503);                // Init fourth filter...
  CAN0.init_Filt(4,1,0x18F00503);                // Init fifth filter...
  CAN0.init_Filt(5,1,0x18F00503);                // Init sixth filter...
 // CAN0.init_Filt(1, 0, 0x0CF00203);                
 // CAN0.init_Filt(2, 0, 0x18FEF803);
 // CAN0.init_Filt(3, 0 ,0x18FEF803);
  CAN0.setMode(MCP_NORMAL);
  // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally
  if(!display.begin(SSD1306_SWITCHCAPVCC)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Don't proceed, loop forever
  }
delay(3000);

  // Show initial display buffer contents on the screen --
  // the library initializes this with an Adafruit splash screen.
  display.display();
  //delay(2000); // Pause for 2 seconds

  // Clear the buffer
  display.clearDisplay();
  // HGM Splash Screen
  display.setTextSize(3);
  display.setTextColor(WHITE);
  display.setCursor(20, 5);
  display.println("HGM");
  display.setCursor(20, 35);
  display.println("GAUGE");

  // Show the display buffer on the screen. You MUST call display() after
  // drawing commands to make them visible on screen!
  display.display();
  delay(1000);
  // display.display() is NOT necessary after every single drawing command,
  // unless that's what you want...rather, you can batch up a bunch of
  // drawing operations and then update the screen all at once by calling
  // display.display(). These examples demonstrate both approaches...
  display.clearDisplay();
}

void loop() {
//  hgmcan();
    if(!digitalRead(2))                    // If pin 2 is low, read receive buffer
    {
//  CAN0.readMsgBuf(&ID, &rxLen, rxBuf);
  //Testing Output
/*        Serial.print("ID: ");
      Serial.print(ID, HEX);
      Serial.print(" Data: ");
      for(int i = 0; i<rxLen; i++)           // Print each byte of the data
      {
        if(rxBuf[i] < 0x10)                // If data byte is less than 0x10, add a leading zero
        {
          Serial.print("0");
        }
        Serial.print(rxBuf[i], HEX);
        Serial.print(" ");
      }
      Serial.println();
  //End Testing Output
  */
//if(CAN_MSGAVAIL == CAN0.checkReceive()) {
//CAN0.readMsgBuf(&ID, &rxLen, rxBuf);
//  if(ID == 0x98FE4A03) {
//    GEARPOS = rxBuf[3];
//    Serial.println(GEARPOS);
//  }


//  Serial.print("ID: ");
//  Serial.print(ID, HEX);
//  Serial.println(" Data: ");

    if(CAN_MSGAVAIL == CAN0.checkReceive()) {          // Check to see whether data is read
      CAN0.readMsgBuf(&ID, &rxLen, rxBuf);
      if(ID == 0x98FE4A03) { // filter for J1939 EEC1 Electronic Engine Controls - 
        GEARPOS = rxBuf[3]; 
         if (GEARPOS == 0xfa){//P
                GEARDISPLAY = "P";
         }
         else if (GEARPOS == 0x7c){//R
                GEARDISPLAY = "R";
         }
         else if (GEARPOS == 0x7d){//N
                GEARDISPLAY = "N";
         }
         else if (GEARPOS == 0x81){//D
                GEARDISPLAY = "D"; 
         }
         else if (GEARPOS == 0x7f){//2
                GEARDISPLAY = "2";
         }
         else if (GEARPOS == 0x7e){//1
                GEARDISPLAY = "1";
         }
       // Serial.println(GEARDISPLAY);
        delay(50);
      }                            
/*      if(ID == 0x8CF00203) { //Lockup Address
        LOCKUP = word(bitRead(rxBuf[0], 5), bitRead(rxBuf[0], 4));
        Serial.println(LOCKUP);
        delay(50);
      } */
      if(ID == 0x98FEF803) { //Transmission Temp
        ATTEMP = word(rxBuf[5], rxBuf[4]);
        ATTEMP = ((ATTEMP / 32) - 273);
        Serial.println(ATTEMP);
        delay(50);
      }
      if(ID == 0x98F00503) { //Current Gear
        GEAR = ((rxBuf[3])-125);
        Serial.println(GEAR);
        delay(50);
      }
    }
    }

  display.clearDisplay();
  display.setTextSize(1);
  display.setCursor(10, 12);
  display.print("Gear");
  display.setTextSize(5);
  display.setCursor(3, 23);
  display.print("D"); //placeholder for shifter postion varible
  display.setCursor(33, 23);
  if (GEAR == 0) {
    display.print(" ");
  } else {
    display.print(GEAR);
  }
  display.setCursor(68, 1);
  display.print("Trans Temp");
  display.setCursor(80, 12);
  display.setTextSize(2);
  display.print(ATTEMP);
  display.setCursor(80, 35);
  display.setTextSize(1);
  display.print("Lockup");
  display.setCursor(80, 50);
  display.setTextSize(1);
  if (LOCKUP == "1") {
    display.setTextColor(BLACK, WHITE);
    display.print("Engaged");
    } else {
    display.setTextColor(BLACK);
    display.print("Disengaged");
    }
  display.display();
}

/*
void hgmcan() {

  CAN0.readMsgBuf(&ID, &rxLen, rxBuf);
  if(CAN_MSGAVAIL == CAN0.checkReceive()) {          // Check to see whether data is read                         
      if(ID == 0x98FE4A03) { // filter for J1939 EEC1 Electronic Engine Controls - 
        GEARPOS = rxBuf[3]; 
         if (GEARPOS == 0xfa){//P
                GEARDISPLAY = 0x09;
         }
         else if (GEARPOS == 0x7c){//R
                GEARDISPLAY = 0x0A;
         }
         else if (GEARPOS == 0x7d){//N
                GEARDISPLAY = 0x0B;
         }
         else if (GEARPOS == 0x81){//D
                GEARDISPLAY = 0x0C; 
         }
         else if (GEARPOS == 0x7f){//H
                GEARDISPLAY = 0x0D;
         }
         else if (GEARPOS == 0x7e){//L
                GEARDISPLAY = 0x0E;
         }
         Serial.println(GEARPOS);
      }                            
      if(ID == 0xCF00203) { //Lockup Address
        LOCKUP = word(bitRead(rxBuf[0], 5), bitRead(rxBuf[0], 4));
      }
      if(ID == 0x18FEF803) { //Transmission Temp
        ATTEMP = word(rxBuf[5], rxBuf[4]);
        ATTEMP = ((ATTEMP / 32) - 273);
      }
      if(ID == 0x418383107) { //Current Gear
        GEAR = ((rxBuf[3])-125);
      }
  }
}
*/
[HGM2LINK_3.0.ino|attachment](upload://1snCPQer435AKwgdn3Y3RvZcnRB.ino) (7.3 KB)

Can you swap the boards/screens to eliminate hardware issues?

Yeah, tested the code on the working one and does the same thing.

The ECU Gauge code runs fine on this one too.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.