Arduino - 字节数组不更新
Arduino - Byte Aray not updating
我有一个字节数组声明为:
byte b_seconds[8];
我可以用这个清除它:
void clear_seconds() {
for (int x = 0; x < 8 ; x++) {
b_seconds[x] = 0b00000000;
}
}
}
作为时钟项目的一部分,我正在更新字节数组以发送到一些移位寄存器以连续点亮 60 个 LED。
要更新字节数组,我有这个函数:
void update_seconds(int _secs) {
if (_secs == 0) {
clear_seconds();
bitSet(b_seconds[0], _secs);
}
if (_secs > 0 && _secs < 8) {
bitSet(b_seconds[0], _secs);
}
if ((_secs > 7) && (_secs < 16)) {
bitSet(b_seconds[1], _secs);
}
if (_secs > 15 && _secs < 24) {
bitSet(b_seconds[2], _secs);
}
if (_secs > 23 && _secs < 32) {
bitSet(b_seconds[3], _secs);
}
if (_secs > 31 && _secs < 40) {
bitSet(b_seconds[4], _secs);
}
if (_secs > 39 && _secs < 48) {
bitSet(b_seconds[5], _secs);
}
if (_secs > 47 && _secs < 56) {
bitSet(b_seconds[6], _secs);
}
if (_secs > 55 && _secs < 64) {
bitSet(b_seconds[7], _secs);
}
}
所以我将秒数发送到 update_seconds 函数,第一个数组 b_seconds[0] 一切正常,但是当秒数达到 8 或超过 8 时,它们不会更新适当的数组。我已经检查并可以在打印的每个部分中设置一个打印函数,但是数组没有按照 bitSet() 函数进行更新。
这意味着在每一分钟结束时,我最终得到 b_seconds[0] 正确的 0b11111111,但所有其他的都是 0b00000000,这是不正确的。
有什么想法吗?
我不确定你为什么要这样做,但是当 n > 7 时,你不能在一个字节中使用 bitSet(x,n)
。
您在 bitSet(b_seconds[i], _secs)
中使用的 _secs
中的数字可能太大(大于 7),因为您在数组中的每个字节中只有 8 位可用于设置。
有关更多信息,请参阅 bitSet() documentation。
我有一个字节数组声明为:
byte b_seconds[8];
我可以用这个清除它:
void clear_seconds() {
for (int x = 0; x < 8 ; x++) {
b_seconds[x] = 0b00000000;
}
}
}
作为时钟项目的一部分,我正在更新字节数组以发送到一些移位寄存器以连续点亮 60 个 LED。 要更新字节数组,我有这个函数:
void update_seconds(int _secs) {
if (_secs == 0) {
clear_seconds();
bitSet(b_seconds[0], _secs);
}
if (_secs > 0 && _secs < 8) {
bitSet(b_seconds[0], _secs);
}
if ((_secs > 7) && (_secs < 16)) {
bitSet(b_seconds[1], _secs);
}
if (_secs > 15 && _secs < 24) {
bitSet(b_seconds[2], _secs);
}
if (_secs > 23 && _secs < 32) {
bitSet(b_seconds[3], _secs);
}
if (_secs > 31 && _secs < 40) {
bitSet(b_seconds[4], _secs);
}
if (_secs > 39 && _secs < 48) {
bitSet(b_seconds[5], _secs);
}
if (_secs > 47 && _secs < 56) {
bitSet(b_seconds[6], _secs);
}
if (_secs > 55 && _secs < 64) {
bitSet(b_seconds[7], _secs);
}
}
所以我将秒数发送到 update_seconds 函数,第一个数组 b_seconds[0] 一切正常,但是当秒数达到 8 或超过 8 时,它们不会更新适当的数组。我已经检查并可以在打印的每个部分中设置一个打印函数,但是数组没有按照 bitSet() 函数进行更新。 这意味着在每一分钟结束时,我最终得到 b_seconds[0] 正确的 0b11111111,但所有其他的都是 0b00000000,这是不正确的。 有什么想法吗?
我不确定你为什么要这样做,但是当 n > 7 时,你不能在一个字节中使用 bitSet(x,n)
。
您在 bitSet(b_seconds[i], _secs)
中使用的 _secs
中的数字可能太大(大于 7),因为您在数组中的每个字节中只有 8 位可用于设置。
有关更多信息,请参阅 bitSet() documentation。