Show Posts
Pages: 1 ... 124 125 [126] 127 128 ... 287
1876  Using Arduino / Programming Questions / Re: How to declare arrays in another (not main) tab? on: March 17, 2013, 10:20:15 pm
In storage, you have only a limited amount of FLASH memory (see your board specs for details, but the UNO has 2k)

In execution, you have a limited amount of SRAM (again see your board specs for details but the UNO has 32k)

Other way 'round actually, 32K flash program memory, 2K static RAM for data, stack, etc.
1877  Using Arduino / Programming Questions / Tips and Tricks to Optimize Your C Code on: March 17, 2013, 09:54:13 pm
I've been reading this application note from Atmel:
Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers
http://www.atmel.com/Images/doc8453.pdf

It's interesting reading. One of the things I found that I do frequently wasn't obvious to me until I read Tip #2.

I had a few variables that I used both in setup() and in loop() so I declared them as global variables. However, they were not used to communicate values between the two functions. Therefore, they could just as well be declared as automatic (local) variables in each function. This sounds like duplication, but in fact it saves both flash program memory and SRAM. Global variables must go into SRAM, whereas local variables can go on the stack or in registers. In addition to saving SRAM, this also shortens the code (and probably makes it faster as well), as addressing the variables can be done with shorter instructions.

Here's an example I cooked up. Declaring the variables twice, once in setup() and once in loop() results in saving 136 bytes of flash and 6 bytes of SRAM. The more references there are to the variables, the more flash is saved.

Code:
//Arduino 1.0.3, Board = Arduino Uno
//Binary sketch size: 3,252 bytes (of a 32,256 byte maximum)
//Free SRAM = 1819 bytes
//See Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers
//http://www.atmel.com/Images/doc8453.pdf

unsigned long n1;
byte n2, n3;

void setup(void)
{
    Serial.begin(115200);
    delay(2000);
    //just some meaningless assignments and calculations
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println();
    Serial.print("mem=");
    Serial.println(freeMemory());
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
}

void loop(void)
{
    delay(5000);
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println();
    Serial.print("mem=");
    Serial.println(freeMemory());
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
}

extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;

int freeMemory() {
  int free_memory;

  if((int)__brkval == 0)
     free_memory = ((int)&free_memory) - ((int)&__bss_end);
  else
    free_memory = ((int)&free_memory) - ((int)__brkval);

  return free_memory;
}


Code:
//Arduino 1.0.3, Board = Arduino Uno
//Binary sketch size: 3,116 bytes (of a 32,256 byte maximum)
//Free SRAM = 1825 bytes
//See Atmel AVR4027: Tips and Tricks to Optimize Your C Code for 8-bit AVR Microcontrollers
//http://www.atmel.com/Images/doc8453.pdf

void setup(void)
{
    unsigned long n1;
    byte n2, n3;

    Serial.begin(115200);
    delay(2000);
    //just some meaningless assignments and calculations
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println();
    Serial.print("mem=");
    Serial.println(freeMemory());
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
}

void loop(void)
{
    unsigned long n1;
    byte n2, n3;

    delay(5000);
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println();
    Serial.print("mem=");
    Serial.println(freeMemory());
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
    n1 = millis();
    n2 = n1 % 10;
    n3 = n1++;
    Serial.println(n1);
    Serial.println(n2);
    Serial.println(n3);
}

extern unsigned int __bss_end;
extern unsigned int __heap_start;
extern void *__brkval;

int freeMemory() {
  int free_memory;

  if((int)__brkval == 0)
     free_memory = ((int)&free_memory) - ((int)&__bss_end);
  else
    free_memory = ((int)&free_memory) - ((int)__brkval);

  return free_memory;
}



1878  Using Arduino / Sensors / Re: Sensing water? on: March 17, 2013, 02:00:34 pm
Similar recent discussion here:
http://arduino.cc/forum/index.php/topic,152516.0.html
1879  Using Arduino / Programming Questions / Re: xbee module wireless communication on: March 17, 2013, 01:25:06 pm
See my blog post, Simple XBee ZB (Series 2) P2P Communications

Fairly easy IMHO. Indeed XBees are complicated in that there are a lot of settings. I highly recommend spending some time in the Product Manual before hooking them up and blindly fiddling with dozens of settings in X-CTU. In most cases, only a very few settings will actually need changing. In other words, there are lots of ways to do it wrong.

Looks to me like the wrong modem type is selected (XB24-B). It should be XB24-ZB for regular XBee-ZB modules (f.k.a. Series 2). But we really would like to know the Digi part number of the model you are working with (e.g. XB24-Z7WIT-004), so add that to the additional information that is needed.

Also IMHO, if you use even half of the capabilities of these modules, they are a downright bargain. Best price I've found for the low power XBee-ZB modules is $17, either direct from Digi, or from Mouser.
1880  Community / Bar Sport / Re: iMall iTeadStudio PCB Ordering Process? on: March 17, 2013, 06:30:08 am
Ahhh thanks. I didn't read all the instructions.  

Their ordering system is a little primitive and a hack job. We'll see how their service goes.

Things are fairly basic, but service has been good for me.

where do you put the outline? it says one of the layers, what should that layer be called?

most people want it in the drill file

The page I linked above lists the following, but their CAM job also produces a milling file (.GML) which has the outline, so I send that too, eight files in all.

Quote
2.Gerber files needed for 2-layer PCB boards:

    Top layer:    pcbname.GTL
    Bottom layer:    pcbname.GBL
    Solder Stop Mask top:  pcbname.GTS
    Solder Stop Mask Bottom pcbname.GBS
    Silk Top:   pcbname.GTO
    Silk Bottom pcbname.GBO
    NC Drill: pcbname.TXT
1881  Community / Bar Sport / Re: iMall iTeadStudio PCB Ordering Process? on: March 16, 2013, 06:39:05 pm
Just sent one this afternoon myself. Send it to: pcb@iteadstudio.com. I always just send a zip file with the Gerbers in it. I use my order number for the filenames (Gerbers and zip), but this is not a requirement of theirs. I also tend to put the order number on the board, just for my reference, but I figure it can't hurt.

Their requirements as well as the email address are at the bottom of the page for whatever particular board you're ordering, e.g. http://imall.iteadstudio.com/open-pcb/pcb-prototyping/im120418001.html
1882  Using Arduino / LEDs and Multiplexing / Re: Controlling many incandescent bulbs on: March 16, 2013, 06:36:33 pm
MOSFETs come to mind. Are these bulbs powered by AC or DC?
1883  Using Arduino / Project Guidance / Re: XBee Antenna on: March 16, 2013, 06:27:34 pm
What can I do to increase range?

Again: What kind of range does it get now, and what is the requirement? What exactly is the problem we are trying to solve here?

If the XBee is truly "inside" the robot, that would be an issue if the robot's skin consists of significant metal parts. Even lacking a metallic skin, keep the XBee away from large metallic components. Consider using an XBee model with an RP-SMA connector (e.g. XBP24BZ7SIT-004, and note also the extension cables and antennas on that page) and get an antenna made for the job, e.g. https://www.sparkfun.com/products/558
1884  Using Arduino / Programming Questions / Re: Wait for real end of serial transmission on: March 15, 2013, 08:08:12 pm
Serial.flush() followed by Serial.end() does it for me in 1.0.3.
1885  Using Arduino / Project Guidance / Re: XBee Antenna on: March 14, 2013, 10:24:14 pm
How far does it need to go? The spec on that particular module is 300 feet indoor/urban, up to 2 miles outdoor line-of-sight.
1886  Using Arduino / General Electronics / Re: Simple Electrical/Timing Switch on: March 14, 2013, 09:43:13 am
Great! Enjoy!
1887  Using Arduino / General Electronics / Re: Simple Electrical/Timing Switch on: March 14, 2013, 08:03:00 am
Arduino digital outputs are 5V (when on) and can only supply about 20mA. But the Power Switch Tail will accept one of those as input, and uses it as the control signal to switch the 120VAC. It also keeps the Arduino isolated from the 120VAC with an optical isolator, which is a very nice safety feature.

You say "precise" control -- just how precise is "precise"? The resonator in an Uno that provides the system clock should be accurate to about ±0.5%. Is that precise enough?
1888  Using Arduino / General Electronics / Re: Simple Electrical/Timing Switch on: March 14, 2013, 07:00:00 am
Some sort of interface will be needed to switch the AC. There are several approaches, this is an easy and relatively safe one:
https://www.adafruit.com/products/268
1889  Using Arduino / Storage / Re: How to write and read the EEPROM? on: March 12, 2013, 10:38:37 pm
IT WORKS =DDDDDDD

thanks formal => http://babuino.info/post/45239830057/gravar-dados-na-eeprom-do-arduino-dados-de-ate-16-e-32

Has Google Translate plugin if you want to read ^ ^

Nice site, and thanks for the kudos smiley-grin
1890  Using Arduino / Storage / Re: How to write and read the EEPROM? on: March 12, 2013, 06:56:01 pm
Jack, how do I get that? #include <avr/eeprom.h>      //http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html

It's already there, just type it in. The code I posted above should compile and run fine. AVR-Libc is part of the Arduino toolchain.
Pages: 1 ... 124 125 [126] 127 128 ... 287