Classes

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 :smiley:
I'll test some more and report back if it works :stuck_out_tongue:

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 :slight_smile:
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?

Aren't that just values ?
Like int val = 1; , but then just returning the value straight away ?

It's a "void" - it doesn't return anything, much less a const char pointer.

[edit]

if(temp_save = 1)
        Mode = 0;
        else
        Mode = 1;

That "=" is probably a problem.
Why not just "Mode = 1 - temp_save;" ?
[/edit]

Thats right , should be a '==' , but why would i do that ? mode 0 is the 'autorun' mode, 1 is the 'manual' mode, so if temp_save would be on 1 (meaning manual) it would return to automatic.
Unless i turn these values around ofc.

So that void part, would mean i have to turn it into a function to get what i want ?

yeah it's a function, but i mean to get the 'return' part working , i should make it

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

like this then ?
and oh yeah, i see know, sorry :slight_smile:
I didn't tought 'bout it that way, think i should use Mode = 0+temp_save; then instead tough, seems i've made another mistake against my own interpretation.
temp_save should be 1 to change to manual mode, then Mode must be 1 to make it apply in the whole program.

And a void can't return a value, but I can use the return statement to exit the void, right ?
Thanks.

int Mode_check(int Cur_mode)

Yes, except a const char pointer isn't an "int".

ok, so i should leave away the quotes . i've removed some of the minor faults, like forgetting ';' and some other '=', ' " ' , conversion faults, and now at least it compiles.

Great, thanks for helping me so far :slight_smile: Shall i post the program again ? So if you have some more suggestions on how to improve you can give them from the program i'm currently using.

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()
    {
      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;
      
      Serial.println("Do you want to modify the step?(1/0");
      Serial.flush();
      do
      {
        delay(1);
      }
      while(!Serial.available());
      confirmation = Serial.read();
      Serial.flush();
      if(confirmation == 1)
      {
        Mode = 0+temp_save;
        Serial.println("What step do you want to program?");
        do
        {
          delay(1);
        }
        while(!Serial.available());
        Manual_step = Serial.read();
        Serial.flush();
        Serial.println("Succesfull");
      }
      
      else if(confirmation == 0)
      {  
        Serial.println("Do you want to adjust the time/cycle? (1/0)");     
        do
        {
          delay(1);
        }
        while(!Serial.available());
        confirmation = Serial.read();
        if(confirmation == 1 )
        {
          Serial.println("With how much do you want to subtract?");
          do
          {
            delay(1);
          }
          while(!Serial.available());
          Time_change(Serial.read());
          Serial.println("Succesfull");
          return;
        }
      }
    }

      
  int 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)
  Program.Mode_change(Serial.read());
  if(Mode==0)
  Program.Autorun();
  if(Mode==1)
  Program.Manual();
  
}

;D

do
        {
          delay(1);
        }
        while(!Serial.available());

I'm unclear why you've got a "delay" in there,

 while (!Serial.available ()) ;

would do the same job, and prove more responsive at baud rates higher than 9600,

I can't recall why i used the delay, probably to make something that would print a message after f.e. A few minutes that it's still waiting for answer or something.
But you're right, i should do it with timers and then make the interrupt instead of using that delay.
Altough i have to figure out how to config the timers and use them by registers then.
Can you tell me how i should capture an interrupt given by timer1?