[last updated: 2023-07-10]
Arduino home page
Arduino programming - main page
-----
On this page:
- Eeprom: reading/writing
- Pause and wait for keyboard input:
- float/int data-type weirdness:
- Programming PWM pins:
- (go to:) Touch switches:
- Switch Debounce:
--------------------------------------------------------------------
- Eeproms:
- Many Arduino boards have onboard eeprom, which is a static memory that is preserved with power-down.
Each memory location stores a single Byte, which can be a single character or any dataType that can be expressed with 8 bits.
Memory locations are referenced by their addresses, with 0 being the first address/memory location.
- Arduino boards have different amounts of eeprom:
1024 bytes on the ATmega328P
512 bytes on the ATmega168 and ATmega8
4 KB (4096 bytes) on the ATmega1280 and ATmega2560
The Arduino and Genuino 101 boards have an emulated EEPROM space of 1024 bytes
- Arduino code to use eeprom:
#include < EEPROM.h >
This is a built-in library.
https://www.arduino.cc/en/Reference/EEPROM
- Library functions most commonly used:
EEPROM.read([address]); // retrieves a single byte from the address specified
EEPROM.write[address], [value]); // writes a single byte, "value", to the address specified
- Get and Put:
These commands read and write to eeprom, but instead of single bytes/characters like the read and write commands,
get and put operate with a single element of whatever dataType is being accessed.
EEPROM.put([address], [variable]);
This means you must be careful about mapping your address space to accomodate the length of whatever dataType you're storing.
Use special caution when working with Strings, which have variable length, and are terminated by null characters.
A get command looking for a string will read characters until it finds the null termination character.
If the null is missing, the get command may run past the end of the memory space and bomb.
- You can also read and write to eeprom directly with the EEPROM function, by treating the memory space as a single array, using eg.:
val = EEPROM[0];
EEPROM[0] = val;
- Other commands available in the library include:
clear, crc, iteration, and update.
---------------------------------
- Pause and wait for keyboard input:
---------------------------------
- float/int data-type weirdness:
- in this situation:
int tmp1 = 10;
int tmp2 = 3;
float tmp3 = tmp1 / tmp2;
returns: 3.00
- But:
float tmp3 = float(tmp1) / float(tmp2);
returns: 3.33
---------------------------------
- Programming PWM pins:
- For UNO, PWM pins are: 3, 5, 6, 9, 10, 11
- Freq of PWM is nom about 500 Hz
- analogWrite(pinNum, dutyCycle);
where dutyCycle can be between 0 and 255 (100% duty cycle)
with 127/255 = 50%
---------------------------------
- Touch Switches:
---------------------------------
- Switch Debounce:
However below code doesn't seem to work ...
- setup:
int PBpin = 3;
int PBState; // stabilized PB state
int lastPBState = HIGH; // most recent PB state
unsigned long lastTimeChanged = 0; // the last time the pin changed
unsigned long debounceDelay = 50;
- loop:
int PBtmp = digitalRead(PBpin); // current PB state
if (PBtmp != lastPBState) {
// state has changed, so reset timer
lastTimeChanged = millis();
} // endif PBtmp
if ( (lastTimeChanged + debounceDelay) > millis() ) {
// if here, PBtmp has been stable long enough to be real
// if the PB state has changed from last stable state:
if (PBtmp != PBState) {
PBState = PBtmp;
} // endif PB state changed
} // endif stable long enough
lastPBState = PBtmp;
---------------------------------
.
.
.
eof