Porsche Coding, using x2 variables outside if statement

I'm trying to make my exhaust valve activate during certain conditions
I'm using the engine RPM and throttle position sensor to trigger the valve

I use 2 if statements to collect engine RPM and throttle position, both values can be seen inside the statement

The issue I'm experiencing is that when returning the value from each CAN ID outside the if statement the value is equal to zero

Any thoughts on how to resolve this?

Please see the code below

/*
 * demo: CAN-BUS Shield, receive all frames and print all fields id/type/data
 * to receive frame fastly, a poll in loop() is required.
 *
 * Copyright (C) 2020 Seeed Technology Co.,Ltd.
 */
#include <SPI.h>

#define CAN_2515
// #define CAN_2518FD

// Set SPI CS Pin according to your hardware

#if defined(SEEED_WIO_TERMINAL) && defined(CAN_2518FD)
// For Wio Terminal w/ MCP2518FD RPi Hat:
// Channel 0 SPI_CS Pin: BCM 8
// Channel 1 SPI_CS Pin: BCM 7
// Interupt Pin: BCM25
const int SPI_CS_PIN  = BCM8;
const int CAN_INT_PIN = BCM25;
#else

// For Arduino MCP2515 Hat:
// the cs pin of the version after v1.1 is default to D9
// v0.9b and v1.0 is default D10
const int SPI_CS_PIN = 9;
const int CAN_INT_PIN = 2;
#endif


#ifdef CAN_2518FD
#include "mcp2518fd_can.h"
mcp2518fd CAN(SPI_CS_PIN); // Set CS pin

// To TEST MCP2518FD CAN2.0 data transfer
#define MAX_DATA_SIZE 8
// To TEST MCP2518FD CANFD data transfer, uncomment below lines
// #undef  MAX_DATA_SIZE
// #define MAX_DATA_SIZE 64

#endif

#ifdef CAN_2515
#include "mcp2515_can.h"
mcp2515_can CAN(SPI_CS_PIN); // Set CS pin
#define MAX_DATA_SIZE 8
#endif

byte RPM;
byte TPS;
byte cdata[MAX_DATA_SIZE] = {0};

void setup() {
Serial.begin(9600);
    // Pin for relay module set as output
  pinMode(10, OUTPUT);
  
    SERIAL_PORT_MONITOR.begin(115200);
    while (!SERIAL_PORT_MONITOR) {}

    #if MAX_DATA_SIZE > 8
    /*
     * To compatible with MCP2515 API,
     * default mode is CAN_CLASSIC_MODE
     * Now set to CANFD mode.
     */
    CAN.setMode(CAN_NORMAL_MODE);
    #endif

    while (CAN_OK != CAN.begin(CAN_500KBPS)) {             // init can bus : baudrate = 500k
        SERIAL_PORT_MONITOR.println(F("CAN init fail, retry..."));
        delay(100);
    }
    SERIAL_PORT_MONITOR.println(F("CAN init ok!"));
}

uint32_t id;
uint8_t  type; // bit0: ext, bit1: rtr
uint8_t  len;


void loop(void) {


    // check if data coming
    if (CAN_MSGAVAIL != CAN.checkReceive()) {
        return;
    }

    char prbuf[32 + MAX_DATA_SIZE * 3];
    int i, n;

    unsigned long t = millis();
    // read data, len: data length, buf: data buf
    CAN.readMsgBuf(&len, cdata);

    id = CAN.getCanId();
    type = (CAN.isExtendedFrame() << 0) |
           (CAN.isRemoteRequest() << 1);
    /*
     * MCP2515(or this driver) could not handle properly
     * the data carried by remote frame
     */

   
    if (id == 578){
        
           byte RPM = cdata[3];
         SERIAL_PORT_MONITOR.println("RPM");
         SERIAL_PORT_MONITOR.println(RPM); // RPM 9 = 900 10 = 1000 30 = 3000
    }
    

    if (id == 582){
         
           byte TPS = cdata[3];
           SERIAL_PORT_MONITOR.println("TPS");
           SERIAL_PORT_MONITOR.println(TPS); // Throttle position 0 = 0
    }


//    if (RPM == 0 && TPS == 254){ // (RPM < 25 && TPS > 5)
//      digitalWrite(10, HIGH); }    // relay relay pin to HIGH
//      else {
//      digitalWrite(10, LOW); }   // else relay pin to LOW

    
        
    SERIAL_PORT_MONITOR.println("RPM after");
    SERIAL_PORT_MONITOR.println(RPM);
    SERIAL_PORT_MONITOR.println("TPS after");
    SERIAL_PORT_MONITOR.println(TPS);

    
}
// END FILE

You have put the RPM into a local variable. Get rid of the word byte here:

           byte RPM = cdata[3];

Same thing for TPS.

1 Like

As simple as that! Thank you very much