#include "map.h"
#include "Button.h"
#define MODE_SWITCH_PIN A0 //一个控制模式 1-5
#define LENGTH_SWITCH_PIN A1 //一个控制循环长度 2-16
#define BUTTON_DIR_PIN 8 //一控制倒放 高电平倒放
#define BUTTON_REST_PIN 9 //一个控制复位 重置是高电平有效
#define BUTTON_PP_PIN 10 //脉冲输入的引脚
// 定义灯光模式,模式由CTorp建议
#define PAT_SINGLE 1 // 单一模式 (依次点亮 1-2-3-4-5-6-7-8-1-2-3-4-5-6-7-8-)
#define PAT_INCROT 2 // 包含旋转模式 (依次点亮 1-2-3-4-5-6-7-8-8-7-6-5-4-3-2-1-)
#define PAT_EXCROT 3 // 排除旋转模式 (依次点亮 1-2-3-4-5-6-7-8-7-6-5-4-3-2-)
#define PAT_DOUBLE 4 // 双重模式 (依次点亮 1-1-2-2-3-3-4-4-5-5-6-6-7-7-8-8-)
#define PAT_RANDOM 5 // 随机模式
Button Button_0(BUTTON_PP_PIN, 1); //
Button Button_1(BUTTON_DIR_PIN, 10); //
Button Button_2(BUTTON_REST_PIN, 10); //
unsigned int display_mode = PAT_SINGLE; // 当前模式,默认为单一模式
unsigned int display_length = 2; // 显示的长度 默认2
unsigned int pat_dir = 1; // 模式方向,1表示向前进行模式,-1表示向后
bool pat_first = true; // 对于成对的灯光,true表示第一个,false表示第二个
void setup() {
Serial.begin(115200);
pinMode(BUTTON_DIR_PIN, INPUT_PULLUP);
pinMode(BUTTON_REST_PIN, INPUT_PULLUP);
pinMode(HC165_DATA_PIN, INPUT);
pinMode(HC165_CLOCK_PIN, OUTPUT);
pinMode( HC165_LATCH_PIN, OUTPUT);
pinMode(HC595_SHCP_PIN, OUTPUT); //串行时钟针脚配置成输出模式
pinMode(HC595_STCP_PIN, OUTPUT); //锁存针脚配置成输出模式
pinMode(HC595_DS_PIN, OUTPUT); //数据输入针脚配置成输出模式
for (int i = 0; i < numOfRegisterPins; i++) { //初始化引脚为低电平
registers[i] = 0;
}
Button_0.begin(); // 初始化
Button_1.begin(); // 初始化
}
uint32_t old_time = 0;
uint32_t old_time_button = 0;
int step_num = -1;
void loop() {
display_mode = map(analogRead(MODE_SWITCH_PIN), 0, 1023, 1, 5);
display_length = map(analogRead(LENGTH_SWITCH_PIN), 0, 1023, 2, 16);
read_button();
if (Button_0.pressed()) {
if (Button_0.repeat_count() == 0) { //被单击
}
}
if (Button_0.released()) { //被松开
for (int i = 0; i < numOfRegisterPins; i++) { //初始化引脚为低电平
registers[i] = 0;
}
writeRegisters(registers);
}
if (Button_1.pressed()) {
if (Button_1.repeat_count() == 0) { //被单击
}
}
if (millis() - old_time >= 100) {
old_time = millis();
for (int i = 0; i < 16; i++) {
// Serial.print(button_nums[i]); Serial.print(" ");
}
//Serial.println();
}
}
void mode_1() {
step_num++;
if (step_num >= display_length) step_num = 0;
registers[step_num] = 1;
if (step_num > 0) {
registers[step_num - 1] = 0;
}
writeRegisters(registers);
}
void read_button() {
digitalWrite( HC165_LATCH_PIN, LOW);
digitalWrite( HC165_LATCH_PIN, HIGH);
for (int i = 0; i < numOfRegisterPins; i++) {
button_nums[i] = digitalRead(HC165_DATA_PIN);
digitalWrite(HC165_CLOCK_PIN, HIGH); // Shift out the next bit
digitalWrite(HC165_CLOCK_PIN, LOW);
}
}
void writeRegisters(bool* _state) {
uint16_t data = 0;
for (int i = 0; i < numOfRegisterPins; i++) {
data |= uint16_t((_state[i] ? 1 : 0)) << i;
}
digitalWrite(HC595_STCP_PIN, LOW); //拉低锁存针脚,为传输数据做准备
for (int i = 0; i < NUM_SHIFT_REGS; i++) {
byte byteToSend = (data >> (8 * i)) & 0xFF;
shiftOut(HC595_DS_PIN, HC595_SHCP_PIN, LSBFIRST, byteToSend);
}
digitalWrite(HC595_STCP_PIN, HIGH); //拉高锁存针脚,结束传输
}
nano:12
nano:11
nano:10
nano:9
nano:8
nano:7
nano:6
nano:5
nano:4
nano:3
nano:2
nano:GND.2
nano:RESET.2
nano:0
nano:1
nano:13
nano:3.3V
nano:AREF
nano:A0
nano:A1
nano:A2
nano:A3
nano:A4
nano:A5
nano:A6
nano:A7
nano:5V
nano:RESET
nano:GND.1
nano:VIN
nano:12.2
nano:5V.2
nano:13.2
nano:11.2
nano:RESET.3
nano:GND.3
sr3:Q1
sr3:Q2
sr3:Q3
sr3:Q4
sr3:Q5
sr3:Q6
sr3:Q7
sr3:GND
sr3:Q7S
sr3:MR
sr3:SHCP
sr3:STCP
sr3:OE
sr3:DS
sr3:Q0
sr3:VCC
sr4:Q1
sr4:Q2
sr4:Q3
sr4:Q4
sr4:Q5
sr4:Q6
sr4:Q7
sr4:GND
sr4:Q7S
sr4:MR
sr4:SHCP
sr4:STCP
sr4:OE
sr4:DS
sr4:Q0
sr4:VCC
led17:A
led17:C
led18:A
led18:C
led19:A
led19:C
led20:A
led20:C
led21:A
led21:C
led22:A
led22:C
led23:A
led23:C
led24:A
led24:C
led25:A
led25:C
led26:A
led26:C
led27:A
led27:C
led28:A
led28:C
led29:A
led29:C
led30:A
led30:C
led31:A
led31:C
led32:A
led32:C
r1:1
r1:2
btn2:1.l
btn2:2.l
btn2:1.r
btn2:2.r
btn1:1.l
btn1:2.l
btn1:1.r
btn1:2.r
btn3:1.l
btn3:2.l
btn3:1.r
btn3:2.r
btn5:1.l
btn5:2.l
btn5:1.r
btn5:2.r
btn6:1.l
btn6:2.l
btn6:1.r
btn6:2.r
btn7:1.l
btn7:2.l
btn7:1.r
btn7:2.r
btn8:1.l
btn8:2.l
btn8:1.r
btn8:2.r
btn9:1.l
btn9:2.l
btn9:1.r
btn9:2.r
btn10:1.l
btn10:2.l
btn10:1.r
btn10:2.r
btn11:1.l
btn11:2.l
btn11:1.r
btn11:2.r
btn12:1.l
btn12:2.l
btn12:1.r
btn12:2.r
btn13:1.l
btn13:2.l
btn13:1.r
btn13:2.r
btn14:1.l
btn14:2.l
btn14:1.r
btn14:2.r
btn15:1.l
btn15:2.l
btn15:1.r
btn15:2.r
btn16:1.l
btn16:2.l
btn16:1.r
btn16:2.r
btn17:1.l
btn17:2.l
btn17:1.r
btn17:2.r
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r5:1
r5:2
r6:1
r6:2
r7:1
r7:2
r8:1
r8:2
r9:1
r9:2
r10:1
r10:2
r11:1
r11:2
r12:1
r12:2
r13:1
r13:2
r14:1
r14:2
r15:1
r15:2
r16:1
r16:2
r17:1
r17:2
sr1:PL
sr1:CP
sr1:D4
sr1:D5
sr1:D6
sr1:D7
sr1:Q7_N
sr1:GND
sr1:Q7
sr1:DS
sr1:D0
sr1:D1
sr1:D2
sr1:D3
sr1:CE
sr1:VCC
sr2:PL
sr2:CP
sr2:D4
sr2:D5
sr2:D6
sr2:D7
sr2:Q7_N
sr2:GND
sr2:Q7
sr2:DS
sr2:D0
sr2:D1
sr2:D2
sr2:D3
sr2:CE
sr2:VCC
pot1:GND
pot1:SIG
pot1:VCC
pot2:GND
pot2:SIG
pot2:VCC
btn4:1.l
btn4:2.l
btn4:1.r
btn4:2.r
btn18:1.l
btn18:2.l
btn18:1.r
btn18:2.r
btn19:1.l
btn19:2.l
btn19:1.r
btn19:2.r