Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- const int AT24CAddr = 0x50;
- byte i = 1;
- void AT24CWrite(int deviceaddress, unsigned int eeaddress, byte data) {
- int rdata = data;
- Wire.beginTransmission(deviceaddress);
- Wire.write(eeaddress >> 8); // MSB
- Wire.write(eeaddress & 0xFF); // LSB
- Wire.write(rdata);
- Wire.endTransmission();
- }
- int16_t AT24CRead(int deviceaddress, unsigned int eeaddress) {
- int16_t rdata = -1;
- Wire.beginTransmission(deviceaddress);
- Wire.write(eeaddress >> 8); // MSB
- Wire.write(eeaddress & 0xFF); // LSB
- Wire.endTransmission();
- Wire.requestFrom(deviceaddress,1);
- if (Wire.available()) rdata = Wire.read();
- return rdata;
- }
- void resetRec() {
- AT24CWrite(AT24CAddr, 0, 0);
- delay(10);
- }
- void record() {
- uint32_t m = millis();
- byte t, state = HIGH;
- while (1) {
- // Wait for state change
- while (digitalRead(A0) == state) {
- if (millis() - m > 3000) {
- AT24CWrite(AT24CAddr, i, 0);
- return;
- }
- }
- // Record time
- t = (millis() - m) / 10;
- m = millis();
- AT24CWrite(AT24CAddr, i++, t);
- digitalWrite(13, state);
- delay(10);
- state = !state;
- }
- }
- void playback() {
- uint32_t m = millis();
- byte t, state = HIGH;
- while (t = AT24CRead(AT24CAddr, i++)) {
- delay((uint32_t)t * 10);
- digitalWrite(13, state);
- state = !state;
- }
- }
- void setup() {
- pinMode(A0, INPUT_PULLUP);
- pinMode(13, OUTPUT);
- digitalWrite(13, HIGH);
- Wire.begin();
- // To reset the EEPROM data indicator:
- //resetRec();
- //return;
- // Light goes off when ready to record/playback
- delay(1000);
- digitalWrite(13, LOW);
- Serial.println(AT24CRead(AT24CAddr, 0));
- if (AT24CRead(AT24CAddr, 0) != 123) {
- record();
- delay(10);
- if (i > 1) AT24CWrite(AT24CAddr, 0, 123); // Mark "recorded"
- } else {
- playback();
- digitalWrite(13, LOW);
- while (1);
- }
- }
- void loop() {
- digitalWrite(13, !digitalRead(13));
- delay(50);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement