Show Posts
Pages: 1 2 [3] 4
31  Using Arduino / Programming Questions / Re: Constructor parameter issues on: January 15, 2013, 04:31:16 pm
Code:
if (kind=true){
...
else if (kind=false) {

Comparison versus assignment.

I'm not sure what you mean. 
32  Using Arduino / Programming Questions / Re: Constructor parameter issues on: January 15, 2013, 04:30:53 pm
Code:
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? 
33  Using Arduino / Programming Questions / Constructor parameter issues 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
Code:

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
Code:
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!
34  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 07:51:56 pm
You obviously misread google:
Code:
char * dtostre (double __val, char *__s, unsigned char __prec, unsigned char __flags)
The 2nd argument is where to write to, not the 4th.

https://ccrma.stanford.edu/courses/250a/docs/avr-libc-user-manual-1.2.5/group__avr__stdlib.html

You're quite right.  I saw it and just assumed it was exactly the same as the dtostrf() and could use the same format.  Now I have it working properly.  I really appreciate your help, thank you. 
35  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 07:17:43 pm
Uh ? Since when google turned into a compiler  smiley-evil

Seriously, though, you should post _the whole code_ that generated the error together with the error..

Okay, here is the code that gave me the previous error.
Code:
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <CONVECTRON.h>

CONVECTRON sensor(A0);

LiquidCrystal_I2C lcd(0b0111000,20,4);

void setup() {
 
}

void loop() {
  char buffer[10];
  dtostre(sensor.readpressure(), 10, 2, buffer);
  lcd.init();
  lcd.backlight();
  lcd.print(buffer);
  delay(1000);
}
36  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 07:09:02 pm
Just like I use dtostrf(). I look it up. Using google.

I, too, used google to try to explain it when I tried using it just as I used dtostrf() and received this error.
Code:
print_w_display.cpp: In function 'void loop()':
print_w_display:14: error: invalid conversion from 'int' to 'char*'
print_w_display:14: error: initializing argument 2 of 'char* dtostre(double, char*, unsigned char, unsigned char)'
print_w_display:14: error: invalid conversion from 'char*' to 'unsigned char'
print_w_display:14: error: initializing argument 4 of 'char* dtostre(double, char*, unsigned char, unsigned char)'
37  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 07:00:32 pm
Why? dtostrf() is not dtostre().

 :smiley-sweat:You're right.  How do you use dtostre()?

Thank you!
38  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 06:18:01 pm
post what you are getting, if you make buffer large enough you can manipulate extra text in there.
Values from 0.97 to 177.70, formatted just like that.
39  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 06:10:33 pm
The Print class, which a lot of classes derive from, does not have a method to do that. The dtostre() function can be used to create a string, though, that the Print class can print.

Great thank you!  I have this now
Code:
char buffer[10];
dtostrf(sensor.readpressure(), 10, 2, buffer);
  lcd.print(buffer);
But it doesn't seem to be adding the exponential notation.
40  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 05:46:01 pm
Quote
How do I return a number in scientific notation with the exponents instead of in float form?
You don't. That's a method of presenting the data, not a way of storing/passing it.

Okay then lets say my Arduino code was
Code:
lcd.print(sensor.readpressure());

How do I print it in exponential form?
41  Using Arduino / Programming Questions / Re: help writing library on: January 10, 2013, 04:27:38 pm
Hey guys.  I got the library working.  Thank you for your help.  Now I have a new question  smiley-slim.  How do I return a number in scientific notation with the exponents instead of in float form?
42  Using Arduino / Programming Questions / Re: help writing library on: January 09, 2013, 02:40:18 pm
Ok, sorry.

An Arduino library is usually split in a .h file (declaration) and a .cpp file (implementation), but that's not a strict requirement.
It depends on how the library code is written.

In essence a library is just a bag of reusable code.

That is, a group of related functions or one or more classes.

If by "second code" you mean this:
Code:
class Counter {
public:
    Counter() { _value = 0; };
    void inc() { _value++; };
    void dec() { if (_value > 0) { _value--; } };
    int getValue() { return _value; };
private:
    int _value;
};
this can go into a .h file and no .cpp file is needed, since both declaration and implementation are specified.

Have a look here

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

under "object oriented programming"

Also, look up the tutorial on this very site about how to write an Arduino lib.

I didn't know that you could do it without a .cpp file.  Thank you for your help.  The Arduino library tutorial (I admittedly don't know how I missed) was helpful in writing another library I was working on.  Hopefully now, I will be able to get this one working with a little work.
43  Using Arduino / Programming Questions / Re: help writing library on: January 08, 2013, 07:47:58 pm
Okay.  But if I wanted to get it working with a library, how would I go about doing that?  It should be pretty simple, right?  However, I do need some help with it. 

Thanks!

Which I translate to: I either didn't read your code or I didn't understand a single bit of it... could you convert *my* code into a library ?

It doesn't translate into that exactly.  I did read you code and thought I understood the basics of it.  To confer it into a library wouldn't you just make the second code your .h file and then create you .cpp file?
44  Using Arduino / Programming Questions / Re: help writing library on: January 08, 2013, 07:18:23 pm
Okay.  But if I wanted to get it working with a library, how would I go about doing that?  It should be pretty simple, right?  However, I do need some help with it. 

Thanks!
45  Using Arduino / Programming Questions / Re: help writing library on: December 29, 2012, 08:21:15 pm


You only need to define public and private variables (and functions) that you actually use.

Am I not using any variables for this?
Pages: 1 2 [3] 4