precompiler switch between mega and mini-pro

Hi,

I’m trying to write a bit of code to let a sketch be used on either a Mega or a mini-pro without the need for me to comment-out bits of code.

This will work but requires manual editing…

// include the library code:
#include <LiquidCrystal.h>

const int rs=8,en=9,d4=10,d5=11,d6=12,d7=13;  // Pin definitions for LCD (mega)
//const int rs=4,en=5,d4=6,d5=7,d6=8,d7=9;  // Pin definitions for LCD (mini pro)

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

I think I should be able to allow for multiple processors using pre-compiler directives as follows…

// include the library code:
#include <LiquidCrystal.h>

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
const int rs=8,en=9,d4=10,d5=11,d6=12,d7=13;  // Pin definitions for LCD (mega)
#else
const int rs=4,en=5,d4=6,d5=7,d6=8,d7=9;  // Pin definitions for LCD (mini pro)
#endif

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

But this generates the error “‘Serial’ was not declared in this scope”

Finally I tried this…

// include the library code:
#include <LiquidCrystal.h>

int rs=0,en=0,d4=0,d5=0,d6=0,d7=0;  

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Settings for Arduino Mega......
//Serial.println("configuring for MEGA. ");
rs=8;
en=9;
d4=10;
d5=11;
d6=12;
d7=13;  // Pin definitions for LCD (mega)
#else
// Settings for mini-pro
//Serial.println("configuring for MIN PRO. ");
rs=4;
en=5;
d4=6;
d5=7;
d6=8;
d7=9;  // Pin definitions for LCD (mini pro)
#endif

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

But this generates " ‘rs’ does not name type"

Can’t help feeling I’m pretty close, but I could still do with some help please!

int rs=0,en=0,d4=0,d5=0,d6=0,d7=0;  

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Settings for Arduino Mega......
//Serial.println("configuring for MEGA. ");
rs=8;

Unless you're inside a function, you can't have assignment statements like you have inside your #if
The usual way of handling this would be something like:

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
// Settings for Arduino Mega......
  int rs=8,en=9,d4=10,d5=11,d6=12,d7=13;  
#else
  int rs=4,en=5,d4=6,d5=7,d6=8,d7=9; 
#endif

The usual way of handling this would be something like:

Thanks for the help, but that's very similar to my 2nd code snippet in my original post, and produces exactly the same error!

The second code snippet compiles (after I added a loop and setup) without a hitch here (and looks fine) so the error is not there. And that would be normal because Serial isn't even in the snippet!

And that's the problem with snippets... So only post compile-able code (or at least code you think should compile).

Btw, switch to a byte to save the pins, saves halve of the memory :wink:

Add #include <Arduino.h>

The IDE does this automatically, but versions up to 1.6.5 may put it the wrong place when there are #if… directives.

Example sketch after preprocessing:

#line 1 "sketch_jan06a.ino"
                            
                      
#include <LiquidCrystal.h>

#if defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__)
#include "Arduino.h"
void setup();
void loop();
#line 6
const int rs=8,en=9,d4=10,d5=11,d6=12,d7=13;                                   
#else
const int rs=4,en=5,d4=6,d5=7,d6=8,d7=9;                                       
#endif

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);  

void setup() {
  Serial.begin(115200);
}

void loop() {
}

This would compile if a Mega board is selected, but not otherwise.

Add
Code: [Select]
#include <Arduino.h>

Thank you!

I normally do add this, but on this occasion I based my sketch on a simple example I found on the net (that’ll teach me)

works now!

Thanks.