Pages: 1 [2] 3   Go Down
Author Topic: Blink Without delay help again.  (Read 1357 times)
0 Members and 1 Guest are viewing this topic.
Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Look here:

http://www.cplusplus.com/doc/tutorial/

Look at the "Object Oriented Programming" setcion
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

heres my code for the blink without delay.
Code:
const uint8_t Led=13;
boolean LedState=LOW;
unsigned long PreviousMillis=0;
unsigned long CurrentMillis;
unsigned long TimerOFF=1000;
unsigned long TimerON=100;

void setup()
{
  pinMode(Led,OUTPUT);
}
void loop()
{
  CurrentMillis=millis();
  if (LedState==LOW)
  {
    if (CurrentMillis-PreviousMillis>=TimerON)
    {
      PreviousMillis=CurrentMillis;
      LedState=HIGH;
    }
  }
  else
  {
    if (CurrentMillis-PreviousMillis>=TimerOFF)
    {
      PreviousMillis=CurrentMillis;
      LedState=LOW;
    }
  }
  digitalWrite(Led,LedState);
}

would this be better in term of implementation.
Logged

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

If you look at the example, the simplest way to vary the on/off time, is to change the value of"interval" when you change the state of the LED.
There is no reason for "currentMillis" to have global scope.
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.

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

(edit: this is a reply to comment #18)

Good answers smiley

As for the "no harm if I put a return there"... well technically it's true but it's a good habit to know if a code line is of any use instead of putting it in "just in case". smiley
In your particular piece of code, the (logical) error is changing a global variable and returning _that same variable_. You must decide how your function and the code that uses it communicate: either via the function return value or via a global variable.

Yes, global variables are generally a bad programming practice, but in an Arduino sketch it's ok to have a few global variables that match the particular hardware setup one is using.
For example, it's common to have an lcd global variable of type LiquidCrystal if a sketch uses an lcd display.

If all your program is doing is blinking ONE led, it's ok to have e.g. ledPin and ledState global variables.
Instead, if you want to write a couple of functions that can be used to blink any led the user specifies, then global variables must be avoided. A class should be used: the variables used to make the led blink (and remember its state) get incapsulated as private data members, and the functions become methods, which work on those private variables.
Each instance of that class (i.e. object) will have its own private state variables, so each led will blink independently of the other leds.

The keyword here is "state". If you have int sum(int a, int b) { return a + b; } you don't have any state to keep between two calls of that function. Instead when you blink an led you call e.g. updateLed() and you have to remember in which state the led was the last time you called that function. Local variables are not enough, as they last only for a function call. Globals (or better yet class private data members) are needed.

I hope it all makes sense... smiley
Logged

Offline Offline
Edison Member
*
Karma: 26
Posts: 1339
You do some programming to solve a problem, and some to solve it in a particular language. (CC2)
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code in reply #19 is much better. AWOL is right, CurrentMillis could be local to loop().
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

i know AWOL, but what I'm trying to achieve here is that the on time is different with the off time, the implementation if this little exercise have a very really application in the future, i am trying to simplify stuff to the very basic so that i can program in the simplest term and when its time for me to make the project at least i have a good(if any) understanding of how to do it. so would this be better?
Code:
const uint8_t Led=13;
boolean LedState=LOW;
unsigned long PreviousMillis=0;
unsigned long TimerOFF=1000;
unsigned long TimerON=100;

void setup()
{
  pinMode(Led,OUTPUT);
}
void loop()
{
  unsigned long CurrentMillis=millis();
  if (LedState==LOW)
  {
    if (CurrentMillis-PreviousMillis>=TimerON)
    {
      PreviousMillis=CurrentMillis;
      LedState=HIGH;
    }
  }
  else
  {
    if (CurrentMillis-PreviousMillis>=TimerOFF)
    {
      PreviousMillis=CurrentMillis;
      LedState=LOW;
    }
  }
  digitalWrite(Led,LedState);
}
Logged

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

Quote
but what I'm trying to achieve here is that the on time is different with the off time,
...which is exactly what happens with the example if you change the value of "interval" when you change the state of the LED.
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.

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Awol,
It is to my limited understanding of C++/C programming that would say in my humblest opinion that the interval only set the duration between how long it will turn on/off.
lets say that i change the interval to 2000
it would mean that the led will turn on for 2 second and then turn off for 2 second and the cycle will repeat it self.
what I'm trying to achieve now is to turn the led on for maybe 3 second and then off for maybe 5 second.
I hope that my explanation is correct and I will be glad if I'm wrong cause only then i could improve my understanding and my self as a whole.
Logged

Johannesburg. UTC+2
Online Online
Faraday Member
**
Karma: 82
Posts: 3853
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ash I think AWOL's point is this...

If you change interval on the fly, at the time you change the LED state, the interval will be correct for the next pass with the LED in the new (on or off) state and will time that on or off section correctly. Then next time the LED state changes, the interval will get changed for that new (off or on) state. Difficult to explain in words.... yet another case where a flowchart will help.
Logged

The Elders of the Internet know who I am.....
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

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

No flowchart required -
Code:
unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval) {
    // save the last time you blinked the LED
    previousMillis = currentMillis;   

    // if the LED is off turn it on and vice-versa:
    if (ledState == LOW) {
      ledState = HIGH;
      interval = ON_TIME;
    }
    else
    {
      ledState = LOW;
      interval = OFF_TIME;
    }
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.

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok now i get his point. haha sorry AWOL for not understanding what you meant by that. urm let me try to make it as been pointed out,

Code:

const int ledPin =  13;
int ledState = LOW;
long previousMillis = 0;
int Count=0;
unsigned long interval;

void setup()
{
  pinMode(ledPin, OUTPUT);      
}

void loop()
{
 
  unsigned long currentMillis = millis();
 
  if(currentMillis - previousMillis > interval)
  {
    previousMillis = currentMillis;  
    ledState=!ledState;
    digitalWrite(ledPin, ledState);
    Count++;
  }
  if(Count%2==0)
  {
    interval = 100UL;
  }
  else
  {
    interval =2000UL;
  }
}

is this something that you meant?
Logged

Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

ok im far off then what you mean and yeah its great. i learn alot today/tonight
Logged

Pages: 1 [2] 3   Go Up
Jump to: