Pages: [1] 2   Go Down
Author Topic: Classes  (Read 2232 times)
0 Members and 1 Guest are viewing this topic.
0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 :
Code:
class led
{
    bit led()
    {
         PORTB = 100000;
     }
}

void loop()
{
led::led();
}
or how am i supposed to get this ?

Regards and thanks smiley
Logged

Global Moderator
Dallas
Offline Offline
Shannon Member
*****
Karma: 200
Posts: 12774
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

correction, i mean how to call a function IN a class smiley
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 :-)
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
, 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.

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

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

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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 ^^
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

From the tutorial you linked:
Code:
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#?)
« Last Edit: June 10, 2010, 02:11:02 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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?
Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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 ,
Code:
public void DoStuff()
{
    Console.WriteLine("I'm doing something...");
}
is a function for example.

And called by DoStuff();

« Last Edit: June 10, 2010, 02:48:44 pm by Bloodyd » Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 291
Posts: 25847
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Code:
class test()
A class isn't a function


You haven't instantiated "test", so "test.whatever()" isn't sensible.
« Last Edit: June 10, 2010, 03:25:54 pm by AWOL » Logged

"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.

Australia
Offline Offline
Jr. Member
**
Karma: 0
Posts: 99
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
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:
Code:
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
}
  
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Well , thanks for that explanation :-)
Now, I tried it the following way :
Code:
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 :-)
Logged

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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.
Logged

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

0
Offline Offline
Newbie
*
Karma: 0
Posts: 26
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I get the following error :
Code:
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 ^^;
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();
  
}
« Last Edit: June 13, 2010, 01:41:40 pm by Bloodyd » Logged

Pages: [1] 2   Go Up
Jump to: