Library function?

Hey,
I wanted to write my own library, so i could keep my program a bit readable, but i'm getting some weird error which i can't figure out.

Using the following as header file :

```
**#ifndef Mosfets
#define Mosfets
#include "Wprogram.h"

class Mosfets()
{
public:
     void Init();
     void Autorun();
     void Manual();
     void Change_mode(int data);
     void Output(int Do_step);
     void Time_change(unsigned int Calculation);
     uint8_t Mode_check(int Cur_mode);
}

#endif**
```

And the following is the cpp file

/*Written and created by Daneo*/
#include "Wprogram.h"
#include "Mosfets.h"

int Stap[6] = { B001101 , B110001 , B000111 , B010011 , B001101 , B011100}; //Outputs on PORTB to control MOSFET's
int Time = 50; //Initial time in ms the arduino will do 
int Correct_mode; //0 will be defined as autorun, initial step, 1 as manual
int Mode = 0; //Autorun mode
int Manual_step; //This variable will hold the step that the user wants the arduino to be in.

void Mosfets::Init()
{
      Serial.begin(9600);
      DDRB = B111111;
}

void Mosfets::Autorun()
{
      for(int i = 0; i <= 5; i++)
      {
            Correct_mode = Mode_check(0);
            if(Correct_mode == 1)
                  Output(i);
      }
}

void Mosfets::Manual()
{
      Correct_mode = Mode_check(1);
      if(Correct_mode == 1)
            Output(Manual_step);
}

void Mosfets::Change_mode(int data)
{
      int temp_save = data;
    int confirmation;
    Serial.println("Do you want to modify the step?(1/0)");
    Serial.flush();
    while(!Serial.available()); // Wait for answer
    confirmation = Serial.read();
    Serial.flush();
    if(confirmation == 1)
    {
        Serial.println("What step do you want to program? Enter 8 to return to automatic.");
        while(!Serial.available()); // Wait for answer
        int temp_step = Serial.read();
        
            if(temp_step > 0 && temp_step <= 5)
        {
          Mode = 1;
          Manual_step = temp_step;
        }
        
        else if(temp_step == 8)
        Mode = 0;

        Serial.flush();
        Serial.println("Succesfully changed Mode to " + Mode);
      }
      else if(confirmation == 0)
    {  
        Serial.println("Do you want to adjust the time/cycle? (1/0)");     
        while(!Serial.available());
        confirmation = Serial.read();
        if(confirmation == 1 ) //It seems he does
            {       
                  Serial.println("Enter the value you want to adjust the time with (if necessary minus included)");
                  while(!Serial.available()); // Wait for answer.
                  Time_change(Serial.read());
                  Serial.println("Succesfull");
                  return;
        }
            else
                  return;
    }
}

void Mosfets::Output(int Do_step)
{
      PORTB = Stap[Do_step];
      delay(Time);
}

void Mosfets::Time_change(unsigned int Calculation)
{
      Time += calculation;
}

uint8_t Mosfets::Mode_check(int Cur_mode)
{   
    if(Cur_mode==Mode)
            return 1;
    else
            return 0;
}

Now when trying to compile i get an error about the fat part above.
The error is the following one : [b]Mosfets.cpp:90: error: explicit qualification in declaration of 'uint8_t Mode_check(int)'[/b]

I tried defining it as explicit, int Mode_check, byte Mode_check, but no luck :frowning:

Can some1 give me an explanation ?

Thanks

uint8_t Mosfets::Mode_check(int Cur_mode)
{  
    if(Cur_mode== ::Mode)
            return 1;
    else
            return 0;
}

?

or int Mosfets::Mode_check(int Cur_mode)
{
if(Cur_mode== ::Mode)
return 1;
else
return 0;
}
neither work, was just testing what it could be, according to your reaction that type should only be used when defining var's ?

I don't think this:

#ifndef Mosfets
#define Mosfets

is your problem, but it is a potential source of confusion.

The more usual construct is

#ifndef _Mosfets_h
#define _Mosfets_h

class Mosfets[glow]()[/glow] ?

void Mosfets::Time_change(unsigned int [glow]C[/glow]alculation)
{
      Time += [glow]c[/glow]alculation;
}

And the real biggy:

      uint8_t Mode_check(int Cur_mode);
}[glow];[/glow]

hey, thanks for the corrections , going to test right away, but why is the '_' in front of it ? and the '_h' does it mean header file or what is it for ?
Just so i understand the syntax and reasons behind it.

I read the tutorial again, and it says the _ is for marking private var's ?

Also, now I corrected the errors, i get an error on the var Stap not being declared in the scope on line 81, which is the Output() void, but i've declared it in the beginning of the cpp file , which i think is right, isn't it ? :s

Mosfets.cpp:81: error: 'Stap' was not declared in this scope

but why is the '_' in front of it ? and the '_h'

There's no compulsion to do it like that, but it makes it very unlikely that anyone else will ever define a macro starting with an underscore and ending with "_h".
Using plain vanilla "Mosfet" is not sensible, when you're going to define a class with exactly the same name.

Some reading:include guard - Wikipedia

Ah ok, i see, so basicly for the safety.

Can you give me some more information on how to declare the var's so they can be used by the whole program?
Tought that was done by the 'public:' section, should i define them there, or can i do it in the '.cpp' file itself? All at the top ?
Because neither of them work, always getting the error 'Stap' (with the '_' in front of each var '_Stap' ==> declared in '.h', tried both) was not declared.

Or is it because i'm trying to acces a certain value of the array '_Stap' by this method : _Stap[Do_step]; ?

This works in Arduino tool, but don't know if i should do it different in the library.

As a rule, you should never declare variables as public - that's one of the key selling points of encapsulation.

Can you post your code as it is now?

#ifndef _Mosfets_h
#define _Mosfets_h
#include "Wprogram.h"

class Mosfets
{
public:
      int _Stap[6] = { B001101 , B110001 , B000111 , B010011 , B001101 , B011100};
      int _Time = 50; //Initial time in ms the arduino will do 
      int _Mode = 0; //Autorun mode
      void Init();
      void Autorun();
      void Manual();
      void Change_mode(int data);
      void Output(int Do_step);
      void Time_change(unsigned int Calculation);
      uint8_t Mode_check(int Cur_mode);

}

#endif

As far as the '.h' file goes, and the '.cpp' :

/*Written and created by Daneo.*/
#include "Wprogram.h"
#include "Mosfets.h"


void Mosfets::Init()
{
      Serial.begin(9600);
      DDRB = B111111;
}

void Mosfets::Autorun()
{
      for(int i = 0; i <= 5; i++)
      {
            Correct_mode = Mode_check(0);
            if(Correct_mode == 1)
                  Output(i);
      }
}

void Mosfets::Manual()
{
      Correct_mode = Mode_check(1);
      if(Correct_mode == 1)
            Output(Manual_step);
}

void Mosfets::Change_mode(int data)
{
      int temp_save = data;
    int confirmation;
    Serial.println("Do you want to modify the step?(1/0)");
    Serial.flush();
    while(!Serial.available()); // Wait for answer
    confirmation = Serial.read();
    Serial.flush();
    if(confirmation == 1)
    {
        Serial.println("What step do you want to program? Enter 8 to return to automatic.");
        while(!Serial.available()); // Wait for answer
        int temp_step = Serial.read();
        
            if(temp_step > 0 && temp_step <= 5)
        {
          _Mode = 1;
          Manual_step = temp_step;
        }
        
        else if(temp_step == 8)
        _FMode = 0;

        Serial.flush();
        Serial.println("Succesfully changed Mode to " + Mode);
      }
      else if(confirmation == 0)
    {  
        Serial.println("Do you want to adjust the time/cycle? (1/0)");     
        while(!Serial.available());
        confirmation = Serial.read();
        if(confirmation == 1 ) //It seems he does
            {       
                  Serial.println("Enter the value you want to adjust the time with (if necessary minus included)");
                  while(!Serial.available()); // Wait for answer.
                  Time_change(Serial.read());
                  Serial.println("Succesfull");
                  return;
        }
            else
                  return;
    }
}

void Mosfets::Output(int Do_step)
{
      PORTB = _Stap[Do_step];
      delay(_Time);
}

void Mosfets::Time_change(unsigned int Calculation)
{
      _Time += Calculation;
}

uint8_t Mosfets::Mode_check(int Cur_mode)
{   
    if(Cur_mode==_Mode)
            return 1;
    else
            return 0;
}    ;

What's the problem, and why have you got public variables?

If you make a variable public, then any Tom Dick or Harry can change it without your object knowing about it, and so you can't rely on the value.

I confused the fact that a private var can't be accessed by any other function from the outside, but if you define it within the class and not within a function that it can be used all over the class .

Sorry 'bout that.

Now i got the following :

#ifndef _Mosfets_h
#define _Mosfets_h
#include "Wprogram.h"

class Mosfets
{
private:
      int _Stap[];
      int _Time; //Initial time in ms the arduino will do 
      int _Mode; //Autorun mode
      int _Manual_step;
      int _Correct_Mode;
public:
      void Init();
      void Autorun();
      void Manual();
      void Change_mode(int data);
      void Output(int Do_step);
      void Time_change(unsigned int Calculation);
      uint8_t Mode_check(int Cur_mode);

}

#endif

and

void Mosfets::Init()
{
      Serial.begin(9600);
      DDRB = B111111;
      _Stap[]= {B001101 , B110001 , B000111 , B010011 , B001101 , B011100};
      _Time = 50;
      _Mode = 0;
      _Correct_Mode = 0;
}

This is not all of the cpp file, but i'm getting an error on this void, namely Mosfets\Mosfets.cpp:6: error: two or more data types in declaration of 'Init'

So it's not allowed to define them there ? Why not ?

You're still missing a semicolon on your class definition. See my earlier reply #3.

Must've done it away accidently then.
Anyway added it, and now I'm getting the error that it's expecting a semicolon instead of { ,

_Stap[]= {B001101 , B110001 , B000111 , B010011 , B001101 , B011100};

right there.

That's the correct way of initializing an array right ?

That's the correct way of initializing an array right ?

Wrong.

If i put

      int _Stap[]= {B001101 , B110001 , B000111 , B010011 , B001101 , B011100};

in the header , private, then it states ' '_Stap' was not declared in this scope' .

If i put _Stap[] in the header and initialize it in the Init, then it says it needs a ';' before '{' .

So .. can you tell me what I'm doing wrong then ?

This
_Stap[]= {B001101 , B110001 , B000111 , B010011 , B001101 , B011100}; is simply illegal in C or C++.

However, it's unclear to me if "Stap" is meant to be part of your class (in which case you'll either have to make it static, or initialise it explicitly in "init") or as you had it in your first post, and nothing really to do with your class.

Ok, but how is it supposed to be then ? Should i use a global variable ? But I would still like to know how to initiate an array tough :slight_smile:

class Mosfets
{
public:
      void Init();
      void Autorun();
      void Manual();
      void Change_mode(int data);
      void Output(int Do_step);
      void Time_change(unsigned int Calculation);
      uint8_t Mode_check(int Cur_mode);
private:
        static int Stap[6];
      
};

int  Mosfets::Stap [] = {B010101, B11011//yadda};

Ok thanks.
If i declare it public, can i acces it from outside the .cpp ?
Like if i want to check in the arduino program what value '_Mode' has, can i acces it with Mosfets._Mode ? Or Mosfets::_Mode ?
Because now i got it going trough the init, which i had to rename to Mosfets::Mosfets to make it occur the first time the lib is called.

Or isn't this possible? Because i can compile now, the Mosfets::Mosfets is being called, but it seems i can't do

 if(Mosfets._Mode==1){
Mosfets.Manual
}

while this would be a requirement to determine to which function i need to call .
:o