i run the code on the arduino and all it does is turn the LED on pin 13 on and its stay there
i used the serial code to see whether the problem is in the switching, but from what i can see in the serial monitor is that the state change on time.
is my function is the one to blame here?
Yup your rite, I increase the time to 100UL then the blink is noticeable. haha why didn't i think of that. well when you are doing for a long time you tend to get blinded by doing it the hard way where as you could implement the simple way around.
It should be noted, though, that the ternary operator is not the most appropriate here, as its return value is discarded.
An if-then-else statement should be used instead.
Also, the TurnOff and TurnOn functions change a global variable and then return its value, which is then discarded. This is fine as long as it's a leftover from previous implementation attempts, but should be fixed in a complete sketch, because such hard-to-read code is a recipe for headaches in the long run.
Before turning the code into a class I'd rename the variables. Ptimer and Ntimer are really poor names, because without reading and understand the full code one can't tell what their role is.
The standard names in blink-without-delay style code is prevMillis and currMillis (or something like that).
No. The constructor will be called before the init() function is called. It is the init() function that sets up the hardware, so calling pinMode() before init() is a waste of time.
Your class needs a begin() method, called in setup(), where hardware-related stuff is done.
The pin argument to the constructor will not be known to the rest of the methods.
Why is the pin field of one type while the pin argument a different type? You don't plan to use pin -14 do you? Or pin 12,349?
tuxduino i notice that in reply #8 u said that my code change the global variable and then discarded. although i dont really follow what you mean, i wish to change my code to something better, so i hope you would help me tuxduino. How can i make it to become a better code, thus later on i could make it as a function and a class so that i could use it again for other led or what not.
Ok now i really dont know how to make a constructor.
First, you make a list of things that the constructor is supposed to do. Then, you decide whether it is appropriate (possible) for the constructor to do each thing on the list.
ash901226:
tuxduino i notice that in reply #8 u said that my code change the global variable and then discarded. although i dont really follow what you mean, i wish to change my code to something better, so i hope you would help me tuxduino. How can i make it to become a better code, thus later on i could make it as a function and a class so that i could use it again for other led or what not.
Just remove the return statements in turnon and turnoff, and declare them void.
Then, as I said before, change the names of ptimer and ntimer to something that matches their role in the code.
Your code looks a bit convoluted, though (at least to me), I'd start over by copying the blink without delay example (you find it in the IDE somewhere under File => examples)
BTW, do you know the difference between global and local variables, and do you know what "return value" means, when talking about functions ?
ash901226:
tuxduino i notice that in reply #8 u said that my code change the global variable and then discarded. although i dont really follow what you mean, i wish to change my code to something better, so i hope you would help me tuxduino. How can i make it to become a better code, thus later on i could make it as a function and a class so that i could use it again for other led or what not.
Just remove the return statements in turnon and turnoff, and declare them void.
Then, as I said before, change the names of ptimer and ntimer to something that matches their role in the code.
Your code looks a bit convoluted, though (at least to me), I'd start over by copying the blink without delay example (you find it in the IDE somewhere under File => examples)
BTW, do you know the difference between global and local variables, and do you know what "return value" means, when talking about functions ?
tuxduino actually i copied the blink without delay. i just thought that making it into a function will help reduce the complexity.before i was using void as it shouldn't return a thing but well my judgment get the best of me in saying no harm done if i put return. haha ok for the global and local variable. ok a global variable can be use and change by any part of the program this is consider as a bad programming practice that i still cant turn my head around. ok for local variable, only the particular section of the code where the variable is initialize could the variable be used.
return value means in function. urm i cant really explain it but i guess i could give you an idea of my understanding,
lets say that i have a fuction that could make a calculation of something
maybe
int Circle(int Radius)
{
int area=2*(22/7)*Radius;
return area;
}
so basically what it does is when the function circle is call with the radius of the circle inside the bracket, the function will make some calculation with the formula of 2 x phi x radius. the answer to that equation is the are of a circle,where by once complete the function will return the area to the loop in which the circle function is call.
am i rite will all this or is there any flaw in my logic/understanding it.