Show Posts
Pages: [1] 2 3 ... 71
1  Using Arduino / Programming Questions / Re: Using an array in class constructor on: August 03, 2013, 05:47:38 pm
The best way I can think of of doing this is to create the Sonar objects in your main code and then pass them to the class:
Code:
class Sonar {
  int pin;
public:
  Sonar(int pinIn) : pin(pinIn) {
  }
  int GetValue() { return 4; }
};
class Robot {
  Sonar* sonars;
  const uint8_t numsonars;
public:
  Robot(Sonar* sonarsIn, uint8_t numsonarsIn) : sonars(sonarsIn) , numsonars(numsonarsIn) {
  }
  void method() {
    for (uint8_t i=0; i<numsonars; i++) {
      int foo = sonars[i].GetValue();
    }
  }
};

Sonar sonars[4] {3, 4, 2, 1};
Robot robot(sonars, 4);
void setup() {}
void loop() {}

You could also construct the Sonar objects inside the class with new:
Code:
class Sonar {
  int pin;
public:
  Sonar(int pinIn) : pin(pinIn) {
  }
  int GetValue() { return 4; }
};
class Robot {
  Sonar* sonars;
  const uint8_t numsonars;
public:
  Robot(uint8_t numsonarsIn) : numsonars(numsonarsIn) {
    sonars = new Sonar[numsonars];
  }
  void method() {
    for (uint8_t i=0; i<numsonars; i++) {
      int foo = sonars[i].GetValue();
    }
  }
};

Sonar sonars[4] {3, 4, 2, 1};
Robot robot(sonars, 4);
void setup() {}
void loop() {}
There might be a way to do it with templates as well, but could get tricky.
2  Using Arduino / Programming Questions / Re: [?] C++ Declarations on: July 29, 2013, 06:13:13 pm
no difference for that example

Here's an example where it makes a lot of difference:
Code:
#include <stdio.h>
struct foo {
  foo(int i) { printf("foo %d\n", i); }
};

class bar {
  foo foo1;
public:
  bar() { }
};
int main() {}
does not compile. g++ gives:
Code:
% g++ foo.cpp
foo.cpp: In constructor ‘bar::bar()’:
foo.cpp:9:9: error: no matching function for call to ‘foo::foo()’
   bar() { }
         ^
foo.cpp:9:9: note: candidates are:
foo.cpp:3:3: note: foo::foo(int)
   foo(int i) { printf("foo %d\n", i); }
   ^
foo.cpp:3:3: note:   candidate expects 1 argument, 0 provided
foo.cpp:2:8: note: foo::foo(const foo&)
 struct foo {
        ^
foo.cpp:2:8: note:   candidate expects 1 argument, 0 provided
while clang++ gives the much more helpful:
Code:
% clang++ foo.cpp
foo.cpp:9:3: error: constructor for 'bar' must explicitly initialize the member 'foo1' which does not have a default constructor
  bar() { }
  ^
foo.cpp:7:7: note: member is declared here
  foo foo1;
      ^
foo.cpp:2:8: note: 'foo' declared here
struct foo {
       ^
1 error generated.

The only way to initialize foo1 in bar is to use the initializer list, which is what your first example shows:
Code:
class bar {
  foo foo1;
public:
  bar() : foo1(3) { }
};

My advice is to use the initializer list whenever you are initializing a value, and avoid doing things like in your second example. The reason for this is that in your second example, setted is initialized twice -- once to its default value of 0 and then again to true. Now the compiler easily optimizes that out, but if instead it was a class with a large constructor, the class could spend a lot of time setting its values to the default values and then spend even more time setting it to the values you actually want it to. Here's an example of that:
Code:
#include <stdio.h>
struct foo {
  foo() { printf("foo()\n"); }
  foo(int i) { printf("foo(%d)\n", i); }
  int operator=(int i) { printf("operator=(%i)\n", i); return i;}
};

class bar {
  foo foo1;
public:
  bar() { foo1 = 4; }
};

class foobar {
  foo foo1;
public:
  foobar() : foo1(4) { }
};

int main() {
    printf("Initializing instance of bar:\n");
    bar bar1;
    printf("Initializing instance of foobar:\n");
    foobar foobar1;
}
This produces:
Code:
% ./a.out
Initializing instance of bar:
foo()
operator=(4)
Initializing instance of foobar:
foo(4)

As you can see, when bar is initialized, 2 of foo's methods are called, while when foobar is initialized, just one of foo's methods are called. This clearly shows how initializer lists are better to use when dealing with classes with constructors, and it's better to be consistent and always use them.
3  Using Arduino / Programming Questions / Re: If I set an analog pin to OUTPUT the corresponding digital pin always reads 0 on: July 11, 2013, 05:47:39 pm
A0 through A5 are #defined as 14 through 20. If analogRead() sees a number greater than 13, it subtracts 14 from it to make it 0 through 5. This is why both analogRead(0) and analogRead(A0) are the same.

Ignore the physical pin numbers on the '328 when talking about arduino, it just gets confusing.

There's a bunch of things in Arduino that are there to make things "just work" that can be confusing if you look halfway down -- if you just stay at the surface and do things that work, you're fine, and if you understand the libraries through and through it's fine, but if you start looking at some things and not others it can be confusing.
4  Using Arduino / Programming Questions / Re: C constructs for Arduino on: June 21, 2013, 03:14:39 pm
Wow!  Thanks!  I hadn't realized that we could write C++ !!!

I thought that the language was limited to what was given on the reference page... 

It never even occurred to me that there could be more stuff beyond... I guess I didn't read enough of the web pages.
The "Read this before posting a programming question..." sticky at the top of this forum mentions that quite explicitly... at the top of the post.

Quote
ROLF
Rolling On Laughing Floor? Or is this some new acronym?
5  Using Arduino / Microcontrollers / Re: Interrupt For Every Pin on: June 20, 2013, 02:23:43 am
There are (on the uno) three pin change interrupts, one for each port. You can configure which pins can trigger the interrupt, but there's no way to tell which one did unless you save the state of the port and look at which pins changed (which can actually be a very efficient operation: save 1 byte for the "last value" then xor with current  value and bitmask with the ones you have enabled).

There's the PCint library which does all of that for you, but it adds quite a bit of overhead you can avoid if you do them yourself. The best resource is probably the atmega datasheet.

If one interrupt is triggered while interrupts are disabled (which is generally the case when executing an interrupt service routine (ISR)), it will be queued up and the ISR will be called after the processor has executed one instruction after interrupts are reenabled. Each vector can be queued up independently, but if a second trigger for the same interrupt happens while they are still disabled, the ISR is still only called once.

If the pin change for two interrupts happen on the exact same clock cycle, the ISRs will be called in the order specified in the atmega datasheet. It is pretty unlikely, however, that two pins setup to interrupt will change within 60 nanoseconds of each other, so you shouldn't worry about that too much.
6  Using Arduino / Programming Questions / Re: Why would people write comment code this way?! on: June 18, 2013, 11:53:30 am
I use vim's insert block mode to put in a // in front of every line I want to get rid of. Makes it really clear even without syntax highlighting
Code:
^v10jI//^[
7  Using Arduino / Programming Questions / Re: readBit() Not understood on: June 17, 2013, 02:25:07 am
byte is an 8-bit integer. 11111110 (decimal) is 110110010000001112 (binary). The top bunch will be cut off because it doesn't fit in 8 bits, so your number is now just 1112 or 710. If you wanted led to be 111111 in binary, you can use hex notation: byte led = 0x3f;

Aside from that, your code looks pretty much correct, although could be a lot shorter and easier to understand if you used an array for the pin numbers.
8  Using Arduino / Programming Questions / Re: what does #endif //ECHO_TO_SERIAL do in this code on: June 14, 2013, 12:52:04 am
Having said that, there are probably better ways to do this that create less visual pollution in the code.
Code folding
9  Community / Bar Sport / Re: 3D gun printing good or bad on: June 13, 2013, 12:54:31 am
Guns are not dangerous. Regulation of guns is pretty silly, especially since the actual material (steel) is easy to come by and it's just how it's shaped that makes it "dangerous" (even though they're not). Furthermore, guns are very easy to make and once one is made, it can be used forever.

Bullets are dangerous. They contain explosives. It's hard to make explosives since the actual materials are hard to come by so you need to do some chemistry. And even if one bullet is made, it can only be used once. Regulation of bullets is a great idea. The only problem is that people could get mad that they have to show ID and do background checks, etc. whenever they buy another magazine.

You can't put 90%, I bet, of anyone you poll on the street, in front of a spinning machine that they can "easily" shape steel into a gun that can forever fire bullets. Don't tell me how easily YOU could make a forever gun. You are not making any sense and you are not addressing the 3D printing at all. Did you just want to bring out gun regulations for a chance to "fight" the other side? I didn't know you were this type of person.
I think you were responding to the OP from a moral standpoint and saying that people shouldn't own guns at all. I was responding from a legal standpoint, that 3D printing guns should be legal because it would be impossible to enforce and enforcement would mean a severe restriction on privacy (spying on people's internet connections to make sure they don't download the gun file). I've had this conversation before and was subconsciously assuming that you had the context from that conversation as well. Let me clarify a bit:

3D printing of guns could be considered bad because it makes it really easy for anybody to make them. Right now, there are laws that say that only certain people can own guns. With the 3D printing of guns, it becomes extremely easy to break that law (at least when 3D printers become desktop devices in 5 years). I'm saying that these laws are misguided. The government should not control the ownership of guns, but rather the ownership of cartridges. Cartridges are much easier to regulate because they're consumables and much harder to manufacture (even if making gunpowder becomes easy, anybody that can make the gunpowder can also just make a straight up bomb which is likely a bigger problem)

I never meant that any random person could make a gun that lasts forever. I meant that making a gun is much, much easier than making a bunch of cartridges, and the gun lasts longer.
10  Community / Bar Sport / Re: 3D gun printing good or bad on: June 12, 2013, 09:00:05 pm
Guns are not dangerous. Regulation of guns is pretty silly, especially since the actual material (steel) is easy to come by and it's just how it's shaped that makes it "dangerous" (even though they're not). Furthermore, guns are very easy to make and once one is made, it can be used forever.

Bullets are dangerous. They contain explosives. It's hard to make explosives since the actual materials are hard to come by so you need to do some chemistry. And even if one bullet is made, it can only be used once. Regulation of bullets is a great idea. The only problem is that people could get mad that they have to show ID and do background checks, etc. whenever they buy another magazine.
11  Using Arduino / Programming Questions / Re: How can I return a 12 byte from a function? on: June 11, 2013, 08:34:12 pm
because I didn't realize that an array length n is universally known to be 12. If you want to use a one byte type, why not define the array as a byte data type. Very confusing to me...
Because byte is not standard C. char is the only standard C known-by-compiler one byte datatype. uint8_t is defined in stdint.h and byte is a nonportable arduinoism (although one I do like)
Quote from: Arduino.h
Code:
typedef uint8_t byte;
Quote from: stdint.h
Code:
typedef unsigned char uint8_t;

Quote
The real problem, as I see it, it that WizenedEE's code is unnecessarily complicated. Reference variables were invented for just the purpose that OP wanted. Wrapping an array in a struct just so that a function can return a single address is useless overhead. Use a reference variable and pass the array address to the function.
I understand and often use passing by reference but I don't really like it. It goes against the ideas of functional programming (passing by reference means there's side effects). It's also not great because defining a variable takes two lines: one to declare it and allocate space and another to call a function to fill it up with nice data. Of course, those are minor issues that people generally ignore. I was simply pointing out an alternative. Furthermore, the OP specifically asked for how to "return" 12 bytes. Of course, one could assume that the OP doesn't know what he/she needs which may be true, but it's hardly considered good form to berate someone for giving a solution to the literal question posted.

Quote
I misunderstood what you were trying to do.
I assumed it was assumed that people posting replies were trying to help the OP by posting solutions to their problems.

Quote
Maybe this is a suggestion:
Code:
pointertest.h: No such file or directory
12  Using Arduino / Programming Questions / Re: How can I return a 12 byte from a function? on: June 10, 2013, 11:15:27 pm
WisenedEE code probably meant the line to be:

Code:
  myd.data[4] = '4';

since it is a character array.
No, char is just the standard one byte type. So a char array of length n is universally known to be 12 bytes.
13  Using Arduino / Programming Questions / Re: How can I return a 12 byte from a function? on: June 10, 2013, 10:38:43 pm
Code:
struct mydatas {
  char data[12];
};
mydatas f();

void setup() {
  mydatas foo = f();
}
void loop() {}

mydatas f() {
  mydatas myd;
  myd.data[4] = 4;
  return myd;
}
14  Using Arduino / Programming Questions / Re: expected unqualified-id before 'if' on: June 09, 2013, 01:10:22 pm
All code to be executed needs to be in a function.

your if statement is not in a function.

Also, you should be using == rather than = in the if.
15  Development / Suggestions for the Arduino Project / Re: bug find? redclaration of int causes infinite loop... on: June 08, 2013, 01:05:09 pm
Not a bug in the programing language.  Just a bug in your programming. 

Local variables can have the same name as global variables.  Your code will use the most local version of the variable.
No program error should cause a "reboot loop." It sounds like a problem with the circuit.

also, instead of
Code:
while (1 ==1)
it makes more sense to say while(true)
Pages: [1] 2 3 ... 71