Chapter 27 Solder Circuit Board
}
outData(data); //Send the data to 74HC595
}
}
return 0;
}
We can see that this program is different from the previous one. We define an array to modulate different
PWM pulse width for LEDs, so that different LEDs can emit different brightness. Starting from the array index
0, take an array of 8 adjacent numbers as the LED duty cycle and output it at a time. Increases the starting
index number in turn, then it will create a flowing effect.
const int pluseWidth[]={0,0,0,0,0,0,0,0,64,32,16,8,4,2,1,0,0,0,0,0,0,0,0};
By recording the moving time point to control the speed of the movement of index number, namely, control
the flowing speed of flowing water light. Variable moveSpeed saves the time interval of each move, and the
greater the value, the slower the flowing rate. On the contrary, the faster the flowing.
if(millis() - lastMove > moveSpeed) { //speed control
lastMove = millis(); //Record the time point of the move
index++; //move to next
if(index > 15) index = 0; //index to 0
}
Finally, in a āforā cycle with i=64, modulate output pulse width of PWM square wave. And the progress, from
the beginning of implementing the for cycle to the end, is a PWM cycle. In the cycle, there is another for cycle
with j=8. And in this cycle, compare the cycle number āiā to the value of the array to determine output high
or low level. At last, the data will be sent to 74HC595.
for(i=0;i<64;i++){ //The cycle of PWM is 64 cycles
int8_t data = 0; //This loop of output data
for(j=0;j<8;j++){ //Calculate the output state of this loop
if(i < pluseWidth[index+j]){ //Calculate the LED state according to
the pulse width
data |= 0x01<<j ; //Calculate the data
}
}
outData(data); //Send the data to 74HC595
}