Show Posts
Pages: 1 [2] 3 4 ... 7
16  Forum 2005-2010 (read only) / Syntax & Programs / Re: Thermistor readings... use lookup table? on: July 17, 2007, 10:14:59 am
In the past when I've done this I've use the lookup table approach.

To create the lookup table I graphed the equation. and developed a set of control points that broke the curve into line segments that kept the error below my minimum threshold.  This method works well and has the virtue of keeping the lookup table size small and only requiring linear interperlation in between points.
17  Forum 2005-2010 (read only) / Syntax & Programs / Re: Simple Digital I/O Program and Optimization on: July 18, 2007, 12:15:42 pm
When I was having some issues with speed I ran the following sketch
void loop()
   digitalWrite(pin3, HIGH);
   digitalWrite(pin3, LOW);

I checked the output on pin3 with and oscilliscope and determined that the digitalWrite function (in v7) took about 9us.  Since the waveform was close to a 50% duty cycle it didn't appear that the overhead for the loop function was impacting performance much.  If it was, in the above example, the low portion of the wave would have been longer then the high.  On a tek 2225? analog scope I couldn't see much difference when the timebase was set to 2us (i think).

A similar test might help identify/quantify the issue/problem for you.  Also if you are using the new v8 then it is possible that something about the new version might have altered the above behavior.

I think I remember someone mentioning that the digitalWrite function was going to be sped up.  If this was the case it is certainly possible that the reduction in time for digitalWrite would mean that the loop overhead could have a greater impact percentage wise when dealing with such simple loops.
18  Forum 2005-2010 (read only) / Syntax & Programs / Re: Simple Digital I/O Program and Optimization on: July 18, 2007, 10:51:04 am
The loop() function never exits.   Its not supposed to.  Unlike programming for a desktop,  microcontroller applications are typically run as endless loops.

The only reason to use the while instead of the if statement is if you want the led to function as an indicator of how low the signal on pin 8 is active.
19  Forum 2005-2010 (read only) / Syntax & Programs / Re: aborting a loop on: June 14, 2007, 11:05:25 am
I would follow woo_oow's suggested approach to your issue; however, if you are ever in a situation where you need to exit a loop there is a c/c++ statement that performs this function 'break'

Basically it will execute the innermost (in the case of nested loops) loop structure that contains the break statement.  The reference page for the arduino has a good description.
20  Forum 2005-2010 (read only) / Syntax & Programs / Re: Converting a char into an int on: June 18, 2007, 04:40:31 pm
What do you mean by a character array?

Do you mean

char serialNum[] = { '1', '2', '3', '4', '5' }


char serialNum[] = "123456";

If you mean something like the first, then that would be the source of the problem with the atoi function.  This function expects to receive a 'c' language string which has a '\0' as the last character of the array.  If you were to look at the memory map for the second version you would see this terminating null.

value = atoi(serialNum)  should work for the second definition of serialNum.

If the first definition was modified to:

char serialNum[] = { '1', '2', '3', '4', '5', '\0' }

It would work also.
21  Forum 2005-2010 (read only) / Syntax & Programs / Re: division problem on: June 06, 2007, 04:24:13 pm
The overflow will only occur if the result is too large for the output.  For instance if the result was 1024 and you were trying to store it in a byte or 70000 and trying to store in an integer.

The interesting problem is your statement that when you tried floats you got an error like:

error: float variable doesn't work with Arithmetic function..

Since the division operator should work fine with floating point values I am inclined to think there is something in another part of your code that is causing the problem.  If you can post the whole code as it stands it would help.  From what you've shown I don't see anything that could be causing the problem.  The division operator probably isn't the source of your problem.

22  Forum 2005-2010 (read only) / Syntax & Programs / Re: division problem on: June 06, 2007, 08:35:18 am
Are you sure that the encoder0Pos value is 1050 when you calc is returning 62

Not knowing what else is in the code I don't see anything wrong.  The division should work fine for the value range you specified.  62 would indicate that the encoder0Pos is between 992 and 1007.
23  Forum 2005-2010 (read only) / Syntax & Programs / Re: read specific bits of a byte? on: June 05, 2007, 01:34:17 pm
I would use something like the following to test a given bit specified by n where 0 <= n <= 7

value = (keys >> n) & 1;

With this expression value will be either 0 or 1 depending upon what the bit contained.
24  Forum 2005-2010 (read only) / Syntax & Programs / Re: Way to wrap long string? on: May 29, 2007, 11:34:10 am
The only way I can think to do this in C/C++ is to physically break the strings and use the strcat function to recombine them.

I don't think there is a line continuation syntax within the C language definition that would help.
25  Forum 2005-2010 (read only) / Syntax & Programs / Re: can the arduino generate frequencies accuratel on: May 22, 2007, 06:47:07 pm
If you are looking for a sine wave rather than a square wave you can run the output through a RC filter to get a good approximation.  This would allow you to use the code as is.  Even if you use some kind of D2A conversion to generate a sine wave you might want some filtering to smooth out the stair steps.
26  Forum 2005-2010 (read only) / Syntax & Programs / Re: read multiple pins at once? on: May 22, 2007, 01:25:55 pm
In your code below above with the change for digital pin 11  there are a couple of problems.  First d11 is on PORTB not PORTD.  If you look at the pin reference diagram it will show which arduino pins map to which ports.

Also there is a more generic way to write c/c++ code for setting/resetting individual pins.  Here is an example for arduino d11 which is the same as pin PB3

PORTB &= ~(1<<3);  // Turns on (high) arduino pin d11
PORTB |= (1<<3);     // Turns of (low) arduino pin d11

The advantage to the above syntax is that it doesn't alter any of the other pins on the port and the 3 can easily be replaced with another pin number or even a variable.
27  Forum 2005-2010 (read only) / Syntax & Programs / Re: read multiple pins at once? on: May 22, 2007, 01:15:38 pm
The direct reading and writing of pins/ports is much faster than using the digitalRead/Write functions.  I was testing some ISR code yesterday and was using a digitalWrite function within it and tested it to be taking a 9uS for the digital write.  This is roughly equivalent to 144 avr instructions.  The direct read may only be one instruction (1/16,000,000) but could be as many as five or six depending upon what the compiler outputs.
28  Forum 2005-2010 (read only) / Syntax & Programs / Re: read multiple pins at once? on: May 22, 2007, 11:25:42 am
The best reference is the datasheets for either the mega8 or mega168 whichever flavor your using.  Those can be a bear to read through but they are the best source for how the chips work and what things are called.

Another helpful source is the header files used by gcc.  If you are using a mega8 I suggest keeping a printout of iom8.h, if using mega168 than it would be iomx8.h  These files define the ports, pins, and most other names that are useful.

They can be located in arduino-0007\tools\avr\avr\include

Hope this helps.

29  Forum 2005-2010 (read only) / Syntax & Programs / Re: read multiple pins at once? on: May 22, 2007, 08:02:20 am
jims has given you the specific answer you requested but you might want to check out the bit math tutorial in the playground (  it provides a little more background if you need it.
30  Forum 2005-2010 (read only) / Syntax & Programs / Re: Persistence Of Vision  (POV) software on: May 08, 2007, 07:13:11 pm
I was having trouble getting it to work as well.  I checked the signals with the an oscilliscope and nothing.

After scratching my head I finally figured out that the problem (for me) was that the IDE chose the wrong chip ATmega8 versus ATmega168
Pages: 1 [2] 3 4 ... 7