battle programmers alliance
Would you like to react to this message? Create an account in a few clicks or log in to continue.

battle programmers allianceLog in

the LivinGrimoire Artificial General Intelligence software design pattern forum

Arduino Grimoire

power_settings_newLogin to reply

descriptionArduino Grimoire - Page 2 EmptyRe: Arduino Grimoire

more_horiz
SOLDERING



descriptionArduino Grimoire - Page 2 EmptyRe: Arduino Grimoire

more_horiz
the ATMega 328 at the heart of the arduino uno has 1k of EEPROM, designed to remember it's contents
for many year, despite its name it is read and writtable.

read save 1 char :

Code:

#include <EEPROM.h>
int addr = 0;
char ch;
void setup() {
  Serial.begin(9600);
  ch = EEPROM.read(addr);
 }
void loop() {
  if (Serial.available() > 0)  {  ch = Serial.read();  EEPROM.write(0, ch);  Serial.println(ch);  }
  Serial.println(ch);  delay(1000);
}


write int :

int x = 1234; EEPROM.write(0, highByte(x)); EEPROM.write(1, lowByte(x));

0, 1 being the EEPROM memory addresses, which much be in the 0 - 1023 range
overall you can save about 100 k times

read int
byte high = EEPROM.read(0); byte low = EEPROM.read(1); int x = (high << 8) + low;

using the built in EEPROM library to save or read data :

Code:

#include <avr/eeprom.h>

void setup() {
 Serial.begin(9600);
 int i1 = 123;
 eeprom_write_block(&i1, 0, 2);
 int i2 = 0;
 eeprom_read_block(&i2, 0, 2);
 Serial.println(i2); }
void loop() { }


read save float :

Code:

#include <avr/eeprom.h>
void setup() {
Serial.begin(9600);
 float f1 = 1.23;
 eeprom_write_block(&f1, 0, 4);
 float f2 = 0;
 eeprom_read_block(&f2, 0, 4);
 Serial.println(f2); }
void loop() { }


read write string

Code:

const int maxPasswordSize = 20;
char password[maxPasswordSize];
void setup() {
 eeprom_read_block(&password, 0, maxPasswordSize);
 Serial.begin(9600);
 }
void loop() {
 Serial.print("Your password is:");
 Serial.println(password);
 Serial.println("Enter a NEW password");
 while (!Serial.available()) {}; int n = Serial.readBytesUntil('\n', password, maxPasswordSize);
 password[n] = '\0'; eeprom_write_block(password, 0, maxPasswordSize);
 Serial.print("Saved Password: ");
 Seria.println(password);


clear the arduino EEPROM

Code:

#include <EEPROM.h>

void setup() {
 Serial.begin(9600);
 Serial.println("Clearing EEPROM")a;
 for (int i = 0; i < 1024; i++) { EEPROM.write(i, 0); }
 Serial.println("EEPROM Cleared");
}
void loop() { }

descriptionArduino Grimoire - Page 2 EmptyRe: Arduino Grimoire

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply