Offline
Newbie
Karma: 0
Posts: 46
|
 |
« on: January 15, 2013, 04:05:35 pm » |
Hello everyone, I am working on a pretty simple library for switching things on and off. In my constructor I have included a parameter so it knows if on is HIGH or if on should be LOW. However, the arduino sketch seems to ignore that and treats it all the the same. Any assistance would be greatly appreciated. .h file class Relay { public: Relay(int pin, bool kind); //kind 1 is positive on kind 2 is negative on void on(); void off(); bool kind; private: int _pin; };
.cpp file Relay::Relay(int pin, bool kind) { pinMode(pin, OUTPUT); _pin = pin; }
void Relay::on(){ if (kind=true){ digitalWrite(_pin, HIGH); } else if (kind=false) { digitalWrite(_pin, LOW); }
}
void Relay::off(){ if (kind=true) { digitalWrite(_pin, LOW); } else if (kind=false){ digitalWrite(_pin, HIGH); }
}
Thanks again!
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 120
Posts: 10201
|
 |
« Reply #1 on: January 15, 2013, 04:09:39 pm » |
Relay::Relay(int pin, bool kind) { pinMode(pin, OUTPUT); Do not call Arduino functions in constructors. Use a begin method for initializing the I/O.
|
|
|
|
|
Logged
|
|
|
|
|
Global Moderator
Dallas
Online
Shannon Member
Karma: 120
Posts: 10201
|
 |
« Reply #2 on: January 15, 2013, 04:10:14 pm » |
if (kind=true){ ... else if (kind=false) { Comparison versus assignment.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 45
Posts: 2289
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #3 on: January 15, 2013, 04:24:10 pm » |
Relay::Relay(int pin, bool kind) { pinMode(pin, OUTPUT); Do not call Arduino functions in constructors. Use a begin method for initializing the I/O. To elaborate... The constructor is called before the main() function which does all the configuration and initialisation of the Arduino. Anything you put in a constructor has a chance (depending on what it is) of being either over-written by defaults set in main(), or of not doing anything at all because it relies on initialisation that hasn't been performed yet. Anything which is "arduinoesque", i.e., anything like pinMode(), digitalWrite() etc will deffinately not work in a constructor. The loosely agreed standard is to provide a .begin() method which you call from setup() to configure any IO etc that needs to be configured. You can either move your setting parameters to that method, or keep them in the constructor but store them in properties for the .begin() method to use later.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #4 on: January 15, 2013, 04:30:53 pm » |
Relay::Relay(int pin, bool kind) { pinMode(pin, OUTPUT); Do not call Arduino functions in constructors. Use a begin method for initializing the I/O. To elaborate... The constructor is called before the main() function which does all the configuration and initialisation of the Arduino. Anything you put in a constructor has a chance (depending on what it is) of being either over-written by defaults set in main(), or of not doing anything at all because it relies on initialisation that hasn't been performed yet. Anything which is "arduinoesque", i.e., anything like pinMode(), digitalWrite() etc will deffinately not work in a constructor. The loosely agreed standard is to provide a .begin() method which you call from setup() to configure any IO etc that needs to be configured. You can either move your setting parameters to that method, or keep them in the constructor but store them in properties for the .begin() method to use later. Thank you for the elaboration. What do you put in the constructor then?
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #5 on: January 15, 2013, 04:31:16 pm » |
if (kind=true){ ... else if (kind=false) { Comparison versus assignment. I'm not sure what you mean.
|
|
|
|
|
Logged
|
|
|
|
|
0
Offline
Full Member
Karma: 1
Posts: 222
Arduino rocks
|
 |
« Reply #6 on: January 15, 2013, 04:35:06 pm » |
The constructor is called before the main() function which does all the configuration and initialisation of the Arduino.
Is this always the case? I.e., if you declare a variable in loop() or setup()? Or in a function only called by them?
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 45
Posts: 2289
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #7 on: January 15, 2013, 04:37:58 pm » |
The constructor is called before the main() function which does all the configuration and initialisation of the Arduino.
Is this always the case? I.e., if you declare a variable in loop() or setup()? Or in a function only called by them? Local variables are constructed and placed on the stack at the time of function call. So it should work with a local variable. If the variable is global, then it will be called before main().
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #8 on: January 15, 2013, 04:46:10 pm » |
This is what I have changed it to and now it doesn't work at all. .h class Relay { public: Relay(int pin, bool kind); //kind 1 is positive on kind 2 is negative on void begin(); void on(); void off(); int pin; bool kind; private: };
.cpp Relay::Relay(int pin, bool kind) { }
void Relay::begin(){ pinMode(pin, OUTPUT); }
void Relay::on(){ if (kind=true){ digitalWrite(pin, HIGH); } else if (kind=false) { digitalWrite(pin, LOW); }
}
void Relay::off(){ if (kind=true) { digitalWrite(pin, LOW); } else if (kind=false){ digitalWrite(pin, HIGH); }
}
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 45
Posts: 2289
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #9 on: January 15, 2013, 04:48:20 pm » |
What are you setting theObject.pin to? You need to assign it in the constructor: this->pin = pin;
... and the same with "kind". You're just throwing away those passed parameters.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #10 on: January 15, 2013, 04:56:49 pm » |
What are you setting theObject.pin to? You need to assign it in the constructor: this->pin = pin;
... and the same with "kind". You're just throwing away those passed parameters. Of course, thank you. I got that working again. But it still seems to be ignoring my "kind" parameter and treating the pin the same regardless.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 45
Posts: 2289
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #11 on: January 15, 2013, 04:59:20 pm » |
What are you setting theObject.pin to? You need to assign it in the constructor: this->pin = pin;
... and the same with "kind". You're just throwing away those passed parameters. Of course, thank you. I got that working again. But it still seems to be ignoring my "kind" parameter and treating the pin the same regardless. That was addressed in an earlier reply: if (kind=true) {
is wrong - it should be == not =.
|
|
|
|
|
Logged
|
|
|
|
|
Offline
Newbie
Karma: 0
Posts: 46
|
 |
« Reply #12 on: January 15, 2013, 05:04:25 pm » |
What are you setting theObject.pin to? You need to assign it in the constructor: this->pin = pin;
... and the same with "kind". You're just throwing away those passed parameters. Of course, thank you. I got that working again. But it still seems to be ignoring my "kind" parameter and treating the pin the same regardless. That was addressed in an earlier reply: if (kind=true) {
is wrong - it should be == not =. The comparison vs. assignment one? I didn't understand that, but now it is working. Thank you again for your help.
|
|
|
|
|
Logged
|
|
|
|
|
UK
Offline
Edison Member
Karma: 45
Posts: 2289
What a host of balls she had seen: gaity, the brass buttons...
|
 |
« Reply #13 on: January 15, 2013, 05:09:47 pm » |
= assigns a value, == compares values. eg: if (a = 1) {
will always be true. C has a useful function of "value fallthrough", whereby a value being assigned to a variable can be assigned to a second variable at the same time: a = b = 1;
will set both a and b to 1. One effect of this is that using = in an "if" causes the assignment to fall through to the if, so it assigns 1 to a (for example), and also passes 1 to the if, so it is like writing: a = 1; if (1) {
Other languages, like BASIC, don't have the distinction between = and ==, and assume that = in an "if" is a comparison. But then BASIC doesn't have assignment fallthrough, and a huge number of other incredibly powerful facilities like C does 
|
|
|
|
|
Logged
|
|
|
|
|
United Kingdom
Offline
Faraday Member
Karma: 131
Posts: 4694
|
 |
« Reply #14 on: January 15, 2013, 05:57:21 pm » |
For the code in the .cpp file, this is simpler and probably more efficient: Relay::Relay(int _pin, bool _kind) : pin(_pin), kind(_kind) { }
void Relay::begin(){ pinMode(pin, OUTPUT); }
void Relay::on(){ digitalWrite(pin, (kind) ? HIGH : LOW); }
void Relay::off(){ digitalWrite(pin, (kind) ? LOW : HIGH); }
|
|
|
|
|
Logged
|
Formal verification of safety-critical software, software development, and electronic design and prototyping. http://www.eschertech.com
|
|
|
|
|