Show Posts
Pages: 1 2 [3] 4 5 ... 19
31  Using Arduino / Programming Questions / Re: How to change the PWM frequency on ATmega 328p to 16Khz. on: November 07, 2013, 12:48:46 pm
you will need to use timer registers directly and not use analogWrite.
use the code from this post
http://forum.arduino.cc/index.php?topic=196815.msg1454187#msg1454187
set ICR1=500 to get a period of 62.5us (16khz)
set OCR1A to any value from 0 to 500 (you got a 500 resolution pwm).

the code is for mega, I think it should work on 328, but check if pwm pin for timer 1 is same PB5 for 328.


32  Using Arduino / Programming Questions / Re: which is more efficient? using -> or . to access array of struct members on: November 06, 2013, 03:17:35 pm
(which mostly goes to show that if you NEED this level of optimization, you probably NEED to look at the actual code generated, ......
If you need this level of optimization you might look at a faster/heavier processor and just keep the code as maintainable/readable as possible. Optimized code seldom is (from experience).

I know, its harder and harder to fight that, even arduino is jumping on the 32-bit bandwagon. just code unoptimized and use a faster processor. I see that all the time now.

gone are the days when you are challenged to write a program on an IBM punch card to print a 10 digit number in reverse order, and the punch card can only take 8 instructions.

programming skill goes down each generation.
times change.
33  Using Arduino / Programming Questions / Re: output format question - XML? on: November 06, 2013, 02:40:17 pm
\r\n is ok after the closing tag. likewise \t or spaces to indent before opening tag to make it readable.
like
Code:
<a>a</a>\r\n

but \r is not ok between the tags. (with exceptions, but that is getting too technical)

BTW, \r is not just ignored by xml parsers,  it must not be present inside the tags, xml parsers are required to normalize presence of line endings in the infoset (to use the proper w3c spec terminology),
34  Using Arduino / General Electronics / Re: Stranded Wire + Breadboards on: November 06, 2013, 02:30:25 pm
I do see the novelty and coolness to be able to show your buddy you can crimp your own jumper wires. smiley
creativity is still the key, like the suggestion above to just solder a resistor leg to the stranded wire. and do not take that literally, you can be creative and use something in place of the resistor leg. 
35  Using Arduino / General Electronics / Re: Stranded Wire + Breadboards on: November 06, 2013, 01:14:34 pm
seems like an awful lot of trouble (not to mention the cost of the crimper) just to connect servo motor wire to the breadboard.

I have a bunch of these (and alligator clip jumpers)  for use on stranded wires, pcbs, or anything that cannot be connected directly by the standard jumper wire. Or just be creative and use what you got.
36  Using Arduino / Programming Questions / Re: output format question - XML? on: November 06, 2013, 01:04:30 pm
do you know if println generates \r\n ?
if so, you must not use it inside xml tags, as only \n is valid between xml tags.
strictly speaking, all white spaces can be trimmed between the opening xml tag and the first character of the element text, and between the last character of the element text and the closing xml tag.  a white space is defined as  space, \r, \n, \t.
37  Using Arduino / Motors, Mechanics, and Power / Re: Timing problems with short 2...12 us pulses generation with PWM out on: November 05, 2013, 04:34:39 pm
I think for what you need, the simplest solution is to just use mode 0 (normal mode) and just control the pin output high low in the compare match ISR. You have total control of pulse width and frequency.. I don't think the suggestion to use the wave generation mode and setting com1a1 to 0 is not quite right, hence. might as well use normal mode.

if you div clock by 8, one count is 0.5us. to get a 2us pulse at 10hz,
set pin high at count=0
set pin low at count=4 (4*0.5=2us)
set pin high at count =200,000 (since this is higher than 8 or 16 bit, you have to count the overflows)
for 2000hz pulse
set pin high at count = 1000 (again if you are using 8bit timer, you need to count the overflows)
38  Community / Bar Sport / Re: I just got the fastest delivery from China :) on: November 05, 2013, 03:01:52 pm
got another fast delivery. just 3 business days. This item costs 99cents total!

39  Using Arduino / General Electronics / Re: Stranded Wire + Breadboards on: November 05, 2013, 02:38:40 pm
just use some alligator clip or grabber to make your connection. this is just for breadboard prototype right?
40  Using Arduino / Programming Questions / Re: output format question - XML? on: November 05, 2013, 02:24:04 pm
I don't know if there is any library that will format data to xml string.
I just use brute force to add the xml open and closing tag as I build the string.
41  Using Arduino / Programming Questions / Re: creating an averaging function correctly on: November 05, 2013, 01:47:50 pm
Code:
public int mean(int in){
  count++;
  if(count<numberReadings){
   avg += in;
   avg = avg/count;
  }
  if(count >= numberReadings){
    count = 0;
    avg = in;
  }
  return avg;
}

This seems to be trying to average every N samples, starting again for the next N samples. It is not producing a rolling average. Did you intend it to start again every N samples?

In order to calculate an average over a number of samples you need to record the sum and the count. You don't need to record the previous average. Instead of this:

Code:
   avg += in;
   avg = avg/count;

You want:
Code:
   sum += in;

The result is then sum / count.

If what you want is a rolling average you can do that easily using an exponential decaying average, e.g.:

Code:
average = (0.9 * average) + (0.1 * newValue);

the equivalent of that rolling avg code using int operations is
Code:
sum = sum-avg+newvalue
avg=sum/count; //where count=10.

you can set count to whatever number of sample that makes sense for what you are averaging
42  Using Arduino / Programming Questions / Re: programmatically disconnect a pin on: November 05, 2013, 01:39:12 pm
try setting pin mode to input with no pullup.
43  Using Arduino / Programming Questions / Re: Direct Port Manipulation -- analogWrite? on: November 05, 2013, 10:57:29 am
When the OP said it didn't work, I was assuming it meant there was no pwm output. I suppose that could mean it did not compile. The sbi function is in analogWrite source, in order to use it you need to include wiring.h. Another way is to do

Code:
TCCR1A |= _BV(COM1A1);
44  Using Arduino / Programming Questions / Re: Frequent timer interrupts and reading Serial data on: November 04, 2013, 01:55:17 pm
you can define an array of bytes that is volatile. array size depends on what you want the buffer size to be.
you need a head and tail pointer.
the push will add the new value to [head]
the pop will get the value from [tail]
if head==tail, there is no value
make sure head does not overrun tail
increment head/tail after push/pop access, then % to queue size.
wrap the code in loop that push to buffer between cli() and sei()
45  Using Arduino / Programming Questions / Re: which is more efficient? using -> or . to access array of struct members on: November 04, 2013, 12:18:51 pm
So the lesson is, don't try to outsmart the compiler. It knows all the tricks.

what I learned from this is to always use / (if possible), as it can not do any worse than >>, even if avr gcc fixes the 8bit >> in the future.

I imagine it will make some difference if the code uses a ton of these, hence what the multirotor code developer did to globally replace all >> with / makes sense (or at least the code will not perform any worse than before).
Pages: 1 2 [3] 4 5 ... 19