Classes

Hello :-) I'm looking for a brief explanation and perhaps a little introduction on how i should make a class (class() ..) but also how i should create and use functions in one and the same file.

Let's say i want a function to blink a led, to keep it easy , and want to call it in the normal 'loop'. is it like :

class led
{
    bit led()
    {
         PORTB = 100000;
     }
}

void loop()
{
led::led();
}

or how am i supposed to get this ?

Regards and thanks :)

I’m looking for a brief explanation and perhaps a little introduction on how i should make a class (class() …) but also how i should create and use functions in one and the same file.

Those are two very different questions requiring somewhat lengthy replies. I suggest you focus on one of the questions first (e.g. the “functions” question) and then pursue the other.

correction, i mean how to call a function IN a class :) i know how to create a function in arduino, but i want to know how to create it in a class, as i don't know how to call them from extern, as well as i dont know how to define functions as public.

I have a bit experience in C#, but i've come across some specialties in arduino and that's why i asked it ;-)

and i can't make sense of http://www.arduino.cc/en/Hacking/LibraryTutorial actually, so if you want please give me a clear explanation :-)

, as i don't know how to call them from extern

You've used "Serial.print"? So, you know how to call a class member.

as well as i dont know how to define functions as public

Well, the clue's in the keyword "public".

Well .. that's a dry answer .. in C# it was 'public void' , is it the same here then ? Or was it like i asked in OP ? Could be i'm mixing up C and PHP too tough ^^

From the tutorial you linked:

class Morse
{
  public:
    Morse(int pin);
    void dot();
    void dash();
  private:
    int _pin;
};

So, "Morse(int pin)" is the constructor, and "dot" and dash" are member functions that are both "public" and "void".

(Sorry, what's C#?)

i read that part, but i don't get it :s is the void then a function ? C# is a variant on C and C++ . A bit similar but there are some differences between C and C# as i've heard.

is the void then a function ?

No, "void" is the return type of of the function, i.e., it doesn't return anything.

Doesn't C# have functions?

class test()
{
  public:
    void Blink(int led)
    {
      digitalWrite(led,HIGH);
      delay(500);
      digitalWrite(led,LOW);
      delay(500);
    }
}

void setup()
{
  pinMode(PORTB,OUTPUT);
}

void loop()
{
  test.Blink(13);
}

Returns error: expected unqualified-id before ')' tokenBad error line: -3

Any tips ? :-)

EDIT:

yes it does ,

public void DoStuff()
{
    Console.WriteLine("I'm doing something...");
}

is a function for example.

And called by DoStuff();

Class definitions are like structure definitions - they need a semicolon.

class test()

A class isn’t a function

You haven’t instantiated “test”, so “test.whatever()” isn’t sensible.

A class isn't a function

Correct, also: A class isn't an object, it's a type. This means you need an object to operate on:

class Test
{
  public:
    void Blink(int led)
    {
      digitalWrite(led,HIGH);
      delay(500);
      digitalWrite(led,LOW);
      delay(500);
    }
};

void setup()
{
  pinMode(PORTB,OUTPUT);
}

void loop()
{
  Test tester; // instantiate an object of type Test (a class)

  tester.Blink(13); // call the class method on the object
}

Well , thanks for that explanation :slight_smile:
Now, I tried it the following way :

int tijd = 60;
int stap[6] =  
    {
      B001101 , B110001 , B000111 , B010011 , B001101 , B011100};
  
class Arduino
{
  public:

    void port()
    {
      Serial.begin(9600);
      DDRB = B111111;
      Serial.println(DDRB,BIN);
    }
    
    void autorun()
    {
      for(int i=0;i >=5;i++)
      {
        Serial.println("Hallo");
        PORTB = stap[i];
        delay(tijd);
        Serial.println(PORTB,BIN);
      }
    }

};
void setup()
{
  Arduino config;
  config.port();
}

void loop()
{
  Arduino Program;
  Program.autorun();
}

it calls the config.port(); , but it doesn’t call the autorun.

For as far as I can see this, I do exactly the same for calling the functions on both setup and loop.

Off topic, the goal is to create a program that when the arduino is started it automaticly runs the autorun(), and when i give a command trought the serial monitor, that I’m able to program what it should do, from the steps in the var stap.

Thanks for the patience :slight_smile:

for(int i=0;i >=5;i++)

This can be read as for i starting at 0, while i is greater than or equal 5, implement the body of the loop, then increment i by 1.

The while condition is never true.

Ow .. that was the worst fault ever .. Thanks :-D I'll test some more and report back if it works :p

I get the following error :

 In member function 'void Arduino::autorun()':
error: void value not ignored as it ought to be In member function 'void Arduino::manual(int)':
 In member function 'void Arduino::Mode_change(int)':
 In member function 'void Arduino::Mode_check(int)':
 In function 'void loop()':

But I can’t see the fault :s

This is the following program.
For some more info; the arduino is meant so controls the mosfets connected on PortB, 6 of them.
With these mosfets i can control the state of a BLDC floppy drive motor, I can do it the normal way, but i’d like the extra’s as time adjusting and waiting time / change of the mosfets, control.
I really can’t figure it out, thanks already for the courage to go trough this, probably spaghetti programmed code ^^;

int Stap[6] =  
    {
      B001101 , B110001 , B000111 , B010011 , B001101 , B011100};
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.

class Arduino
{
  public:
    void port()
    {
      Serial.begin(9600);
      DDRB = B111111;
    }
    
    void autorun()
    {
      for(int i=0;i <=5;i++)
      {
        //Correct_mode = Mode_check(0);
        //if(Correct_mode == 1)
        output(i);
      }      
    }
    
    void output(int do_step)
    {
      PORTB = Stap[do_step];
      delay(Time);
    }
     
    void manual(int manual_step)
    {
      Correct_mode = Mode_check(1);
      if(Correct_mode == 1)
        output(manual_step);
    }
    
    void Time_change(int minus)
    {
      Time -= minus;
    }
    
    void Mode_change(int mode)
    {
      int temp_save = mode;
      char confirmation;
      int stap;
      Serial.println("Do you want to modify the step?(Y/N");
      Serial.flush();
      do
      {
        delay(1);
      }
      while(!Serial.available);
      confirmation = Serial.read();
      Serial.flush();
      if(confirmation == "Y")
      {
        if(temp_save = 1)
        Mode = 0;
        else
        Mode = 1;
        do
        {
          delay(1);
        }
        while(!Serial.available());
        stap = Serial.read();
        Serial.flush();
      }
      return stap;
      else if(confirmation == "N")
      {  
        Serial.println("Do you want to adjust the time/cycle? (Y/N)");     
        do
        {
          delay(1);
        }
        while(!Serial.available());
        confirmation = Serial.read();
        if(confirmation =="Y")
        {
          Serial.println("With how much do you want to subtract?")
          do
          {
            delay(1);
          }
          while(!Serial.available());
          Time_change(Serial.read());
          Serial.println("Succesfull");
          return;
        }
      }
    }

      
  void Mode_check(int Cur_mode)
  {
      if(Cur_mode==Mode)
        return "1";
      else
        return "0";
  }     
};
void setup()
{
  Arduino config;
  config.port();
}

void loop()
{
  Arduino Program;
  if(Serial.available>0)
  Manual_step = Program.Mode_change(Serial.read());
  if(Mode==0)
  Program.autorun();
  if(Mode==1)
  Program.manual();
  
}
confirmation = Serial.available();
      Serial.flush();
      if(confirmation == "Y")

Oops.
(make that two “oops”)

Correct_mode = mode_check(1);

“mode_check” is void, so you can’t assign the value that it doesn’t return to anything.

Why ? :s it saves the serial data to the var, and then clears the serial data if any there , or am i wrong ? :s

And the void , i tell it to return something, a 1 or a 0, that is something, or what do you mean?

"available" returns how many characters are in the rx buffer, not the vaues of the characters.

== "Y" is very unlikely to work.

Ow .. another failure today -.- thanks for getting that 1 out :-) had it on 1/2 other places to , fixed those.

Updating Code on previous page.

 void Mode_check(int Cur_mode)
  {
      if(Cur_mode==Mode)
        return "1";
      else
        return "0";
  }

void? string pointers?