I obviously don't want to write to the eeprom every time through the loop. I first tried with a counter but could never get it to work as the counter kept getting overwritten and other problems.
I am now using time.monotonic which is working except the rotary encoder stops working after the save. I am not sure the write to eeprom code is correct.
Please can somebody let me know what my problem is.
Tinket MO with external Adafruit eeprom module
After the write I am getting this error:
Traceback (most recent call last):
File "code.py", line 82, in <module>
File "adafruit_24lc32.py", line 142, in __setitem__
RuntimeError: EEPROM currently write protected.
Code: Select all
import board
import neopixel
import time
import adafruit_24lc32
from time import sleep
i2c = board.I2C()
countEnd = 33
ORDER = neopixel.GRB
pixel_pin = board.D1
num_pixels = 33
eeprom = adafruit_24lc32.EEPROM_I2C(i2c)
encoder = rotaryio.IncrementalEncoder(board.D3, board.D4)
eeprom_position = int.from_bytes(eeprom[0], (b"\x02\x00\x10\x00"))
encoder.position = 0
print("Value from eeprom")
print(eeprom_position)
PURPLE1 = (155, 48, 255)
SPRINGGREEN = (0, 255, 127)
SEAGREEN1 = (0, 255, 127)
CYAN = (0, 255, 255)
LIGHTGOLDENROD1 = (255, 236, 139)
AQUAMARINE = (127, 255, 212)
WHITE = (255, 255, 255)
DARKSALMON = (233, 150, 122)
LIGHTPINK = (255, 182, 193)
PALEVIOLETRED = (219, 112, 147)
LIGHTCORAL = (240, 128, 128)
MELON = (227, 168, 105)
chocolate1 = (255, 127, 36)
DarkOrange = (255, 140, 0)
DeepPink = (255, 20, 147)
DarkOrchid = (153, 50, 204)
RED = (255, 0, 0)
colours = [
WHITE,
LIGHTGOLDENROD1,
RED,
LIGHTPINK,
DeepPink,
DARKSALMON,
PALEVIOLETRED,
LIGHTCORAL,
DarkOrchid,
chocolate1,
DarkOrange,
SPRINGGREEN,
SEAGREEN1,
PURPLE1,
CYAN,
AQUAMARINE,
MELON
]
pixels = neopixel.NeoPixel(pixel_pin, num_pixels,
brightness=0.1, auto_write=False, pixel_order=ORDER)
last_position = None
count = 0
save_delay = 15
initial_time = time.monotonic()
while True:
position = (encoder.position + eeprom_position) % len(colours)
print("Value from encoder")
print(encoder.position)
print("Value from eeprom_position")
print(eeprom_position)
print("position")
print(position)
if last_position is None or position != last_position:
print("Value from loop")
print(eeprom_position)
last_position = position
eeprom[0] = position
if type(colours[position]) is tuple:
pixels.fill(colours[position])
pixels.show()
sleep(0.05)
# WORKS PROPELY DOWN TO HERE
# THIS IS THE WRITE TO EEPROM
current_time = time.monotonic()
if current_time - initial_time > save_delay:
eeprom[0] = position
eeprom.write_protected = False
print(eeprom.write_protected)
print(eeprom[0])
eeprom.write_protected = True
print(eeprom.write_protected)