Show Posts
Pages: [1] 2 3 ... 27
1  Using Arduino / Programming Questions / Re: Enable Interrupt on: September 22, 2014, 07:51:37 am
if you meant this way , it doesn't work .
Code:
PCICR |= (1<<PCIE1);
PCMSK1 |= (1<<PCINT9);

I think it does work.  I think that this doesn't work:
Quote
Code:
ISR (PCINT9_vect)

From the ATMega2560 datasheet, Section 15:
Quote
The Pin change interrupt PCI2 will trigger if any enabled PCINT23:16 pin toggles, Pin change interrupt PCI1 if any enabled PCINT15:8 toggles and Pin change interrupts PCI0 will trigger if any enabled PCINT7:0 pin toggles.
(Emphasis mine.)  There's no PCINT9 interrupt.  The interrupt is PCINT1, and it triggers when any pin on PORTJ. whose pin change interrupt has been enabled, changes.  The interrupt fires when any enabled pin changes.  If more than one are enabled, your code has to figure out which one changed.  I don't see that there's a flag register associated with individual pins, so I presume that the code has to remember the last state of the pins, and compare it to the current state, to learn which pin changed.  I hope there's a better way, and that our friends on this forum will describe it.

As Nick says, IDE 1.0.5 silently discards an ISR whose name doesn't exactly match its list of interrupts.  If you turn on verbose output during compilation, you'll see a warning that something "appears to be a misspelled signal handler."  As I understand it, the correct names are the names found in table 14.1 of the datasheet, with underscore characters substituted for spaces, and with the characters "_vect" attached at the end.
2  Using Arduino / Programming Questions / Re: Enable Interrupt on: September 21, 2014, 10:32:46 pm
From the ATMega2560 datasheet, accessible from the arduino.cc product page for the Arduino Meg ADK:
Quote
15.2.5
PCICR – Pin Change Interrupt Control Register
• Bit 1 – PCIE1: Pin Change Interrupt Enable 1
When the PCIE1 bit is set (one) and the I-bit in the Status Register (SREG) is set (one), pin
change interrupt 1 is enabled. Any change on any enabled PCINT15:8 pin will cause an inter-
rupt. The corresponding interrupt of Pin Change Interrupt Request is executed from the PCI1
Interrupt Vector. PCINT15:8 pins are enabled individually by the PCMSK1 Register.
That suggests that the way to enable PCINT[15:8] is to set PCIE1 in register PCICR.  Section 15.2.8 in the same document suggests that the way to enable PCINT9 is to set PCINT9 in register PCMSK1.

Note that I've never used an ATMega2560, so my suggestions are untested.
3  Using Arduino / Installation & Troubleshooting / Re: Disconnecting USB causes IDE to use 100% CPU on Linux on: September 20, 2014, 11:25:58 pm
Confirmed.  Ubuntu 14.04, 64-bit, fully updated.  Java implementation: openjdk-7-jre.  Dell Latitude E4310.  hardinfo reports the OS as "Ubuntu 14.04.1 LTS," and the processor as "4X Inter(R) Core(TM) i5 CPU M560 2.67 GHz."  Arduino IDE is 1.0.5.

Here are the first few lines from the "top" command, after unplugging the USB:
Code:
top - 22:53:28 up 1 day,  1:54,  2 users,  load average: 0.57, 0.52, 0.42
Tasks: 236 total,   1 running, 235 sleeping,   0 stopped,   0 zombie.1
%Cpu(s): 13.6 us, 16.1 sy,  0.0 ni, 69.7 id,  0.5 wa,  0.0 hi,  0.2 si,  0.0 st
KiB Mem:   7969868 total,  6140968 used,  1828900 free,   299712 buffers
KiB Swap: 16777212 total,        0 used, 16777212 free.  3037524 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S  %CPU %MEM     TIME+ COMMAND     
 3136 tom       20   0 4794744 169792  27576 S 100.1  2.1  39:00.62 java       

If I use top's interactive command, "I," "Irix mode," to turn "Irix mode" to OFF, it shows this, again, after unplugging the USB:
Code:
top - 23:07:52 up 1 day,  2:08,  2 users,  load average: 1.18, 1.17, 0.90
Tasks: 238 total,   1 running, 237 sleeping,   0 stopped,   0 zombie
%Cpu(s): 18.5 us, 14.7 sy,  0.2 ni, 66.1 id,  0.5 wa,  0.0 hi,  0.0 si,  0.0 st
KiB Mem:   7969868 total,  6201904 used,  1767964 free,   300460 buffers
KiB Swap: 16777212 total,        0 used, 16777212 free.  3045604 cached Mem

  PID USER      PR  NI    VIRT    RES    SHR S %CPU %MEM     TIME+ COMMAND     
 3136 tom       20   0 4794744 171392  27576 S 25.0  2.2  53:25.37 java         
The man page for top says that when "Irix mode" is OFF, %CPU is divided by the total number of processors.  With "Irix mode" set to OFF, the %CPU reported for java matches the output of the system monitor.

Plugging the Arduino back onto the USB port didn't change anything.  Closing the serial port sent java to a %CPU of 0.1 to 0.9.  That seems to reflect the info posted by tf68.
4  Using Arduino / Audio / Re: [noob inside]read a sound, then play a sound on: September 20, 2014, 09:39:07 pm
The Due has a digital-to-analog converter on board.  If you use that, instead of pulse-width modulation, you'll get much better sound without the necessity of a sharp cutoff filter.  In that sense, the Due does some of the things you need,  "automagically."

Here's a tutorial describing how to drive a speaker from a Due DAC output:  http://arduino.cc/en/Tutorial/SimpleAudioPlayer.  But don't hook it up just yet.  Note that the tutorial has this line:
Quote
For a simple test you can attach a pair of headphones directly to ground and DAC0
I'd recommend that you don't do that.  The Due's DAC outputs seem to be fragile, and headphones are generally "poorly characterized" when it comes to impedance.  Also, the maximum allowable load current for the DACs isn't specified in the SAM3X/A datasheet.  The SAM3S datasheet describes the minimum load resistance as 5,000 ohms, as discussed here:  http://forum.arduino.cc/index.php?topic=139733.msg1134372#msg1134372.  That's a lot higher than a typical set of headphones.  So, I recommend against testing the DACs by directly connecting a set of headphones.  Other posts in that thread describes the mysterious failure of a DAC0 output, suggesting that the output is a bit finicky.

Note, too, that the Due's DAC output ranges from a low of about 0.55V to a high of about 2.75V.  That won't affect operation with the DAC output AC-coupled.  A lot of people have been surprised by it, though.
5  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 18, 2014, 09:04:53 am
I feel a strange inclination to abandon this thread.  

Must not descend into name-calling; I'll quote the Bard.  Interested persons can check the reference.  Regarding some of the histrionic, silly things that have been said here:
Omg, unbelievable ... YOU ARE OFFTOPIC. ... NOW! ... FOCUS ON THIS? ... tell us you dont know what is happening ...

"... full of sound and fury, signifying nothing."  Macbeth, Act 5, Scene 5
6  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 17, 2014, 11:06:27 pm
Maybe "ERW" is "Enhanced Release for Windows:"  http://forum.arduino.cc/index.php?topic=118440.0.
I get the feeling that the OP isn't really trying.
7  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 17, 2014, 09:48:57 am
The original post suggests that you're running some sort of code, and that it's not giving you the results you expect.  If that's so, please post the code that you're using.

Subsequent posts suggest that you're asking a theoretical question about some example code you found somewhere.  If that's so, please tell us that, and post a link to the source of that example code.
8  Using Arduino / Audio / Re: Frequency Detection on: September 17, 2014, 07:41:48 am
Indeed. 

I'm thinking that this line
Code:
      period = i;
would work better if it said
Code:
      period = i - 1;
Variable i is the lag for the calculation that follows the local maximum, rather than the lag of the local maximum itself.  At that point, I think that a better estimate of the period is the lag of the previous calculation.  Tests with simple sine functions  as input wield better results, within the one-sample-time resolution of the calculation, using i - 1.

Here's the code I used:
Code:
//    FILE: frequencyDSP.ino
//  AUTHOR: rob tillaart (org version akellyirl)
// VERSION: 0.1.03
// PURPOSE: frequency analysis using DSP
//    DATE: 2013-10-27
//     URL: http://www.instructables.com/id/Reliable-Frequency-Detection-Using-DSP-Techniques

char rawData[970]; // CHANGE: deleted sample.h, added this declaration

void setup()
{
  Serial.begin(115200);
  Serial.println("FrequencyDSP");
  Serial.println("------------");

  load(220.5);  // CHANGE: added this line

  uint32_t start = millis();
  float fr = autoCorrelateFrequency(rawData, sizeof(rawData), 22050);
  uint32_t stop = millis();

  Serial.print("freq:\t");
  Serial.println(fr);
  Serial.print("time:\t");
  Serial.println(stop - start);
}

void loop()
{
}

float autoCorrelateFrequency(char * sample, int len, float sampleFreq)
{
  long sum = 0;
  long sum_old = 0;
  int thresh = 0;
  byte pd_state = 0;
  int period = 0;  // 0 results in inf

  // Autocorrelation
  for (int i=0; i < len && (pd_state != 3); i++)
  {
    sum_old = sum;
    sum = 0;

    for (int k=0; k <len-i; k++)
    {
      sum += (rawData[k]) * (rawData[k+i]);
    }
    sum /= 256;

    // Peak Detect State Machine
    // 0: initial, set threshold
    // 1: detect positive slope
    // 2: detect negative slope
    // 3: peak found
    if (pd_state == 0)
    {
      thresh = sum / 2;
      pd_state = 1;
    }
    else if (pd_state == 1 && (sum > thresh) && (sum - sum_old) > 0)
    {
      pd_state = 2;
    }
    else if (pd_state == 2 && (sum - sum_old) <= 0)
    {
      period = i-1;  // CHANGE: revised this line
      pd_state = 3;
    }
  }

  return sampleFreq/period;
}

// CHANGE: Added this function
void load(float f) {
  const float pi = 3.14159265359;
  for (int i=0; i<sizeof(rawData);i++) {
    rawData[i] = round(127*sin(2*pi*f*i/22050));
  }
}
The test frequency of 220.5 is admittedly a bit contrived.  It's selected to get a precise result with the one-sample resolution of the calculation.
9  Using Arduino / Programming Questions / Re: Send integer get 256. Arduino I2C comunication on: September 16, 2014, 10:39:59 pm
"SLAVE" code doesn't compile.  Is this really the code you're using?
10  Using Arduino / Project Guidance / Re: Help with serial input on: September 15, 2014, 11:40:30 am
You'll get better results if you follow these guidelines about posting your code:
  • Post code.  You did that; good job.
  • Post a complete sketch.  I should be able to copy your code into the IDE, and have it compile.  This code is a snippet - part of a program that won't compile without some additional lines.  If you post a complete sketch, a reader can copy, paste and try it without a whole lot of effort.  If you don't, at least some readers will skip to the next post, and won't respond to yours.
  • Post the smallest sketch you can come up with that illustrates the problem you've encountered.  It's important to post a complete sketch, but it's also important to isolate the issue to a small, easily readable sketch.  If the code is too long and complex, a reader may again skip to the next post.  Your snippet, and the code that I can extrapolate around it, isn't long or complex.  But, if you stay with this activity long enough, one day it will be, and you'll get more and better answers if you're willing to isolate the issue to a small sketch.
  • Post your code in [ code ] tags.  Here's a description of how to do that:  http://forum.arduino.cc/index.php/topic,148850.0.html.  See item #7.  As a new poster to this forum, you'd do well to read the whole thing.  This information is accessible from first sticky post, at the top of the first page of any section of the forum.  As for code tags, they tell the server's software to display each character exactly as it is, rather than interpret some strings of characters as special characters, or display instructions.  That ensures that your code shows up correctly, and isn't garbled.
Some of that has already been said in this thread.  I think that repeating it won't hurt anything.  Please note that, for any of these guidelines that you might not have followed in your initial post, no apologies are necessary, expected, or even desired.

Looking at your code, here's what I see right away:  something is peculiar about this line:
Code:
  float temp;, c;
It looks like there's an extra character in the declaration.  That may be an artifact of not using code tags, but intuitively it looks like real code.  That line gives a compiler error, leading me to believe that this code wasn't copied directly from a sketch that compiled and ran.  It's important that the code you post is precisely the code you used; otherwise, everybody wastes time solving problems that don't exist in your actual sketch.

Looking further, I don't see that variable c ever gets a value.  Yet, it's used in this calculation:
Code:
f= (c*9)/(5)+32;                                //do math
Did you meant to reference variable temp in this calculation, rather than c?

I'll comment on the comment in this line:
Code:
float f ;                                                // Declared and Initialized
Variable f is local to loop().  Local variables aren't initialized when they're declared.  They have a value that depends on whatever was already in their memory location - it could be anything.  The comment doesn't affect your sketch, but it suggests that you expect these local variables to be initialized, and they aren't. 

Finally, I'll note that Serial.parseFloat() takes characters that make sense as a floating point number - digits, decimal point, minus sign, and plus sign.  It leaves any other character it finds in the buffer.  If your serial monitor is set up to send a newline or carriage return, then the next time loop executes it will find that character in the buffer, try to parse a float from it, and return a zero.  I would expect that you'd see the program print a "32" as a second line for each entry.  You can set the serial monitor to send nothing after the input, and that will work.  Or, you can use Serial.peek() to look at the buffer, and then read characters off the buffer until the first character is something that makes sense as a float.
11  Using Arduino / General Electronics / Re: 555 resistor value for Tm = Ts on: September 14, 2014, 09:42:32 pm
TI's datasheet for the 555 describes how to get a 50% duty cycle.  See this link, page 12:  http://www.ti.com/lit/ds/symlink/lm555.pdf.  If a precise 50% duty cycle matters, you'll be better off running the 555 at twice the frequency you need, and clocking an external flip-flop with the output.  Otherwise, the formula in the datasheet will get you close.
12  Using Arduino / Programming Questions / Re: Convert two integers to a long on: September 14, 2014, 05:35:04 pm
Your subject sounds like you want to perform some kind of operation on two integers, with a long result.  The content of your post sounds like you want to convert a single integer to a single long.

Can you tell us, specifically, what you really want to do?
13  Using Arduino / General Electronics / Re: Running batteries in parallel and still getting power with negative disconnected on: September 10, 2014, 09:24:51 am
Yes, a schematic, and maybe a picture of the batteries and the conductors connected to them, too.  And tell us exactly why you think that you "still getting power."  If you're measuring something, tells us exactly where you're measuring, and exactly what the readings are.

Our impression is that you believe that power flows to the load with the negative terminal of the paralleled batteries disconnected from the load.  That may not be what you intend to report.  It may be that you're measuring the current at one of the batteries' negative terminals.  If that's so, then you're measuring current flowing between the batteries.  Not unexpectable; it's almost certain that their voltages aren't precisely equal, and, with the very low impedances involved, a little bit of voltage difference will give rise to a noticeable current.
14  Using Arduino / Project Guidance / Re: rms Voltage on: September 08, 2014, 12:24:41 am
Code:
(unsigned long)( adc_raw * adc_raw)
does not give the same result as
Code:
(unsigned long) adc_raw * (unsigned long)adc_raw
I don't know why.  It just doesn't on the uno. 
It's because the Uno uses 16-bit integers, and the values of adc_raw are bigger than 8 bits, so their product overflows an integer calculation on the Uno.  adc_raw  is type int, so the calculation is dutifully carried out with integer math. When the result is bigger than 16 bits, the calculation overflows and information is lost.  The reason that it doesn't work on the Uno, and might on some other platforms, is that Uno integers are 16 bits, and they're bigger on some other machines.

I'd prefer to cast adc_raw as long, since it clearly takes on negative values.  The calculation yields the same result either way, on account of the miracle of two's complement arithmetic, but I'm more comfortable closer to the mathematical realities of the problem.

The other thing that rankles about the original program is the nominal sample rate at 1 per 100 us.  A standard analogRead() takes a bit longer than that, so the timing will always be off.
15  Using Arduino / Programming Questions / Re: Problems using Adafruit RGB LCD Shield on: September 07, 2014, 11:17:11 pm
OK, when I try your code, as posted in the original post, with the library properly installed, it compiles without complaint.  So, I think that there's some quirk in the way the library is installed on your system.  Mine has all of the *.h and *.cpp files, and keywords.txt.  That's this list:
  • Adafruit_MCP23017.cpp
  • Adafruit_MCP23017.h
  • Adafruit_RGBLCDShield.cpp
  • Adafruit_RGBLCDShield.h
  • keywords.txt
That's everything except the example, the ReadMe file, which seems to be some kind of an advertisement, and the license file.  To install the library, I followed the instructions, quite precisely, that are linked from Adafruit's product page for installing libraries.  I'm using Ubuntu 14.04.
Pages: [1] 2 3 ... 27