目录
简介
这是一个使用8×8 LED矩阵和Arduino的随机数发生器。它可在1到60之间调节,还具备定时器播放模式。
该器件是一个随机数发生器,采用8×8 LED矩阵,由Arduino驱动。
它完全可调,工作范围为1至60。
装配非常简单,该装置可用于需要一组随机数的许多应用中。
主要有两种操作模式:
模式1:随机生成(N =种群大小,n =样本大小)。
模式2:绘图计时器(1到60秒)。
步骤一 材料准备
硬件准备:
arduino uno
led 驱动器max7219
按钮开关
面包板
杜邦线
软件准备:
arduino IDE
步骤二 电路搭建
步骤三 编写程序
/*
Project: Random Number Generator
Author: LAGSILVA
Hardware: Arduino UNO-R3, MAX72XX LED Matrix (8x8)
Date: 20.Jul.2019
Revision: 2.0
License: CC BY-NC-ND 4.0
(Attribution-NonCommercial-NoDerivatives 4.0 International)
*/
#include // Library for LED Display - MAX72XX
// Global Variables
byte pDown = 10, pUp = 9, pMode = 8;
String shuffleDezenas, dezenas;
byte row, col, mode = 3;
byte btnMode, btnUp, btnDown;
byte totGrupo = 6, totDez = 60;
float timer = 1;
unsigned long ti, tf;
/*
Pin numbers of Arduino to be connected into MAX72XX (LED Matrix controlled by MAX72XX)
pin 5 is connected to the DataIn (DIN)
pin 6 is connected to LOAD (CS)
pin 7 is connected to the CLK (CLK)
*/
LedControl lc = LedControl(5, 7, 6, 1); // LedControl(int dataPin, int clkPin, int csPin, int numDevices)
void setup() {
pinMode(pUp, INPUT_PULLUP);
pinMode(pDown, INPUT_PULLUP);
pinMode(pMode, INPUT_PULLUP);
// Setup of Display "0"
lc.shutdown(0, false); // Wakeup Display "0"
lc.setIntensity(0, 8); // Set the Brightness of Display (0 to 15)
lc.clearDisplay(0); // Clear Display "0" (Hour)
randomSeed(analogRead(A1));
numbers();
}
void shuffle() { // Shuffling routine
byte k, n;
String tempSeq = dezenas;
shuffleDezenas = "";
for (k = 0; k < totGrupo; k++) {
n = random(tempSeq.length() / 2);
shuffleDezenas = shuffleDezenas + tempSeq.substring(n * 2 , n * 2 + 2);
tempSeq.remove(n * 2, 2);
}
}
void numbers() { // Numbers to be drawn
dezenas = "";
for (byte k = 1; k <= totDez; k++) {
if (k < 10) {
dezenas = dezenas + "0" + k;
}
else {
dezenas = dezenas + k;
}
}
}
void plot(byte tot) {
byte row, col;
if (tot > 4) {
row = (12 - tot % 8) % 8;
col = (tot + 3) / 8;
}
else {
row = 6 - tot;
col = 0;
}
lc.setLed(0, row, col, true);
}
void loop() {
btnMode = digitalRead(pMode);
btnUp = digitalRead(pUp);
btnDown = digitalRead(pDown);
if (btnMode == LOW) {
mode = (mode + 1) % 4; // Modes: 0 (N = Population Size)) / 1 (n = Sample Size) / 2 (Manual Draw)) / 3 (Timer of Draw)
lc.clearDisplay(0);
if (mode <= 1) {
lc.setLed(0, 7 - mode, 0, true);
} else {
lc.setLed(0, 3 - mode, 0, true);
}
delay(150);
if (mode == 0) {
plot(totDez);
}
if (mode == 1) {
plot(totGrupo);
}
}
if (mode == 0 && btnUp == LOW) { // Mode 0: Setup of Population (Increase mode)
lc.clearDisplay(0);
lc.setLed(0, 7, 0, true);
totDez += 1;
if (totDez > 60) {
totDez = 1;
}
plot(totDez);
numbers();
delay(150);
}
if (mode == 0 && btnDown == LOW) { // Mode 0: Setup of Population Size (Decrease mode)
lc.clearDisplay(0);
lc.setLed(0, 7, 0, true);
totDez -= 1;
if (totDez < 1) {
totDez = 60;
}
plot(totDez);
numbers();
delay(150);
}
if (mode == 1 && btnUp == LOW) { // Mode 1: Setup of Sample Size (Increase mode)
lc.clearDisplay(0);
lc.setLed(0, 6, 0, true);
totGrupo += 1;
if (totGrupo > 60) {
totGrupo = 1;
}
plot(totGrupo);
delay(150);
}
if (mode == 1 && btnDown == LOW) { // Mode 1: Setup of Sample Size (Decrease mode)
lc.clearDisplay(0);
lc.setLed(0, 6, 0, true);
totGrupo -= 1;
if (totGrupo < 1) {
totGrupo = 60;
}
plot(totGrupo);
delay(150);
}
if (mode == 2 && (btnUp == LOW || btnDown == LOW)) { // Mode 2: Manual Draw of Number
for (byte s = 1; s <= 20; s++) {
lc.clearDisplay(0);
lc.setLed(0, 1, 0, true);
shuffle();
for (byte k = 0; k < totGrupo; k++) {
byte dez = (shuffleDezenas.substring(k * 2, k * 2 + 2)).toInt();
plot(dez);
}
delay(25);
}
}
if (mode == 3 && btnUp == LOW) { // Mode 3: Increase the time lapse
lc.clearDisplay(0);
lc.setLed(0, 0, 0, true);
timer += 1;
if (timer > 60) {
timer = 0;
}
plot(timer);
delay(150);
ti = millis();
}
if (mode == 3 && btnDown == LOW) { // Mode 3: Decrease the time lapse
lc.clearDisplay(0);
lc.setLed(0, 0, 0, true);
timer -= 1;
if (timer < 0) {
timer = 60;
}
plot(timer);
delay(150);
ti = millis();
}
if (mode == 3) { // Automatic draw of numbers by timer
if (timer == 0) {
timer = 0.050;
}
tf = millis() - ti;
if (tf >= timer * 1000) {
lc.clearDisplay(0);
lc.setLed(0, 0, 0, true);
shuffle();
for (byte k = 0; k < totGrupo; k++) {
byte dez = (shuffleDezenas.substring(k * 2, k * 2 + 2)).toInt();
plot(dez);
ti = millis();
}
}
}
}
步骤四 验证结果
完成以后连接图如上,运行起来如封面可以正常生成随机数,可以实现相应的模式。
学习了
学到了