hello , First of all Happy new year.
i got this code
class Blink{
public:
void Begin(int pin)
{
pinMode(pin,OUTPUT);
_pin=pin;
state=0;
}
Blink()
{
switch(state)
{
case 0:
digitalWrite(_pin,HIGH);
timer=micros();
state = 1;
break;
case 1:
if (micros()-timer>=100000UL)
{
state=2;
}
break;
case 2:
digitalWrite(_pin,LOW);
timer=micros();
state=3;
break;
case 3:
if (micros()-timer>=200000UL)
{
state=0;
}
}
}
private:
unsigned long timer;
int pin;
int _pin;
int state;
};
Blink LED1,LED2;
LED1 Begin(52);
LED2 Begin(53);
void setup(){
}
void loop()
{
LED1.Blink();
LED2.Blink();
}
I want to be able to crate a class. there are many error that i have never seen b4, if its help im using my mega and version 22 arduino IDE
heres the error
sketch_dec31c:42: error: 'LED1' does not name a type
sketch_dec31c:43: error: 'LED2' does not name a type
sketch_dec31c.cpp: In function 'void loop()':
sketch_dec31c:48: error: invalid use of 'class Blink'
sketch_dec31c:49: error: invalid use of 'class Blink'
could someone teach me how to make a good class that arduino will not say sorry you cant do that...
im been going thru countless example but i still dont understand. i done the morse code example herehttp://arduino.cc/en/Hacking/LibraryTutorial
i try implimenting this onehttp://www.programmingsimplified.com/cpp/source-code/cpp-class-example-program
but i still dont understand. Any tips and help would be greatly appreciated.
Furthermore, only a constructor can have the same name as the class. Your Blink() function is not intended to be a constructor, it is supposed to be a function, so there are two things wrong with it:
(1) it needs a return type, in your case 'void' as it returns nothing.
(2) it can't have the name 'Blink' as that is the name of the class. Alternatively if you want the function to be called Blink, you need to rename the class, for example:
You can't have a member function called Blink in a class called Blink. Your member function is being interpreted as a constructor, which is not what you want, because you are trying to call it in loop(). Rename the function to something else, e.g. doBlink, and declare it to have return type void.
"LED1 Begin();" doesn't make sense. Use "LED1.Begin();" and move it into the setup function.
You have a number of problems. First, you seem to have a serious misunderstanding of what a constructor does. This is the method with the same name as the class. It is called when an instance of the class is created. You are doing stuff in the constructor that is completely inappropriate.
Second, you should have the class definition in a .h file, the class implementation in a .cpp file, and the sketch should be in its own file, and should include the class header file.
Third,
LED1 Begin(52);
LED2 Begin(53);
is NOT how to call an instance's method. These are the lines that the compiler is initially complaining about.
Fourth, the "do something" method can NOT be the constructor, so this is rubbish:
Ok then guys i guess its wrong for me to do this. actually this is only the beginning of a much larger projct that i wanted to do. its actually for me to understand what actually a class is. and yes i have seen numerous tutorial about how to define class, object and what not but seriously during my time studying i didn't learn how to make class and all. so this serve as for me to understand what it is and how i could implement it in the future.
could someone guide me thru the process?
i really like to learn but i seriously dont understand a think about it.
i learn by doing so if you could give me an example for me to work on would be great too.
btw Thanks for the replies, and Happy New Year
Thanks Tom Carpenter, Your reply help me debug the program. but when i try to expand it more,
the led isnt doing what it suppose to.
edit:
when i say expand the code some more, what i did was i change the Blink() to become Blink(int Count) and added some count++ in the case3 and if count==Count then state=4 where by it ends there. but if not equal then its just loop around and go back to state 0. The LED does not blink at all
Your reply help me debug the program. but when i try to expand it more,
the led isnt doing what it suppose to.
edit:
when i say expand the code some more, what i did was i change the Blink() to become Blink(int Count) and added some count++ in the case3 and if count==Count then state=4 where by it ends there. but if not equal then its just loop around and go back to state 0. The LED does not blink at all
I made some code changes. I'm not going to show them to you. They don't work (whatever that means). Please help me.
PaulS Happy New Year,
Haha, PaulS i didn't meant that the code is something i need help with but what i need help is how to structure my class.
haha i think this is one of the dark art if C++ programming that i need to master, but hay its something that a master magician need to do learn to tackle the problem so be it. I will make more and more class exercise until i get it ok . I hope that all of you could help me go thru the process.
class Blinker{
public:
Blinker(int pin)
{
pinMode(pin,OUTPUT);
_pin=pin;
state=0;
delayms = 100;
}
void loop()
{
long now = millis();
switch(state)
{
case 0:
digitalWrite(_pin,HIGH);
timer=now;
state = 1;
break;
case 1:
if (now-timer>=delayms)
{
state=2;
}
break;
case 2:
digitalWrite(_pin,LOW);
timer=now;
state=3;
break;
case 3:
if (now-timer>=delayms)
{
state=0;
}
}
}
void setDelayMs(long dms){ delayms=dms; }
private:
unsigned long timer;
int pin;
int _pin;
int state;
long delayms;
};
Blinker led1(13); // pin 13
Blinker led2(11); // pin 11
void setup()
{
led2.setDelayMs(250); // led2 will be constant at 2 cycles per second
}
void loop()
{
// make led1 speed up over the course of 30 seconds
long delay = 1000 - (millis()/1000)%30 * 30;
led1.setDelayMs(delay);
led1.loop();
led2.loop();
}