Arduino EEPROM读写阵列? [英] Arduino EEPROM write and read array?

查看:69
本文介绍了Arduino EEPROM读写阵列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Arduino通电后,它会在闪存中存储一​​个int数组,例如:

When the Arduino is powered up it has an int array stored in the flash, for example:

int secretCode[maximumKnocks] = {50, 25, 25, 50, 100, 50, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

按下程序按钮后,它将等待压电式敲击,然后此数组更改为例如:

When the program button is pressed, it then waits for the piezo to pick up a knock and this array then changes to, for example:

int secretCode[maximumKnocks] = {25, 50, 25, 50, 100, 100, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

(基于 http://grathio.com/assets/secret_knock_detector.pde )

我如何在EEPROM中读写阵列?这对我来说是全新的,所以任何帮助都会很棒.

How would I write and read the array to/from the EEPROM? This is completely new to me, so any help would be great.

推荐答案

您将使用 EEPROM写入值.Write 函数-遍历数组,依次写入每个值.

You would write the values using the EEPROM.Write function - loop over the array, writing each value in turn.

假设您不需要存储大于254的整数(在这种情况下,您必须为secretCode中的每个元素写两个字节),则为:

Assuming you don't need to store integer values > 254 ( in which case you'd have to write two bytes for each element in secretCode ), this would be:

for ( int i = 0; i < maximumKnocks; ++i )
   EEPROM.write ( i, secretCode [ i ] );

已经编写了它们,您可以在启动时使用的读取功能读取.设置.如果EEPROM中的值是0xff,即您首次刷新芯片时的值,请不要将其复制到密码中.

Having written them, you would read them back on start-up using the read function in the setup. If the values in the EEPROM are 0xff, which they will be when you first flash the chip, don't copy them into the secret code.

if ( EEPROM.read ( 0 ) != 0xff )
    for (int i = 0; i < maximumKnocks; ++i )
        secretCode [ i ] = EEPROM.read ( i );

这篇关于Arduino EEPROM读写阵列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆