Show Posts
Pages: 1 ... 114 115 [116] 117 118 ... 265
1726  Using Arduino / Programming Questions / Re: Which one is faster or better ? why? on: May 25, 2013, 05:27:36 am
The thing I always point out:

"const int" is strongly typed, "#define" is not.

Using "const" can both warn you if you're doing something wrong - plus it can help the compiler to do maths properly.  For instance, take this little snippet using floats:
Code:
#define MYDIV 3
const float mydiv = 3;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  float sum;
  unsigned int randomnumber = random(1,50000);  
  sum = randomnumber / MYDIV;
  Serial.print(sum);
  Serial.print(" ");
  sum = randomnumber / mydiv;
  Serial.println(sum);
  delay(1000);
}
Get a random number, divide it by three.  Simple enough...

Both the #define and the const are set to 3, so you'd expect the same results, yes?  No.

#define is a integer of 3, but the const is a float of 3.  An integer divided by an integer is an integer - regardless of the destination variable type, but an integer divided by a float yields a float.  The output of that little sketch is:

Code:
5602.00 5602.67
10299.00 10299.67
10842.00 10842.33
4452.00 4452.67
10604.00 10604.33
6892.00 6892.33
9855.00 9855.00
10012.00 10012.00
2366.00 2366.67
9285.00 9285.00
10304.00 10304.00
...

See those discrepancies?  The column on the right is the right value, the column on the left is not what we want.

Tip: You can think of mathematical operators as functions.  a / b can be thought of as div(a,b).  Divide two integers, and the function prototype would look like: int div(int a, int b).  Divide an int by a float and it may look like: float div(int a, float b).

That's the advantage of the strongly typed constants over the untyped #define values.
1727  Using Arduino / Programming Questions / Re: How can use a value from a class in a cpp file on: May 25, 2013, 05:01:20 am
Is mySerial declared public or private?

And doesn't the GSM class provide its own functions for accessing the serial?
1728  Using Arduino / Programming Questions / Re: Arduino does not understand a ranged FOR loop on: May 25, 2013, 04:55:51 am
"simple" for loops can be quite complex and convoluted.

Not many people use that syntax for a for loop, as it's not as intuitive as putting the assignment (or using the iterator variable directly) inside the loop itself.
Well also, as was mentioned, the ranged syntax is a feature of the new C++ standard.  So it depends on whether you are using new compilers (and possibly options to enable the new standards).  For example, on my Fedora 17 system, which uses GCC 4.7.2, I have to add the option -std=c++11 or -std=gnu++11 to enable the ranged syntax.  My laptop, which is frozen at Fedora 14 due to the graphics chip and uses GCC 4.5.1, does not support -std=gnu++11, and if I use the older form of the switch (-std=gnu++0x), it still doesn't have support for ranged for statements.  The GCC released with the IDE is even older (4.3.2 for the Linux distros).

BTW, the chipKIT currently uses 4.5.1 - so no C++11 there either...
1729  Using Arduino / General Electronics / Re: My "where do babies come from?" of electronics. on: May 25, 2013, 04:49:06 am
Those 9V batteries are capable of putting out a couple of amps, but only for a few minutes before they're completely drained.

The typical internal resistance of a PP3 is between 1Ω and 3Ω depending on manufacturer and chemistry.  Let's take 2Ω as a good mean value.

At 2Ω and a nominal 9V voltage, I=V/R, so I = 9 / 2 = 4.5A.  That's the short circuit current of a typical PP3.

Pass that through 117 LEDs in parallel (assuming the "perfect" LED - exactly the same voltage drop as all other LEDs and zero resistance) - 4.5/117 = 38mA.  That's normally above the IF of a typical LED, but won't normally cause the LED to blow immediately.

Now, the world is not perfect.  So what you have is equivalent to a perfect 9V power supply, with a 2Ω current limiting resistor, and a bunch of LEDs, each with its own VF.  What that means in practice is that some LEDs will get a slightly higher current than others.  You probably noticed that the LEDs may not have all been the same brightness.

The ones with the most current will blow first.  That will mean there's a greater proportion of the current available to the rest of the LEDs.  The next ones to blow will do the same - increase the current available to the rest, and so on.  A cascade effect will rapidly occur where just one LED blowing could cause the rest of the LEDs to blow.

Of course, all this relies on the battery holding out, which at 4.5A and the typical 565mAh of an alkeline PP3, would take (T=mAh/mA = 565/4500) = 0.12555...h, or about 7 and a half minutes.
1730  Using Arduino / Programming Questions / Re: please help!!! on: May 25, 2013, 04:37:30 am
There's more to controlling a servo than just the pulse width.

The pulse has to happen at the proper frequency.  Typical servos require around 50Hz pulses, with the duty cycle lasting between around 800uS and 2ms for 0° to 180° - actual values vary depending on the servo.

Your analogue reading loop with the delay(1) plus the overhead of the rest of that loop gives around a 120Hz pulse (not including whatever extra time the wave shield causes - that depends on your sound file bitrate).  You might want to slow that down a little.

Also, you are reading the analogue input 8 times and using the last value.  That will be an instantaneous volume, not an average or peak volume.  Remember - sound is a waveform.  You read 8 points along that waveform and use the last.  I would have though sampling rapidly over a short period and calculating a good value to use from that might be better - maybe repeatedly sample for say 10ms (tip: use millis()) and using the maximum value read would be a good start.
1731  Using Arduino / Programming Questions / Re: Arduino does not understand a ranged FOR loop on: May 24, 2013, 07:25:15 pm
"simple" for loops can be quite complex and convoluted.

Not many people use that syntax for a for loop, as it's not as intuitive as putting the assignment (or using the iterator variable directly) inside the loop itself.

While it's perfectly valid, it's mainly there as a demonstration of how things in C aren't always what they appear.  What you think may be missing in C may just be doable in another way - a not very obvious way.

In general though, for things like that, it's better to be more long winded and do it as specific steps within the loop - that way when you come to look at it again in the morning you know what's going on.
1732  Using Arduino / Programming Questions / Re: Arduino does not understand a ranged FOR loop on: May 24, 2013, 07:12:35 pm
Code:
for (size_t i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
You sure you got that comma in the right place? Doesn't look right to me.
Yup, I'm sure.  It's the "comma operator".  It means "do the left side, then do the right side, and return the value from the right side".

So, that assigns array[ i ] to out, then does the comparison, and returns the result of the comparison to the for loop.

Code:
matt@laptop01:~$ cat test.c
#include <stdio.h>

void main()
{
int array[10] = {0,1,2,3,4,5,6,7,8,9};
int out;
size_t i;

for (i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
  {
    printf("%d\n", out);
  }
}
matt@laptop01:~$ cc -o test test.c
matt@laptop01:~$ ./test
0
1
2
3
4
5
6
7
8
9
matt@laptop01:~$
1733  Using Arduino / Programming Questions / Re: please help!!! on: May 24, 2013, 07:07:29 pm
You have conflicting timers.

The Wave shield uses timer1 to drive it.  Timer1 is also used by default in the Servo library.

You can disable Timer1 in the Servo library by editing Servo.h and commenting out all the lines that say:
Code:
#define _useTimer1
There's a number of them for different chips.  Best to do them all then you're sure to get the one for your board.
1734  Using Arduino / Programming Questions / Re: Arduino does not understand a ranged FOR loop on: May 24, 2013, 06:51:08 pm
But you can always do it via:

Code:
int array[10] = {0,1,2,3,4,5,6,7,8,9};

for (size_t i = 0; i < sizeof (array) / sizeof (array[0]); i++)
  {
    int out = array[i];
    Serial.print (out);
  }
Or...

Code:
int array[10] = {0,1,2,3,4,5,6,7,8,9};
int out;

for (size_t i = 0; out = array[i], i < sizeof (array) / sizeof (array[0]); i++)
  {
    Serial.print (out);
  }
1735  Using Arduino / Programming Questions / Re: Need to shrink down this code. on: May 24, 2013, 02:23:06 pm
Quote
is there a better way of doing this other than the if statements I am using or is this pretty much my only option?

You could create an array of values for the RGB values, and assuming the limits are linear, use division and addition/subtraction to convert the temperature into array indexes.
If you learn to count in binary and use the right thresholds you can use shifts instead of division.  If you have your thresholds every 4 or 8 degrees instead of every 5 you can then just add the right offset (the lowest negative) and shift the result 2 or 3 places right to get the array index.  Very efficient and small code.
1736  Using Arduino / Programming Questions / Re: Arduino does not understand a ranged FOR loop on: May 24, 2013, 02:14:05 pm
FIY it's the same as a "foreach" in other languages.
1737  Using Arduino / Programming Questions / Re: Need to shrink down this code. on: May 24, 2013, 02:12:55 pm
Put the thresholds and colour settings in an array stored in PROGMEM, then iterate through it.  If you keep the thresholds in order in the array then you can terminate as soon as the threshold rises too high.
1738  Using Arduino / General Electronics / Re: several wires in one pocket on: May 24, 2013, 09:33:11 am
That's exactly the kind of thing I'd expect from you smiley-razz
1739  Using Arduino / General Electronics / Re: several wires in one pocket on: May 24, 2013, 05:53:28 am
Why do you keep wires in your pocket?

I think you mean "socket".  Socks don't have pockets.
1740  Using Arduino / General Electronics / Re: Best way to solder/make connections between holes on PCB on: May 24, 2013, 05:51:21 am
Mmmm... Components with legs... I remember those...

Pages: 1 ... 114 115 [116] 117 118 ... 265