Show Posts
Pages: 1 2 3 [4] 5 6 ... 22
46  Using Arduino / Programming Questions / Re: How to Use Timer/Counter/Prescaler ??? on: January 30, 2014, 09:09:14 pm
You can find information about how the timers work in the datasheet for the ATmega2560.  There's a link to an English version on the Arduino Mega 2560 product page, here:  It may be available in other languages at the Atmel website.

If you compare the timer settings from this code to the information about Timer1 in the datasheet, you'll see that two important items aren't established the way you want them:
  • The timer is set to mode 0, "Normal" mode, which counts to 0xFFFF, rolls over to zero, and counts all the way to 0xFFFF again.  That will roll over at a frequency of about Hz.  There's no way to control the frequency in the Normal mode.
  • Nothing in the code sets anything that can affect the rollover frequency of the timer. 

You will want to look at the datasheet, read about modes of operation for this timer, select a mode that lets you set the frequency.  You'll want to change your code to set the timer mode, and add code to set whichever register you want to use for the timer rollover value.
47  Using Arduino / Audio / Re: Arduino FFT code not expected results... on: January 29, 2014, 11:54:58 pm
It is very unlikely that anyone on the forum will have the time and/or patience to go through your code line by line ...
And I didn't.  I notice, though, that there's nothing in the code that performs complex arithmetic, and the results of the butterflies are, in general, complex numbers.  It looks like the twiddle factors are complex - the sine and cosine terms are kept separate, at least - but there's no complex multiplication implemented, and all the results are added together as real numbers. 

I don't see anything that suggests that you understand the notion of a complex number.  Try these articles:
If the mathematical concepts in those articles aren't immediately familiar, I'd recommend that you step back and work on understanding the math behind the FFT.  It starts with those concepts.  If that's not somewhere you want to go, then I'd recommend sticking with FFT programs and libraries written by others.
48  Using Arduino / Project Guidance / Re: To find time difference between two states on: January 28, 2014, 11:10:22 am
I'm not sure what you want to accomplish.  But, I think you want to decide what the program does based on whether some particular amount of time has elapsed.

Look at the "Blink without Delay" sketch in the examples folder that came with your Arduino IDE.  The path, relative to the IDE's folder, is
.  I'll recommend that you read that code, load it, test it, and play with it until you understand it.  Then, the code that will manage the timing will be obvious to you.
49  Using Arduino / Programming Questions / Re: Compile two sketches on: January 28, 2014, 09:52:39 am
Read Grumpy Mike's tutorial on merging code:
50  Using Arduino / Audio / Re: Sound gets distored when going thru digital pot on: January 28, 2014, 07:06:53 am
The part requires that the voltage on all three potentiometer pins be between VSS and VDD.  I see the voltage on the upper connection to the potentiometer going negative; it the potentiometer were linear under those conditions, the wiper would be negative, too.

I like the circuit in the revised schematic, attached.
51  Using Arduino / Audio / Re: Sound gets distored when going thru digital pot on: January 28, 2014, 01:22:58 am
Maybe I'm missing something, but I see a problem with the voltage divider used for input voltage offset in this design.

Here's my take on the voltage divider:  The digital potentiometer's internal resistor - 10K, from the original post - is in parallel with the lower half of the voltage divider.  That makes the divider's DC voltage a lot lower than half of VDD, since the upper resistor is 100K, and the lower resistor figures to be about 9.1K.  That'll be OK as long as the input signal amplitude doesn't go above about .42V peak-to-ground.  If the signal gets bigger than that, the input to the digital potentiometer will be below VSS, outside the operating range.

I think that 10K connected between VDD and the input to the digital potentiometer, and no resistor connected between the potentiometer input and ground, will give a DC voltage of 2.5V.
52  Using Arduino / Programming Questions / Re: Converting char value from to integer values on: January 27, 2014, 11:44:06 am
You have a lot of good suggestions already.  I'll add one:

Write a program that collects characters from the serial monitor, identifies the terminating character, does atoi() on the result, and prints the resulting integer.  When you get that working, transfer the method to your web-based program.  

Here's what I like about that method:
  • The code will be short.  You can post the whole sketch each time, and we won't have to work with snippets, or wonder if you made some other change in the program that isn't shown.
  • There won't be all that distracting other stuff.
  • You'll be in control of the input.  You'll know what you put in, and you'll see what you get out.  You, and we, won't have to speculate about  what some server is sending you.

[Edit: Added omitted word, "you."]
53  Using Arduino / Programming Questions / Re: Converting char value from to integer values on: January 26, 2014, 10:30:51 pm
Are you getting these values as text, or as binary integers?  Your code has comments that suggest it's text.  If that's so, you're not getting 56, you're getting "56".  The first is a numerical value, corresponding to 56 decimal, 38 hexadecimal, and 00111000 binary.  The second is a two-byte string, and the values of those bytes are 53 and 54 decimal.

Investigate.  Change this:
   c =;
to this:
   c =;
    Serial.print(" ");
That'll print each character on its own line, along with its decimal representation.  I suspect that you'll find that you're receiving more than one character, and that those characters represent the number that you're looking for.

atoi() expects a pointer to a character array.  It skips any initial whitespace, and then looks for characters in the range '0' ... '9'.  It takes as many as it finds in a row, and interprets those characters as a numerical value.  If you pass it a character, the compiler complains and gives up, because it can't turn a character into a pointer to a character array.  If you pass it the address of the character, it'll compile, but it will keep looking at characters, and keep converting, until it finds a non-decimal character, or a string-ending zero.  I don't think that's what you want.

I think you'll have to collect the characters into a character array, maybe add a string-terminating zero, and then give the array's pointer to atoi(), in order to get the results you hope for.

[Edit: Fix grammar]
54  Using Arduino / Programming Questions / Re: Converting char value from to integer values on: January 26, 2014, 09:18:59 pm
Can you post a complete sketch? If you don't, you waste time while people ask you to do that. Please post a minimal sketch that demonstrates the problem, as opposed to hundreds of lines of code.

55  Using Arduino / Audio / Re: Sound gets distored when going thru digital pot on: January 26, 2014, 10:31:37 am
Intuitively, I suspect that the digital potentiometer requires that its inputs be between VSS and VDD, while the input signal is bipolar.  When the input signal is lower than VSS, the IC is outside its operating range, and it responds in some non-linear fashion.  I think you hear it most in bassy passages because the bass signal is usually very large compared to higher-frequencies, and the voltage gets further below VSS.

That view is bolstered by this line in the datasheet, found here -, page 5, "AC/DC Characteristics:"
Resistor Terminal Input Voltage Range (Terminals A, B and W)  Min: VSS  Max:VDD
To fix it, you'll have to offset the input signals.  Depending on how your amplifier is coupled, you might have to shift the output signals back to bipolar.
56  Using Arduino / Programming Questions / Re: Shortest Path Algorithm Program Error on: January 24, 2014, 12:17:03 am
If you get past the error, I think you'll find that the compilation will still fail, because this program requires more memory than the Pro Mini has available.   It also exceeds the memory of an Uno.

The "invalid conversion" error arises because the program tries to compare two values whose data types aren't compatible:  an element of pathmatrix, type String, and -1, type int.  There's no reasonable way to compare those two things, so the compiler complains and gives up.

This posted version of this program doesn't even compile.  That suggests that there may be other problems with it - well, there's at least one more problem, it takes too much memory.  I'd suggest that you carefully evaluate whether you need Dijkstra's algorithm for your application, and, if you do, learn how to write it yourself, and look for better examples.
57  Using Arduino / Programming Questions / Re: Timer interrupt and analogRead() on: January 23, 2014, 11:11:34 pm
Timer1 can be used to trigger an analog conversion when the timer overflows.  The TimerOne library makes it easy to set up Timer1's overflow interrupt, but it doesn't do anything with the ADC.  Here's a way to use that feature:, using the TimerOne library:
  • Select the ADC input pin you want to use.  You can do that by writing to register ADMUX, and you'll have to be careful to set the REFS0 and REFS1 bits to select the proper analog reference.  Looking at the code for analogRead, it looks like you can select that pin by executing analogRead() on the pin before you start acquiring data.  It looks like the ADC multiplexer is set in that routine and left alone.  If you leave it alone afterward, it shouldn't change.
  • Execute an analogRead(), to get past the extra-long first conversion of the ADC.  That can be the same analogRead() that sets the ADC input pin, if you like.
  • Create and attach a Timer1 interrupt service routine that doesn't do anything.  That ISR's only purpose will be to reset the Timer1 overflow flag, bit TOV1 in register TIFR1.
  • Create an ISR for the ADC that collects the ADC reading, stores it in a golbal variable, sets a global flag, and exits.  ISR's are best that do the least possible amount of work.
  • Enable the ADC, enable its auto-trigger, enable its interrupt, and select Timer1 overflow as the trigger source.  That'll involve writing to the ADC control and status registers ADCSRA and ADCSRB.
  • I'd recommend waiting for the first ADC conversion to complete, by waiting for the flag from the ADC, and then resetting the flag.  You might even want to wait for it to complete two conversions, to be sure that the Timer has fully reacted to the change of its rollover value, and the ADC has synced with it.
  • in loop(), watch the ADC flag.  When it goes active, reset it, process the ADC data, and wait for the next one.
You'll have to be sure to set the Timer1 rollover time high enough to allow for the ADC conversion to complete, and for the ADC ISR to execute afterward.  The Timer1 interrupt will execute while the ADC conversion is going on, so, unless you make it a complex ISR, it shouldn't interfere with reading the ADC.  You should also allow time for the Timer0 overflow ISR to execute, too, since it may be active when the ADC completes its conversion, and the ADC ISR won't execute until that ISR completes.

You can find out how to do all those things in the datasheet, in the descriptions of Timer1 and the ADC.

An alternative is to refrain from enabling the ADC interrupt, don't provide an ADC ISR, and read the ADC and set the flag in the Timer1 ISR.  You still need to consider the relative timing between the ADC and Timer1, since the ADC will write over the result of the last conversion when the new conversion completes.  You'll have an ADC conversion time to let other ISR's complete, respond to the Timer1 overflow interrupt, and read the ADC; after the conversion time elapses, the previous reading will be gone.

The code that Jack Christensen references in Reply #4 manipulates Timer1 directly, without help from the TimerOne library.  I like that better, since it gives you precise control over the Timer1 functions.  You may prefer to let the TimerOne library handle those details for you.
58  Using Arduino / Programming Questions / Re: Shortest Path Algorithm Program Error on: January 23, 2014, 10:00:06 pm
This test
compares an element of pathmatrix, whose type is String, to the number -1, whose type defaults to int.  I got it to compile by changing that test to this:
I've never used String, so I don't know whether that's valid.

What sort of Arduino are you using for this effort?
59  Using Arduino / Project Guidance / Re: Need a way to detect a voltage drop. on: January 19, 2014, 09:28:01 am
Check the datasheet.  Read about the Analog Comparator, and the interrupt associated with it.  Look at the comparator's speed in the "electrical characteristics" section, and decide if it's fast enough.  If it's not, consider using an external comparator connected to one of the INTX pins.

You don't say anything about what "strictly timed" means in this context.  Maybe you need a response - like an output - within a narrow time window, or maybe you need to know the time that the transition happened quite closely.  If you want to know when something happened, then the Input Capture register associated with Timer1 and the analog comparator will help, since it captures the timer value in hardware, without assistance or response from the program.

... will explain what im working on if needed ...
I'll go out on a limb and say, "needed."  It's hard to tell what will really help you in this application without knowing more about it.
60  Using Arduino / Project Guidance / Re: High current draw, external power supply and grounding guidance on: January 19, 2014, 09:15:10 am
I took the power dissipation of the photo resistors from the spec sheet ... converted to amps and multiplied ...
As has been noted already, the power dissipation you're talking about is from the listing, "Absolute Ratings Maximum," in the datasheet.  That's not how to use the absolute maximum ratings.  They don't describe normal operating conditions for the device.  Instead, they describe the limits at which the device won't be damaged.  I think you intuitively know this:  you saw that the absolute maximum DC voltage is 250V, but you didn't consider operating the sensors at that voltage. 

The onus is on the user to design his circuit so that the absolute maximum ratings aren't exceeded; indeed, the onus is to see that the normally more restrictive "recommended operating conditions" are maintained, but this datasheet doesn't provide that listing.

If you're still concerned about the power requirement of the sensor array, hook up one sensor and resistor and measure the current through them with an ammeter.  You should probably do that test even if you're already convinced.  I wouldn't recommend hooking up a bunch of stuff, against your intuition, on just the say-so of some anonymous guys in an Internet forum.  Please post your results.

I recommend that you bone up on the theory involved in this circuit.  Look up Ohm's law in the Wikipedia.  Google "series resistors" and look for something that explains things understandably.  If you have specific questions, come back to this forum  under "General Electronics."

Finally, asking again:
What are you trying to accomplish?  Please be specific about what your project will do - don't just say something like, "I'm trying to detect light in 48 places."
Pages: 1 2 3 [4] 5 6 ... 22