项目背景:这是我为arduino编写的代码,用于尝试创建二进制时钟,在这里我创建了一个24长度的布尔值数组来定义二进制时钟。这个数组被分解成这样:00 | 0000 | 000 | 0000 | 000 | 0000H1| H2 | M1| M2 | S1| S2
我打算使用3个8位移位寄存器来控制输出led,因此这可以分解为每个数字1个,但是我没有这样做......不知道为什么。
TLDR:我做了这个该死的代码,它目前只增加每隔一个周期打印的二进制值和每隔一个周期打印的十进制值,有人能帮我弄清楚为什么吗?
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
}
void loop() {
int h1 = 0;
int h2 = 0;
int m1 = 0;
int m2 = 0;
int s1 = 0;
int s2 = -1;
while(0 == 0)
{
// put your main code here, to run repeatedly:
s2 = s2 + 1;
if (s2 == 10)
{
s2 = 0;
s1 = s1 + 1;
if ( s1 == 6)
{
s1 = 0;
m2 = m2 + 1;
if ( m2 == 10)
{
m2 = 0;
m1 = m1 + 1;
if (m1 == 6)
{
m1 = 0;
h2 = h2 + 1;
if ( h2 == 10 )
{
h2 = 0;
h1 = h1 + 1;
}
else if ((h2 == 5) && (h1 == 2))
{
h1 = 0;
h2 = 0;
}
}
}
}
}
stringSetter(h1,h2,m1,m2,s1,s2);
delay(1000);
}
}
void stringSetter(int h1, int h2, int m1, int m2, int s1, int s2)
{
//h1 - converting to boolean array
boolean h1b[2];
if ( h1 == 0 )
{
h1b[0] = 0;
h1b[1] = 0;
}
else if ( h1 == 1 )
{
h1b[0] = 1;
h1b[1] = 0;
}
else if ( h1 == 2 )
{
h1b[0] = 0;
h1b[1] = 1;
}
//h2 - converting to boolean array
boolean h2b[4];
if ( h2 == 0 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 1 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 2)
{
h2b[0] = 0;
h2b[1] = 1;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 3 )
{
h2b[0] = 1;
h2b[1] = 1;
h2b[2] = 0;
h2b[3] = 0;
}
else if ( h2 == 4 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 5 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 6 )
{
h2b[0] = 0;
h2b[1] = 1;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 7 )
{
h2b[0] = 1;
h2b[1] = 1;
h2b[2] = 1;
h2b[3] = 0;
}
else if ( h2 == 8 )
{
h2b[0] = 0;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 1;
}
else if ( h2 == 9 )
{
h2b[0] = 1;
h2b[1] = 0;
h2b[2] = 0;
h2b[3] = 1;
}
//m1 - converting to boolean array
boolean m1b[3];
if ( m1 == 0 )
{
m1b[0] = 0;
m1b[1] = 0;
m1b[2] = 0;
}
else if ( m1 == 1 )
{
m1b[0] = 1;
m1b[1] = 0;
m1b[2] = 0;
}
else if ( m1 == 2)
{
m1b[0] = 0;
m1b[1] = 1;
m1b[2] = 0;
}
else if ( m1 == 3 )
{
m1b[0] = 1;
m1b[1] = 1;
m1b[2] = 0;
}
else if ( m1 == 4 )
{
m1b[0] = 0;
m1b[1] = 0;
m1b[2] = 1;
}
else if ( m1 == 5 )
{
m1b[0] = 1;
m1b[1] = 0;
m1b[2] = 1;
}
//m2 - converting to boolean array
boolean m2b[4];
if ( m2 == 0 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 1 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 2)
{
m2b[0] = 0;
m2b[1] = 1;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 3 )
{
m2b[0] = 1;
m2b[1] = 1;
m2b[2] = 0;
m2b[3] = 0;
}
else if ( m2 == 4 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 5 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 6 )
{
m2b[0] = 0;
m2b[1] = 1;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 7 )
{
m2b[0] = 1;
m2b[1] = 1;
m2b[2] = 1;
m2b[3] = 0;
}
else if ( m2 == 8 )
{
m2b[0] = 0;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 1;
}
else if ( m2 == 9 )
{
m2b[0] = 1;
m2b[1] = 0;
m2b[2] = 0;
m2b[3] = 1;
}
//s1 - converting to boolean array
boolean s1b[3];
if ( s1 == 0 )
{
s1b[0] = 0;
s1b[1] = 0;
s1b[2] = 0;
}
else if ( s1 == 1 )
{
s1b[0] = 1;
s1b[1] = 0;
s1b[2] = 0;
}
else if ( s1 == 2 )
{
s1b[0] = 0;
s1b[1] = 1;
s1b[2] = 0;
}
else if ( s1 == 3 )
{
s1b[0] = 1;
s1b[1] = 1;
s1b[2] = 0;
}
else if ( s1 == 4 )
{
s1b[0] = 0;
s1b[1] = 0;
s1b[2] = 1;
}
else if ( s1 == 5 )
{
s1b[0] = 1;
s1b[1] = 0;
s1b[2] = 1;
}
//s2 - converting to boolean array
boolean s2b[4];
if ( s2 == 0 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 1 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 2)
{
s2b[0] = 0;
s2b[1] = 1;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 3 )
{
s2b[0] = 1;
s2b[1] = 1;
s2b[2] = 0;
s2b[3] = 0;
}
else if ( s2 == 4 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 5 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 6 )
{
s2b[0] = 0;
s2b[1] = 1;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 7 )
{
s2b[0] = 1;
s2b[1] = 1;
s2b[2] = 1;
s2b[3] = 0;
}
else if ( s2 == 8 )
{
s2b[0] = 0;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 1;
}
else if ( s2 == 9 )
{
s2b[0] = 1;
s2b[1] = 0;
s2b[2] = 0;
s2b[3] = 1;
}
Serial.print('H');
Serial.print(h1);
Serial.print(h2);
Serial.print('M');
Serial.print(m1);
Serial.print(m2);
Serial.print('S');
Serial.print(s1);
Serial.print(s2);
Serial.println();
// setting up pin booleans
boolean combiBi[24] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
combiBi[23] = 0;
combiBi[22] = 0;
combiBi[21] = 0;
combiBi[20] = 0;
combiBi[19] = h1b[1];
combiBi[18] = h1b[0];
combiBi[17] = h2b[3];
combiBi[16] = h2b[2];
combiBi[15] = h2b[1];
combiBi[14] = h2b[0];
combiBi[13] = m1b[2];
combiBi[12] = m1b[1];
combiBi[11] = m1b[0];
combiBi[10] = m2b[3];
combiBi[9] = m2b[2];
combiBi[8] = m2b[1];
combiBi[7] = m2b[0];
combiBi[6] = s1b[2];
combiBi[5] = s1b[1];
combiBi[4] = s1b[0];
combiBi[3] = s2b[3];
combiBi[2] = s2b[2];
combiBi[1] = s2b[1];
combiBi[0] = s2b[0];
// printing out binary for pins
for(int o = 23; o > 0; o--){
if ( o == 19 )
{
Serial.print('H');
}
else if (o == 13)
{
Serial.print('M');
}
else if (o == 6)
{
Serial.print('S');
}
Serial.print(combiBi[o]);
}
Serial.println();
}
Serial Monitor Output
我试图在串行监视器中打印十进制和二进制等效输出,但是二进制只在每隔一个周期更新一次,而十进制则按预期执行。
for(int o = 23; o > 0; o--)
在这个循环中,o
永远不会达到0。因此,您永远不会打印每秒钟都在变化的最后一位。
更改运行条件为o >= 0
正如我在评论中已经提到的,你应该更多地了解数字及其在内存中的表示。您不必使用数组显式地手动实现每个单个位组合。这是一台计算机,让它来计算。