Pages: [1] 2 3   Go Down
Author Topic: Blink Without delay help again.  (Read 1350 times)
0 Members and 1 Guest are viewing this topic.
Malaysia
Offline Offline
Sr. Member
****
Karma: 7
Posts: 393
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Hay guys sorry to bother you all
heres my code for now
Code:
unsigned long Ptimer=0;
unsigned long Ntimer;
bool LedState=false;

void setup()
{
  pinMode(13,OUTPUT);
  Serial.begin(9600);
}
void loop()
{
  Ntimer=millis();
  (LedState==false)?TurnOFF():TurnON();
  digitalWrite(13,LedState);
}

int TurnOFF()
{
  if(Ntimer-Ptimer>=10UL)
  {
   Ptimer=Ntimer;
   LedState=!LedState;
   Serial.println("turn off");
   return LedState;
  }
}

int TurnON()
{
 if(Ntimer-Ptimer>=1000UL)
  {
   Ptimer=Ntimer;
   LedState=!LedState;
   Serial.println("turn on");
   return LedState;
  }
}
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?
Logged

Massachusetts, USA
Offline Offline
Tesla Member
***
Karma: 201
Posts: 8684
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Perhaps turning off for 10 milliseconds is too fast to see.
Logged

Send Bitcoin tips to: 1L3CTDoTgrXNA5WyF77uWqt4gUdye9mezN
Send Litecoin tips to : LVtpaq6JgJAZwvnVq3ftVeHafWkcpmuR1e

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

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

Online Online
Faraday Member
**
Karma: 82
Posts: 3831
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
(LedState==false)?TurnOFF():TurnON();

Is that ")" correct? Shouldn't it look like this:

Code:
(LedState==false? TurnOFF(): TurnON() );
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

California
Offline Offline
Faraday Member
**
Karma: 88
Posts: 3359
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
(LedState==false)?TurnOFF():TurnON();

Is that ")" correct? Shouldn't it look like this:

Code:
(LedState==false? TurnOFF(): TurnON() );

Looks fine to me.
Logged

Online Online
Faraday Member
**
Karma: 82
Posts: 3831
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
Looks fine to me.

I guess it must be, else the OP would have mentioned a compile error. But I thought the syntax of the ternary operator was

(A? B : C);

ie, all in parens, not

(A?) B : C;

Live and learn....
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

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

It may not need any parentheses at all, if precedence is clear, e.g.
Code:
z = x > y ? x : y;
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.

Online Online
Faraday Member
**
Karma: 82
Posts: 3831
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
It may not need any parentheses at all

Ah, ok... thanks
Logged

Although I answer in good faith, remember always: Your Mileage May Vary.
Get me on LinkedIn: za.linkedin.com/in/jimbrownza/

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

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

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

ok guys could you all help me to turn this into a class?
heres what i think so far
Code:
class Blink
{
  public:
            Blink(int pin);
            int TurnON();
            int TurnOFF();
 private:
            uint8_t pin;
            unsigned long Ptimer;
            unsigned long Ntimer;
            bool LedState;
};

 Blink::Blink( int pin)
{
  pinMode(pin,OUTPUT);
}

  

int Blink::TurnOFF()
{
   if(Ntimer-Ptimer>=100UL)
  {
   Ptimer=Ntimer;
   LedState=!LedState;
   digitalWrite(pin,LedState);
   return LedState;
  }
}

int Blink::TurnON()
{
 if(Ntimer-Ptimer>=1000UL)
  {
   Ptimer=Ntimer;
   LedState=!LedState;
   digitalWrite(pin,LedState);
   return LedState;
  }
}
void setup(){}
void loop(){}

would this be correct ? it does compile but im lost when i get to this part.

  
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

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

Seattle, WA USA
Offline Offline
Brattain Member
*****
Karma: 601
Posts: 48543
Seattle, WA USA
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Quote
would this be correct ?
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?
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.
Logged

North Queensland, Australia
Online Online
Edison Member
*
Karma: 64
Posts: 2101
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
uint8_t pin;

This needs to be initialised before use, and use different names to avoid insanity (pin/pin).

basic assignment:
Code:
Blink::Blink( int i_pin)
{
  pin = i_pin;
  pinMode(pin,OUTPUT);
}

initialisation list:
Code:
Blink::Blink( int i_pin) : pin( i_pin )
{
  pinMode(pin,OUTPUT);
}

To use you need to create a variable of type 'Blink'

Code:
Blink b_BlinkObj( 4 ); //on pin 4, you probably use different.
void setup(){}
void loop(){

static bool b_Test = false;

if( b_Test )
  b_BlinkObj.TurnON();
else
  b_BlinkObj.TurnOFF();
b_Test = !b_Test;
}

but like everyone said, arduino functions must be used in or after setup
Logged


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

Ok now i really dont know how to make a constructor.
is there any pointer that i could use PaulS?
Logged

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