Last active
June 3, 2021 07:08
-
-
Save s0ren/db0067af5519907b373ffc6baa85ded3 to your computer and use it in GitHub Desktop.
Mine demoer fra undervisningen i "Embedded Controller I" med 53pom, juni '21 TEC
This file contains hidden or 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
// Demo af brug af Arduino library EEPROMex | |
// Denne demo skriver et heltal (int) til EEPROM med fortegn, og henter det igen. | |
// Inspireret af https://github.com/thijse/Arduino-EEPROMEx/blob/master/examples/EEPROMEx/EEPROMEx.ino | |
// Se også https://github.com/thijse/Arduino-EEPROMEx for at få mere info | |
#include <EEPROMex.h> | |
//#include <EEPROMVar.h> | |
const int maxAllowedWrites = 8000; // for at sikre at et evt amok-loop ikke smadrer hele EEPROMen | |
const int memBase = 350; | |
void setup() { | |
Serial.begin(115200); | |
// start reading from position memBase (address 0) of the EEPROM. Set maximumSize to EEPROMSizeUno | |
// Writes before membase or beyond EEPROMSizeUno will only give errors when _EEPROMEX_DEBUG is set | |
EEPROM.setMemPool(memBase, EEPROMSizeMega); //MEGA har mere end Uno | |
// Set maximum allowed writes to maxAllowedWrites. | |
// More writes will only give errors when _EEPROMEX_DEBUG is set | |
EEPROM.setMaxAllowedWrites(maxAllowedWrites); | |
delay(100); | |
Serial.println(""); | |
int temperatur = -17; // Variabel med den værdi jeg vil gemme | |
int res = EEPROM.writeInt(7, temperatur); // Skriver i EEPROM på adresse 7 (skulle nok være deleligt med 2 eller 4 | |
Serial.print(String(" res: ") + String(res)); // Udskriv 0 eller 1, for om det gik godt | |
Serial.println(); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
int temp = EEPROM.readInt(7); // Læser fra EEPROM på adresse 7 | |
Serial.print(", temp: " + String(temp)); // Udskriver den læste værdi | |
Serial.println(); | |
} |
This file contains hidden or 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
// Demo af brug af Arduino library EEPROMex | |
// Denne demo skriver en string til EEPROM med fortegn, og henter den igen. | |
// Det er lidt avanceret fordi en string er af variabel længde afhængig af hvor mange bogstaver der er i strengen. | |
// Derfor må vi bruge blockWrite og blockWrite, men først skal vi kende længden... | |
// På arduino bruges ikke "normale" c++ stings, som bare er en char* (pointer til char | |
// Inspireret af https://github.com/thijse/Arduino-EEPROMEx/blob/master/examples/EEPROMEx/EEPROMEx.ino | |
// Se også https://github.com/thijse/Arduino-EEPROMEx for at få mere info | |
#include <EEPROMex.h> | |
//#include <EEPROMVar.h> | |
const int maxAllowedWrites = 8000; | |
const int memBase = 350; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
// start reading from position memBase (address 0) of the EEPROM. Set maximumSize to EEPROMSizeUno | |
// Writes before membase or beyond EEPROMSizeUno will only give errors when _EEPROMEX_DEBUG is set | |
EEPROM.setMemPool(memBase, EEPROMSizeMega); | |
// Set maximum allowed writes to maxAllowedWrites. | |
// More writes will only give errors when _EEPROMEX_DEBUG is set | |
EEPROM.setMaxAllowedWrites(maxAllowedWrites); | |
delay(100); | |
Serial.println(""); | |
String tekst = F("Hej mormor. Nu skal du høre... Sidst vi var i Jylland og besøge dig, så vi en lille kattekilling..."); | |
//String tekst = "Hej mormor. Nu skal du høre... Sidst vi var i Jylland og besøge dig, så vi en lille kattekilling..."; | |
//Serial.println(tekst); | |
Serial.println(tekst.length()); | |
int res = EEPROM.writeBlock(10, "Hej mormor. Nu skal du høre... Sidst vi var i Jylland og besøge dig, så vi en lille kattekilling...", 102); | |
Serial.print("skrive-res skal være 102: "); | |
Serial.println(res); | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
char* mormor = new char[102]; | |
int res = EEPROM.readBlock(10, mormor, 102); | |
mormor[102] = 0; | |
Serial.print("læse-res skal være 102: "); | |
Serial.println(res); | |
Serial.println( String(mormor) ); | |
for (int i = 0; i<5; i++) | |
{ | |
Serial.println(byte(mormor[i])); | |
} | |
delete(mormor); | |
} |
This file contains hidden or 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
#include <MsTimer2.h> | |
#define KNAP 2 | |
#define btnThreshold 5 | |
static int btnCount = 0; | |
static int btnState = 0; | |
static long firstClick = 0; | |
void setup() { | |
// put your setup code here, to run once: | |
Serial.begin(115200); | |
pinMode(KNAP, INPUT_PULLUP); | |
attachInterrupt(digitalPinToInterrupt(KNAP), knapTryk, FALLING); | |
MsTimer2::set( 50, checkKnap ); | |
MsTimer2::start(); | |
} | |
void knapTryk(){ | |
Serial.print("x"); | |
btnCount++; | |
firstClick = millis(); | |
} | |
void checkKnap() | |
{ | |
if (!digitalRead(KNAP)) | |
{ | |
btnCount++; | |
} | |
else | |
{ | |
btnCount = 0; | |
firstClick = 0; | |
} | |
} | |
bool isBtnDown() | |
{ | |
return btnCount > btnThreshold | |
} | |
void loop() { | |
btnState = isBtnDown(); | |
Serial.print("btnCount: "); | |
Serial.print(btnCount); | |
Serial.print(", erTrykket: "); | |
Serial.print(btnState); | |
Serial.print(", firstClick: "); | |
Serial.print(firstClick); | |
Serial.println(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment