Go Down

Topic: Including Other Files (Read 1 time) previous topic - next topic

aaaelite21

I am making a game using the Adriano code and an LCD. I have everything in one file and it is fairly sloppy to deal with and I was wondering if there was a way that i could include a C++ file (or any file for that matter) that has the the classes I made along with other information that the setup and loop could use when they execute. any help would be appreciated. Thank you very much!!!

~aaaelite21

Nick Gammon

Click on the "new tab" button in the IDE (top RH corner). Give it a name, eg. foo.cpp, or foo.h.

Put stuff in the new file(s). They become part of your project.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

aaaelite21

Now when I try and call the class in the original file it says that it is not defined in this scope. Do have have to label this the main file somehow?

WizenedEE

You'll need to make a header file

aaaelite21

Still does not work. I named  my new file "ghost.h" and then compiled it and it gave the error. Then I typed #include <ghost.h> in the top of my main file and still nothing. My Sketch isn't in the main project area the folder is on my desktop but both files r in the same folder.

johncc

#include "ghost.h"

i.e.
#include <HeaderInSystemDirectory.h>
#include "HeaderInUserDirectory.h"

Cheers,
John

aaaelite21

#6
Jan 04, 2013, 01:56 am Last Edit: Jan 04, 2013, 01:59 am by aaaelite21 Reason: 1
Thank you very much John! It worked like a charm. Now one last thing. how do i get the new file to include variables from the main file??? At this point it is saying that they are undefined. I tried including the main .ino file but that did not work. Should I just make all of that data separate header files or is there a simpler way? Also if one file alters a variable in a header file will the data it contains be changed for all files?

Nick Gammon

Better look up the "extern" qualifier.
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

Nick Gammon

http://en.wikipedia.org/wiki/External_variable
Please post technical questions on the forum, not by personal message. Thanks!

More info:
http://www.gammon.com.au/electronics

aaaelite21

It will not allow it and says "storage class specified for 'class name'" it recognizes the class and its constructor but not the functions within the class.

PaulS

Quote
It will not allow it and says "storage class specified for 'class name'" it recognizes the class and its constructor but not the functions within the class.

Three pronouns with no referents in one sentence, with no code, has to be some kind of record.

Don't post again without posting ALL of your code.

aaaelite21

Main .ino file. this is not complete but there are no errors other than this problem

Code: [Select]

const int TxPin = 2;
const int LED = 13;
const int rightBUTTON = 7;
const int leftBUTTON = 6;
const int upBUTTON = 5;
const int downBUTTON = 4;
#include "map.h"
#include "ghost.h"                                                 //included file that contains class
#include <SoftwareSerial.h>
SoftwareSerial mySerial = SoftwareSerial(255, TxPin);

// button values
int Rval = 0;
int Lval = 0;
int Uval = 0;
int Dval = 0;
// counter
int i;
//player cordinates
int playerX = 0;
int playerY = 1;
int ghostDELAY = 0;
//LCD command corosponding to the courdenents
int playerLOCATION;

  byte steeve[8] = {
  B00000, //00000
  B01110, // XXX
  B01110, // XXX
  B11111, //XXXXX
  B10101, //X X X
  B01110, // XXX
  B01010, // X X
};

byte ghost[8] = {
  B00000,//
  B01110,// XXX
  B10101,//X X X
  B11111,//XXXXX
  B11111,//XXXXX
  B11111,//XXXXX
  B01010,// X X
  B00000,//
};

Cat Cat1;                                  //creation of the new class
Cat1.Cat();
//Player VI
class Player{
  public:
    int y;
    int x;
    int LOCATION;
 
  Player(){
    y = 1;
    x = 0;
    LOCATION = MAP [x] [y];
  }
 
  void render(int z){
      mySerial.write(z);
      mySerial.write(1);
    }
   
  void run(){
   
  }
 
  void restart(){
    x = 0;
    y = 1;
    LOCATION  = MAP [playerY] [playerX];
    mySerial.write(playerLOCATION);
    mySerial.write(1);
  }
};

//ghost AI
class Ghost{

  private:
    int x;
    int y;

   
  public:
  int LOCATION;
    Ghost(){
      x = 15;
      y = 0;
      LOCATION = MAP[y] [x];     
    }
    void render(int z){
      mySerial.write(z);
      mySerial.write(2);
    }
    void chase(){
      if(ghostDELAY >= 500){
         if(x > playerX){
          mySerial.write(LOCATION);
          mySerial.print(" ");
          x--;
           if (x == -1){
             x = 15;
            }
          LOCATION = MAP [y] [x];
          mySerial.write(LOCATION);
          mySerial.write(2);
          ghostDELAY = 0; 
         }else if(x < playerX){
          mySerial.write(LOCATION);
          mySerial.print(" ");
          x++;
           if (x == -1){
             x = 15;
            }
          LOCATION = MAP [y] [x];
          mySerial.write(LOCATION);
          mySerial.write(2);
          ghostDELAY = 0;
         }
         if(y > playerY){
          mySerial.write(LOCATION);
          mySerial.print(" ");
          y--;
           if (y == -1){
             y = 1;
            }
          LOCATION = MAP [y] [x];
          mySerial.write(LOCATION);
          mySerial.write(2);
          ghostDELAY = 0;
         }else if(y < playerY){
          mySerial.write(LOCATION);
          mySerial.print(" ");
          y++;
           if (y == 2){
         y = 0;
            }
          LOCATION = MAP [y] [x];
          mySerial.write(LOCATION);
          mySerial.write(2);
          ghostDELAY = 0;
         }     
       }
      }
      void restart(){
        x = 15;
        y = 0;
        LOCATION = MAP[y] [x];
        mySerial.write(LOCATION);
        mySerial.write(2);
   
      } 
};

static Ghost ghost1;

void setup() {
 
  pinMode(LED, OUTPUT);
  pinMode(rightBUTTON, INPUT);
  pinMode(leftBUTTON, INPUT); 
  pinMode(upBUTTON, INPUT); 
  pinMode(downBUTTON, INPUT); 
  pinMode(TxPin, OUTPUT);
  digitalWrite(TxPin, HIGH);
 
  mySerial.begin(9600);
  delay(100);
 
    mySerial.write(249);
     for (i = 0; i < 8; i++) {
       mySerial.write(steeve[i]);
    }
    mySerial.write(250);
     for (i = 0; i < 8; i++) {
       mySerial.write(ghost[i]);
    }
 
  mySerial.write(12);                 // Clear             
  mySerial.write(17);                 // Turn backlight on
  mySerial.write(22);
  delay(5);                           // Required delay
  mySerial.print("Look What I Made");  // First line
  delay(3000);
  mySerial.write(12);
  delay(5);
  playerLOCATION  = MAP [playerY] [playerX];
  mySerial.write(playerLOCATION);
  mySerial.write(1);
  ghost1.render(ghost1.LOCATION);
  delay(1000);                        // Wait 3 seconds
}

void loop() {
  Rval = digitalRead(rightBUTTON);
  Lval = digitalRead(leftBUTTON);
  Uval = digitalRead(upBUTTON);
  Dval = digitalRead(downBUTTON);
  playerLOCATION  = MAP [playerY] [playerX];

  //command for right button
  if (Rval == HIGH){
  digitalWrite(LED, HIGH);
  mySerial.write(playerLOCATION);
  mySerial.print(" ");
  playerX++;
   if (playerX == 16){
     playerX = 0;
    }
  playerLOCATION = MAP [playerY] [playerX];
  mySerial.write(playerLOCATION);
  mySerial.write(1);
  ghostDELAY += 250;
  delay(250);
}else  if (Lval == HIGH){ //left button
  digitalWrite(LED, HIGH);
  mySerial.write(playerLOCATION);
  mySerial.print(" ");
  playerX--;
   if (playerX == -1){
     playerX = 15;
    }
  playerLOCATION = MAP [playerY] [playerX];
  mySerial.write(playerLOCATION);
  mySerial.write(1);
  ghostDELAY += 250;
  delay(250);
}else if (Dval == HIGH){  //up button
  digitalWrite(LED, HIGH);
  mySerial.write(playerLOCATION);
  mySerial.print(" ");
  playerY++;
   if (playerY == 2){
     playerY = 0;
    }
  playerLOCATION = MAP [playerY] [playerX];
  mySerial.write(playerLOCATION);
  mySerial.write(1);
  ghostDELAY += 250;
  delay(250);
}else if (Uval == HIGH){  //up button
  digitalWrite(LED, HIGH);
  mySerial.write(playerLOCATION);
  mySerial.print(" ");
  playerY--;
   if (playerY == -1){
     playerY = 1;
    }
  playerLOCATION = MAP [playerY] [playerX];
  mySerial.write(playerLOCATION);
  mySerial.write(1);
  ghostDELAY += 250;
  delay(250);
}else {
   digitalWrite(LED, LOW);
}
ghostDELAY += 1;
ghost1.chase();
delay (1);
if (ghost1.LOCATION == playerLOCATION){
     mySerial.write(12);
     delay(5);
     mySerial.print("      GAME      ");
     mySerial.print("      OVER      ");
     delay(3000);
     mySerial.write(12);
     delay(5);
     mySerial.print("  RESTARTING..  ");
     delay(1000);
     mySerial.write(12);
     delay(5);
    // restart();
     ghost1.restart();
     delay(1000);
  }
}


ghost.h file

Code: [Select]
class Cat{     // an error is returned when "extern" in placed before the class
  private:
   
  public:
  int y; // if extern is placed any where before the varaible or the function it also returns an error
  Cat(){
      y = 0;
    }
    void Max(){
      y = 2;
    }
   
};


map.h file
Code: [Select]
int MAP [2] [16] = {
  {128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143},
  {148, 149, 150, 151, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163}
};

PaulS

Code: [Select]
Cat1.Cat();
This is wrong. You NEVER directly invoke the constructor.

Defining the class Cat in the file called ghost.h, while defining the class Ghost in the ino file border on stupid.

After removing the call to the Cat constructor, and fixing the mismash of mySerial.write() and mySerial.print() calls to be all mySerial.print(), the code compiles.

aaaelite21

The mySerial.write is necessary. I am writing commands directly to the LCD screen and it receives the command by having it written to it and then performs a task based on these commands. The commands are written directly into the firmware of the screen, and that is how the manufacturer said to use the board, I haven't had a problem with that.

the mismatch on the locations of the Ghost and Cat classes has nothing to do with my project. I was simply using the Cat class to demonstrate the problem I was having with the Ghost class while not having to butcher my code so much so I could continue to work on it.

Can you give me an example of how to properly invoke a constructor from another file??? I'm new to using C++. I do not have a problem with any of the other classes I have and I handled them the same way so i dont really see what the problem is.   


aaaelite21

Crap just realized what you were talking about. Wow lol yeah Cat1.Cat was completely wrong. Total dyslexic moment my bad. 

Go Up