Switch....Case issue

Hello arduino community,

First of all please bare with me as I'm not a software developer.
I'm working on a project where a have a few boards communicating over CANBUS with each other.
On the latest boards i added some extra functionality and the pinout does not match the old boards.
What i would like to do is to create a single library for all the boards and select de right one when i program them.
I have created three files for the library: P24LSx.h, P24LSx.cpp, hardware.h
In "hardware.h" i defined the pinout for new and old boards.

The issue:
I have two boards P24LS2WS and P24LS2WH.
No mater which one i choose it always defines the last case on the switch case control structure.

Here is my code:

hardware.h

#if defined P24LS2WS

  #define HW_ERR_LED					A1
  #define HW_POWER_LED					A0
  #define HW_BUTTON_1_LED				A5
  #define HW_BUTTON_1					6

#endif

#if defined P24LS2WH

  #define HW_ERR_LED					2
  #define HW_POWER_LED					3
  #define HW_BUTTON_1_LED				5
  #define HW_BUTTON_1					4

#endif

P24LSx.h

#ifndef P24LSx_h
#define P24LSx_h

#include "Arduino.h"

enum BOARD_INFO { __P24LS2WS__, __P24LS2WH__ };


class P24LSx {
  
  public:

	  P24LSx(BOARD_INFO board1);
	  
	  void begin();

  private:
  
    
};

#endif

P24LSx.cpp

#include "Arduino.h"
#include "P24LSx.h"

P24LSx::P24LSx(const BOARD_INFO board1) {

  switch (board1)
  {
	case __P24LS2WS__:
	  #define P24LS2WS
	break;
	
	case __P24LS2WH__:
	  #define P24LS2WH
	break;
  }	  
}

#include "hardware.h"

void P24LSx::begin(){
	
  Serial.println(HW_ERR_LED, HEX);
 
}

main code

  #include <P24LSx.h>

  P24LSx lswc (__P24LS2WS__);


  void setup() {

    Serial.begin(9600);

    lswc.begin();
  }

I can not figure out what is the problem.
Any advice is much apreciated.

Best Regards,
Fabian

this doesn't do anything. these #defines are processed at compile not run time

either P24LS2WS or P24LSwWH need to be defined in hardware.h

hardware.h could include a .h where one of these are defined

they could be defined on the command line, but i'm not sure how to do that with the IDE

Try this

#define P24LS2WS     // to use that configuration or
// #undef P24LS2WS     // to use the other one

#ifdef P24LS2WS
  #define HW_ERR_LED					A1
  #define HW_POWER_LED					A0
  #define HW_BUTTON_1_LED				A5
  #define HW_BUTTON_1					6
#else
  #define HW_ERR_LED					2
  #define HW_POWER_LED					3
  #define HW_BUTTON_1_LED				5
  #define HW_BUTTON_1					4
#endif

Remove all other references to to P24LS2WS from your code. Haven't tested this, but it should work.

2 Likes

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