Last active
June 17, 2019 14:55
-
-
Save DaveCalaway/5d196b27ec962d2ccaec8b0ce9f0d3e3 to your computer and use it in GitHub Desktop.
2variables to Bytes - EEPROM
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Thx u internet | |
/* OUPUT | |
smash n | |
5 | |
0 | |
--- | |
smash c | |
14 | |
1 | |
n == 5 | |
c == 270 | |
*/ | |
#include <EEPROM.h> | |
int n = 5; | |
int c = 270; | |
void setup() { | |
Serial.begin(9600); | |
unsigned bytes[4]; // 4 byte = 2^32 = 4.294.967.296 | |
Serial.println("smash n"); | |
bytes[3] = n & 0xFF; | |
EEPROM.write(0, bytes[3]); | |
Serial.println(bytes[3]); | |
bytes[2] = (n >> 8) & 0xFF; | |
EEPROM.write(1, bytes[2]); | |
Serial.println(bytes[2]); | |
Serial.println("---"); | |
Serial.println("smash c"); | |
bytes[1] = c & 0xFF; | |
EEPROM.write(2, bytes[1]); | |
Serial.println(bytes[1]); | |
bytes[0] = (c >> 8) & 0xFF; | |
EEPROM.write(3, bytes[0]); | |
Serial.println(bytes[0]); | |
//Read the 2 bytes from the eeprom memory. | |
int four = EEPROM.read(0); | |
int three = EEPROM.read(1); | |
int two = EEPROM.read(2); | |
int one = EEPROM.read(3); | |
Serial.print("n == "); | |
Serial.println(((four << 0) & 0xFF) + ((three << 8) & 0xFFFF)); | |
Serial.print("c == "); | |
Serial.println(((two << 0) & 0xFF) + ((one << 8) & 0xFFFF)); | |
} | |
void loop() { | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment