Show Posts
Pages: [1] 2 3 ... 19
1  Using Arduino / Programming Questions / Re: Incorrect calculation for micros() for CPUs running at 20 MHz, 24 MHz etc. on: November 14, 2013, 02:11:27 pm
sorry, but you are looking at this incorrectly.

start from 0, how many times do you increment to get to 255?
you say 256?
I say 255.
at 0, time is 0, after 255 increments, time is 1020 us.

If you really think it is 256, you need to have arduino correct their pwm frequency to 488 instead of saying it is 490.

I  can guarantee you without any doubt that I am right on this one.
and since this thread is about accuracy, I thought I should point out your mistake.
2  Using Arduino / Programming Questions / Re: Incorrect calculation for micros() for CPUs running at 20 MHz, 24 MHz etc. on: November 14, 2013, 01:47:36 pm
But aren't you still out by a small amount?

Running at 16 MHz, the interrupt service routine is called every...
(1/(16000000/(64*256)))*1000 = 1.024 ms

the above is not quite right.
16000000/64 = 250,000
1/250,000=4us per timer count.
but timer counts from 0 to 255 is only 255 increments, not 256, so the time is 1020us to be exact.
The overflow interrupt is called once every 510 count, not 512, since 0 and 255 only occurs once (check the datasheet)
hence one cycle is 2040us, or 490.19608 which is the PWM frequency (otherwise if it is really 2.048, arduino documentation should say the pwm frequency is 488hz).

It took me a while to figure out why I am getting 2.04ms cycle on the oscilloscope when I initially thought it should be 2.048.
3  Using Arduino / Programming Questions / Re: Arduino HTML timer on: November 14, 2013, 01:15:55 pm
I have an implementation that's similar to what you are looking for.

what I do is I run a javascript countdown on the html page, and periodically (like every 5 seconds I think, I can't remember),  the javascript will make a call back to the arduino to get the actual time in order to synchronize the local countdown.

I implement json service on the arduino to provide the countdown value, and it is an ajax call from the html file.

I initially tried to update the countdown in real time, but the network latency is very unpredictable, so I just do a local countdown and just synchronize.

this is a screenshot of the page doing the countdown
4  Using Arduino / Programming Questions / Re: Port manipulation Arduino Uno on: November 14, 2013, 12:28:32 pm
PORTD &= ~(_BV(PD4)|_BV(PD7));
instead of this
PORTD = PORTD & ~B00000000;// set pins 4&7 LOW
5  Using Arduino / Programming Questions / Re: creating an averaging function correctly on: November 12, 2013, 11:37:06 am
from performance point of view
average = ((9 * average) + newValue + 5) / 10;
the divider better be a multiple of 2  (even if average and newvalue are floats)
average = ((7 * average) + newValue + 5) / 8;

indeed better for integer values. However, if the value is a float, I think the method using sum and avg variable is more efficient, as it only involves one addition, one subtraction and one division.
6  Using Arduino / General Electronics / Re: Arduino monitoring Amp flow from a Car 12V Battery to some drvice on: November 11, 2013, 05:00:47 pm
I think there might be a confusion between amps and ampere-hour.
If all you want is to cut power at 10a, just replace the inverter fuse with a 10a fuse. (brings lots of 10a fuse)
7  Using Arduino / Programming Questions / Re: Anyone have a sketch to calculate the day of week based on the date? on: November 09, 2013, 04:21:35 pm
ok, I missed the part where OP said he cannot use time library.

I'd be more curious as to why and address that.

Just the same, the time library code is better, as Paul said, OP can adapt that. Just takes a little creativity. smiley-razz

At first I could not use Time library because I need to call time function now() from that ISR, so I modified the Time library with a few lines change so I have an interrupt friendly now() function. No need to reinvent the wheel.
8  Using Arduino / Programming Questions / Re: Anyone have a sketch to calculate the day of week based on the date? on: November 09, 2013, 03:55:01 pm
+1 on using the macro that comes with Time library.

the macro is defined as (and way simpler than the two examples above)
#define dayOfWeek(_time_)  ((( _time_ / SECS_PER_DAY + 4)  % DAYS_PER_WEEK)+1) // 1 = Sunday

just call
dayOfWeek(now()) to get the day of week for the current day.

why reinvent the wheel?
9  Using Arduino / Networking, Protocols, and Devices / Re: NTP Time Server on: November 09, 2013, 03:48:43 pm
well, there's that, and there is the practicality side of a solution.
time library does not sync itself, you must call the now() function for it to check if it is time to synchronize.
plus like I said, it does not matter if your time source is accurate to 1 billionth of a second if arduino is only accurate to 62 billionth. plus gps is not as accurate as you think, in theory it may be, but not from practical point of view. There is latency for the signal to get from satellite to the gps (ntp introduces network latency as well) , not to mention you are using a serial connection, which itself introduces timing error. Anyone who has ever used a gps for navigation knows how much error there is on location data.  Gps signal can disappear or be blocked, hence it is not 100% reliable as well.  Just understand its limitations so as not to fall into a false sense of security you are synchronized with atomic clock in fort collins.
the fastest sync is still via rtc. Connecting to ntp server on the internet adds a lag of several seconds (I've see it hang up to 10 seconds). That's why it is not practical to use it (and gps) as a main sync source. Hence you cannot sync to ntp or gps as often as you like. You will find all these out when you actually build your project.
10  Using Arduino / Networking, Protocols, and Devices / Re: Arduino as Web Server: visibility to outside world? on: November 09, 2013, 03:30:15 pm
the webcam video feed is from a foscam ip camera, but integrated into the web page.
if you buy a foscam, you get a dns service from foscam that works similar to dyndns, so thats what I use. I do not have a static ip per se, but my dynamic ip has been the same for the last 10 years. As long as you do not disconnect your modem (cable or dsl) too long, you will most likely get the same dynamic ip address from your isp.
11  Using Arduino / Networking, Protocols, and Devices / Re: Arduino as Web Server: visibility to outside world? on: November 09, 2013, 03:07:24 pm
you need to configure port forwarding on your router. Just google it, there are tons of instructions for it, there might even be one specifically for your router model.

Here is my webserver.
I use (modified) TinyWebserver library and wrote an html5/javascript program using jquery heavily for a mobile web interface to my arduino based aquarium controller. I access it via my smart phone. This is how it looks (everything is served out of the arduino, no external service used)


12  Using Arduino / Networking, Protocols, and Devices / Re: NTP Time Server on: November 09, 2013, 02:54:02 pm
or half price on ebay

 if accuracy is your concern, then syncing time every 8 hours is defeating the whole objective. A better solution perhaps is a combination gps + rtc. you can actually sync the rtc to gps once every 8 hours, and rtc to sync the arduino time every 5 minutes.
13  Using Arduino / Networking, Protocols, and Devices / Re: NTP Time Server on: November 09, 2013, 01:53:13 pm
And the gps needs to be outside for it to lock to the satellites.

GPS always gives accurate UTC time.  RTCs will always drift.

The ublox and MT3329 chipset GPS receivers are very sensitive and work very well inside a house. 

you can get more accurate RTC chips. It is still a  way simpler (and faster) solution to using GPS if all you need is time.  If you want accuracy, the time sync of 8 hours is not sufficient, that is why the default is 5 minutes. I would not count on the gps working on all indoor location. I know when I first setup my neo6 gps while indoors (for a quadcopter I am working on), it took several hours before it got a 3D lock (I actually was not expecting it to),. even outdoors it takes up to 10 minutes on a cold boot. so yes, it will work indoors, but...
14  Using Arduino / Programming Questions / Re: How to change the PWM frequency on ATmega 328p to 16Khz. on: November 09, 2013, 01:18:39 pm
Hi, thanks for the info.  It is honestly more than I can comprehend.  For me to try and understand any part of the datasheet, would be equivalent to opening a book written in a foreign language and expect to be able to read it.  I say this because I have looked at it before.  I am not afraid or unwilling to learn something, quite the contrary.  I am just realistic about my background level regarding these things.  I cannot ask the datasheet to explain itself!

So any chance this can be "dumbed" down even more, I know it is asking a lot?  Otherwise, I do indeed appreciate your efforts nonetheless!


don't sell yourself short, it's really easier than it looks. I started in the same situation you are.

just think of it this way.

you start with a 16mhz clock.

you have a timer (8 or 16 bit). which will increment once per clock cycle.

so say you want 16000 hz, that is 1/16= 0.0625ms or 62.5us.

one cycle of the pwm is when counter starts from 0, counts to top (255 for 8 bit, 65536 for 16 bit, or can be user specified), then back down to 0.

so you need to divide 62.5us by either 512 (256*2 due to upcount then downcount), 65536*2, or a custom value.

62.5/512=0.122us. per timer increment using 8 bit.

now the arduino clock of 16mhz can be divided by 1,8,64,256,1024.
if we use divided by 1, 1/16 = 0.0625us, which is too fast to get 16khz
if divided by 8. 16mhz/8=2mhz, 1/2 = 0.5us per timer increment.
we cannot get the exact 16khz freq using the standard 8bit or 16bit top, so we must use a custom counter top (top is the number where the timer switch direction and counts downwards)

lets use the divided by 1 value of 0.0625us per count.
so we divide 62.5/0.0625 =1000
this means if timer increments once every 0.0625us, after 1000 increments/decrements, 62.5us has elapsed, exactly the time period for 16khz!  Since we need 1000 count, 8 bit timers are out.

since the counter counts up then down, divided 1000 by2 to get the 500 for TOP.

Now you got all the values, just figure where in the timer registers to set these.
TCCR1A, TCCR1B for setting the clock divider, wave generation mode
ICR1 for the TOP
OCR1A for the duty cycle.

wave generation mode 10 is whats needed here. it clears the pin low on  match to OCR1A value on counting up, and sets the pin high on OCR1A match on the way down.

say you set OCR1A to 125.
counter start from 0, count to 500, back down to 0.
lets start from the top, from 500, the pin is low. when it decrements down to 125, it will set the pin high, once it hits 0, it start incrementing, and when it reaches 125, it set the pin low.

so the pin is high from 125 to 0 to 125 for a total of 250 clock cycles out of 1000 or exactly 25% duty cycle.

all these info can be found in atmega328 datasheet chapter 15. study section 15.11 carefully for the register description. and you can see in my code how I arrived at the values to set the registers to.

you can use that pwm library, but it won't hurt to understand how this works.
15  Using Arduino / Storage / Re: Using Lots of Data From SD Card on: November 08, 2013, 12:44:36 pm
unless you are mass producing, you will not save anything using atmega328 chip and building your circuit. you can get an arduino pro mini now for $3.45 and can use the full 32kb program space if you program via icsp.

anyway, if you are doing animation, is that at 15 frames per second? or probably slower right? at 15 frames, that gives you 66ms to setup the data and send it out.did you try this at all yet? sd read should take a shorter time that sd write. worse case you can use timer interrupt code to control your led line, reading from a circular buffer containing the frame data, and the sd card read will be in the main loop just continuously filling up the buffer.
Pages: [1] 2 3 ... 19