Pages

20180523

Programming Arduino: Next Steps by Simon Monk


  • Note that any program stored on the device is retained because it is kept in nonvolatile flash memory--that is, memory that remembers even when the device is not powered on.
  • Because your Arduino is connected to your computer by USB, you can send messages between the two using a feature of the Arduino IDE called the serial monitor.
  • We are now in the Arduino’s realm. The Arduino has a small resident program installed on every microcontroller that is included with its board. THis program is called a bootloader. The bootloader actually runs very briefly every time an Arduino is reset.
  • The Arduino boards all come with a six-pin header that can be used to program the Arduino directly using AVR Studio. In fact, some boards come with two six-pin headers: one for the main processor and one for the USB interface, so be careful to connect to the right one.
  • The Arduino installation folder contains bootloader hex files that can be flashed onto an ATmega 28 using AVR Studio. You will find these files in the hardware/arduino/bootloaders folder. There you will find hex files for all sorts of different hardware.
  • Interrupts allow microcontrollers to respond to events without having to poll continually to see if anything has changed. In addition to associating interrupts with certain pins you can also use timer-generated interrupts.
  • While inside an ISR, interrupts are automatically turned off. This prevents the potential confusion caused by ISRs interrupting each other, but it has some side effects.
  • As a rule of thumb, you can calculate the number of hours that a battery will last before it is discharged by dividing the capacity in milliamp hour (mAh) by the number of milliamps (mA) being drawn.
  • The ultimate way to save power on your Arduino is to put it to sleep when it doesn’t have anything useful to do.
  • There are two methods to wake up an Arduino. One is to use an external interrupt and the other is to set a timer to wake the Arduino after a period of time.
  • The best ways to minimize current consumption are to:
  • Put the microcontroller to sleep when it’s not doing anything.
  • Run the Arduino at a lower voltage.
  • Run the Arduino at a lower clock frequency.
  • The RAM in an Arduino is only used to hold the contents of variables and other data relating to the running of the program. RAM is not persistent; that is, when the power is disconnected, the RAM is cleared. If the program needs to store persistent data, then it must write that data to EEPROM. The data can then be read back when the sketch restarts.
  • A really good way to reduce memory usage is to make sure that any constant variables are declared as such. To do this, just put the word const in front of the variable declaration. Knowing that the value will never change allows the compiler to substitute tin the value in place of the variable, which saves space.
  • Recursion is a technique where a function calls itself. Recursion can be a powerful way of expressing and solving a problem. In functional programming languages such as LISP and Scheme, recursion is used a great deal.
  • Every time you call a function, a stack frame is created. A stack frame is a small memory record that includes storage space for parameters and local variables used by the function, as well as a return addresses that specifies the point in the program from which execution should continue when the function has finished running and returned.
  • Any use of Serial.println pulls about 500 bytes of library code into the sketch. So, once you are convinced that the sketch is working, remove or comment out these lines.
  • The malloc (memory allocate) command allocates memory from an area of RAM called the heap. Its argument is the number of bytes to be allocated.
  • The danger with dynamic memory allocation is that you can easily get in a situation where memory is allocated by not released, so then the sketch unexpectedly runs out of memory. Running out of memory can cause the Arduino to hang. If all the memory is allocated statically, however, this cannot happen.
  • The contents of any variable used in an Arduino sketch will be cleared and lost whenever the Arduino loses power or is reset. If you need to store values persistently, you need to write them a byte at a time into EEPROM memory. The Arduino Uno has 1kB of EEPROM memory.
  • EEPROM contents are not cleared by uploading a new sketch; once written, EEPROM values can only be changed by writing a new value on top of the old value. So if this is the first time that the sketch has been run, then there is no way to know what value might be left in EEPROM by a previous sketch.
  • EEPROM is slow to read and write (about 3ms). It is also only guaranteed to be reliable for 100,000 write cycles before it starts suffering from amnesia. For this reason, you need to be careful not to write to it every time around a loop.
  • The flash memory in an Arduino can only be written to about 100,000 times before it becomes useless.
  • The flash contains your program, so, if you miscalculate and write over the program, very strange things could happen.
  • The easiest way to create flash-stored string constants is to use the F() function.
  • By putting the PROGMEM directive in front of the array declaration, you ensure that it is only stored in flash memory. To read value out of it, however, you now have to use the function pgm_read_word from the avr/pgmspace library.
  • The I2C interface bus is a standard for connecting microcontrollers and peripherals together. I2C is sometimes referred to as Two Wire Interface (TWI). All the Arduino boards have at least one I2C interface to which you can attach a wide range of peripherals.
  • I2C uses two wires to transmit and receive data. These two lines are called the Serial Clock Line (SCL) and the Serial Data Line (SDA).
  • The baud rate is the number of signal transitions per second, which would be the same as the number of bits per second, were it not for the fact that a byte of data may have start, end, and parity bits.
  • The serial commands are not contained in a library, so you do not need an include command in your sketch.
  • The essence of Digital Signal Processing (DSP) is to digitize a signal using an analog-to-digital converter (ADC), manipulate it in some way, and then generate an analog output signal using a digital-to-analog converter (DAC).
  • In general, Arduinos are not the ideal devices for DSP. They cannot capture analog signals particularly fast, and their digital output is limited to PWM. The exception to this is the Arduino Due, which, as well as having lots of ADCs also has a fast processor and two tru DACs. Therefore, the Due’s hardware is sufficiently good enough to stand a fighting chance digitizing a stereo audio signal and doing something with it.

No comments:

Post a Comment