Go Down

Topic: Classes (Read 2825 times) previous topic - next topic

BloodyD

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: [Select]

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

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

or how am i supposed to get this ?

Regards and thanks :)

Coding Badly

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.

BloodyD

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 :-)

AWOL

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

BloodyD

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

AWOL

#5
Jun 10, 2010, 09:07 pm Last Edit: Jun 10, 2010, 09:11 pm by AWOL Reason: 1
From the tutorial you linked:
Code: [Select]
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#?)
"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.

BloodyD

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.

AWOL

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?
"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.

BloodyD

#8
Jun 10, 2010, 09:46 pm Last Edit: Jun 10, 2010, 09:48 pm by Bloodyd Reason: 1
Code: [Select]
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: [Select]
public void DoStuff()
{
   Console.WriteLine("I'm doing something...");
}
is a function for example.

And called by DoStuff();


AWOL

#9
Jun 10, 2010, 10:17 pm Last Edit: Jun 10, 2010, 10:25 pm by AWOL Reason: 1
Class definitions are like structure definitions - they need a semicolon.

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


You haven't instantiated "test", so "test.whatever()" isn't sensible.
"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.

crimony

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: [Select]
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
}
 


BloodyD

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

PaulS

Code: [Select]
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.

BloodyD

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

BloodyD

#14
Jun 13, 2010, 08:25 pm Last Edit: Jun 13, 2010, 08:41 pm by Bloodyd Reason: 1
I get the following error :
Code: [Select]

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: [Select]
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();
 
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy