CS sur D10 LiquidCrystal lcd

#include <SPI.h> // Nécessaire pour la communication avec le MCP2515
#include <mcp2515.h>
#include <LiquidCrystal.h>

struct can_frame canMsg;
MCP2515 mcp2515(10); // CS sur D10
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int currentPID = 0x0C; // PID par défaut : RPM

// Fonction pour lire le bouton du Shield LCD
int readKey() {
int x = analogRead(0);
if (x < 50)  return 1; // RIGHT
if (x < 250) return 2; // UP
if (x < 450) return 3; // DOWN
if (x < 650) return 4; // LEFT
if (x < 850) return 5; // SELECT
return 0; // aucun
}

// Fonction pour envoyer une requête OBD2
void sendOBD2Request(byte pid) {
canMsg.can_id = 0x7DF; // ID de requête standard (Broadcast)
canMsg.can_dlc = 8;
canMsg.data\[0\] = 0x02; // Nombre d'octets de données utiles
canMsg.data\[1\] = 0x01; // Mode 01 (Données en temps réel)
canMsg.data\[2\] = pid;  // PID demandé
for (int i = 3; i < 8; i++) {
canMsg.data\[i\] = 0x00;
}
mcp2515.sendMessage(&canMsg);
}

void setup() {
lcd.begin(16, 2);
lcd.print("Init CAN...");

SPI.begin(); // Initialisation obligatoire du bus SPI

mcp2515.reset();
// Configuration à 500 Kbps avec un quartz de 16 MHz (fréquence standard OBD2)
mcp2515.setBitrate(CAN_500KBPS, MCP_16MHZ);
mcp2515.setNormalMode();

lcd.clear();
lcd.print("CAN Ready");
delay(1000);
}

void loop() {
// Lecture du bouton pour changer de PID
int key = readKey();
if (key == 2) currentPID = 0x0C; // UP → RPM
if (key == 3) currentPID = 0x0D; // DOWN → Vitesse
if (key == 4) currentPID = 0x05; // LEFT → Température eau

// Envoi de la requête OBD2
sendOBD2Request(currentPID);

// Attente active de la réponse (pendant max 100ms) pour éviter de rater le message
unsigned long startTimeout = millis();
bool messageReceived = false;

while ((millis() - startTimeout < 100) && !messageReceived) {
if (mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK) {
// 0x7E8 est la réponse du calculateur moteur principal (ECU)
if (canMsg.can_id == 0x7E8) {
byte modeResponse = canMsg.data\[1\];
byte pid = canMsg.data\[2\];

    // Vérifie que la réponse correspond au Mode 1 (0x01 + 0x40 = 0x41) et au bon PID
    if (modeResponse == 0x41 && pid == currentPID) {
      lcd.clear(); 
      messageReceived = true;

      if (pid == 0x0C) { // RPM 
        unsigned int rpm = ((canMsg.data[3] * 256) + canMsg.data[4]) / 4; 
        lcd.print("RPM:"); 
        lcd.setCursor(0, 1); 
        lcd.print(rpm); 
      } 
      else if (pid == 0x0D) { // Vitesse 
        int speed = canMsg.data[3]; 
        lcd.print("Speed:"); 
        lcd.setCursor(0, 1); 
        lcd.print(speed); 
        lcd.print(" km/h"); 
      } 
      else if (pid == 0x05) { // Température eau 
        int temp = canMsg.data[3] - 40; 
        lcd.print("Coolant:"); 
        lcd.setCursor(0, 1); 
        lcd.print(temp); 
        lcd.print(" C"); 
      } 
    }
  } 
} 


}

// Petit délai avant la prochaine requête pour ne pas surcharger le bus
delay(300);
}

Your topic does not indicate a problem with IDE 2.x and hence has been moved. If you would like the topic to be moved to the French category you can do so yourself or click the flag icon under your post and ask a moderator to move it.


  1. Do you have a problem? Please provide a detailed description of what you want to achieve or of the problem that you are experiencing.
  2. Which mcp2515 library are you using?
  3. Which Arduino are you using?

===
Votre sujet ne signale aucun problème avec l'IDE 2.x et a donc été déplacé. Si vous souhaitez le déplacer dans la catégorie française, vous pouvez le faire vous-même ou cliquer sur l'icône de signalement sous votre message et demander à un modérateur de le déplacer.


  1. Rencontrez-vous un problème ? Veuillez décrire en détail ce que vous souhaitez réaliser ou le problème que vous rencontrez.
  2. Quelle bibliothèque mcp2515 utilisez-vous ?
  3. Quelle carte Arduino utilisez-vous ?

I gazed into my crystal ball and read your mind to figure out that you were using this MCP2515 library. Note for future reference: please don't make people do that. It tends to make them cranky. :enraged_face:

#include <SPI.h>  // Nécessaire pour la communication avec le MCP2515
#include <mcp2515.h>
#include <LiquidCrystal.h>

struct can_frame canMsg;
MCP2515 mcp2515(10);  // CS sur D10
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);

int currentPID = 0x0C;  // PID par défaut : RPM

// Fonction pour lire le bouton du Shield LCD
int readKey() {
   int x = analogRead(0);
   if( x < 50 ) return 1;   // RIGHT
   if( x < 250 ) return 2;  // UP
   if( x < 450 ) return 3;  // DOWN
   if( x < 650 ) return 4;  // LEFT
   if( x < 850 ) return 5;  // SELECT
   return 0;               // aucun
}

// Fonction pour envoyer une requête OBD2
void sendOBD2Request(byte pid) {
   canMsg.can_id = 0x7DF;  // ID de requête standard (Broadcast)
   canMsg.can_dlc = 8;
   canMsg.data[0] = 0x02;  // Nombre d'octets de données utiles
   canMsg.data[1] = 0x01;  // Mode 01 (Données en temps réel)
   canMsg.data[2] = pid;   // PID demandé
   for( int i = 3; i < 8; i++ ) {
      canMsg.data[i] = 0x00;
   }
   mcp2515.sendMessage(&canMsg);
}

void setup() {
   lcd.begin(16, 2);
   lcd.print("Init CAN...");

   SPI.begin();  // Initialisation obligatoire du bus SPI

   mcp2515.reset();
   // Configuration à 500 Kbps avec un quartz de 16 MHz (fréquence standard OBD2)
   mcp2515.setBitrate(CAN_500KBPS, MCP_16MHZ);
   mcp2515.setNormalMode();

   lcd.clear();
   lcd.print("CAN Ready");
   delay(1000);
}

void loop() {
   // Lecture du bouton pour changer de PID
   int key = readKey();
   if( key == 2 ) currentPID = 0x0C;  // UP → RPM
   if( key == 3 ) currentPID = 0x0D;  // DOWN → Vitesse
   if( key == 4 ) currentPID = 0x05;  // LEFT → Température eau

   // Envoi de la requête OBD2
   sendOBD2Request(currentPID);

   // Attente active de la réponse (pendant max 100ms) pour éviter de rater le message
   unsigned long startTimeout = millis();
   bool messageReceived = false;

   while( (millis() - startTimeout < 100) && !messageReceived ) {
      if( mcp2515.readMessage(&canMsg) == MCP2515::ERROR_OK ) {
         // 0x7E8 est la réponse du calculateur moteur principal (ECU)
         if( canMsg.can_id == 0x7E8 ) {
            byte modeResponse = canMsg.data[1];
            byte pid = canMsg.data[2];
            // Vérifie que la réponse correspond au Mode 1 (0x01 + 0x40 = 0x41) et au bon PID
            if( modeResponse == 0x41 && pid == currentPID ) {
               lcd.clear();
               messageReceived = true;

               if( pid == 0x0C ) {  // RPM
                  unsigned int rpm = ((canMsg.data[3] * 256) + canMsg.data[4]) / 4;
                  lcd.print("RPM:");
                  lcd.setCursor(0, 1);
                  lcd.print(rpm);
               } else if( pid == 0x0D ) {  // Vitesse
                  int speed = canMsg.data[3];
                  lcd.print("Speed:");
                  lcd.setCursor(0, 1);
                  lcd.print(speed);
                  lcd.print(" km/h");
               } else if( pid == 0x05 ) {  // Température eau
                  int temp = canMsg.data[3] - 40;
                  lcd.print("Coolant:");
                  lcd.setCursor(0, 1);
                  lcd.print(temp);
                  lcd.print(" C");
               }
            }
         }
      }
   }

   // Petit délai avant la prochaine requête pour ne pas surcharger le bus
   delay(300);
}
arduino-cli compile -b arduino:avr:uno --warnings all --build-property "build.extra_flags=-Wno-strict-aliasing" --output-dir ~/tmp --no-color (in directory: /home/me/Documents/sketchbook/Uno_R3/test)
Sketch uses 7552 bytes (23%) of program storage space. Maximum is 32256 bytes.
Global variables use 168 bytes (8%) of dynamic memory, leaving 1880 bytes for local variables. Maximum is 2048 bytes.
Compilation finished successfully.

And? So? Therefore? What's the problem?

I edited your post to fix the code tags.

In the future, please post code between code tags, if you make a mistake you can fix it using the pencil icon in the toolbar just below your post and add the code tags (it's hard to read as is):

  • select the part of the text that corresponds to the code
  • press the <code/> icon in the toolbar to indicate that it's code

(Also make sure to indent the code properly in your IDE before copying it here. This is done by pressing ctrl+T on PC or cmd+T on Mac)

Also if you want to use French, post in the French speaking forum.

(edited - changed to English)

@J-M-L

I'm aware that the topic title is in French but I suggest that we keep the communication in English as @fredtuner12 originally posted in the English section of the forum.

Oops , thought you had moved it - changing my text then!

I did move it; but not to the French section :wink: