Arduino PISOのテスト
何かが間違っているようでうまく動かないため、やり直しが必要な様です。
#define DATA_PIN 2 #define LOAD_PIN 3 #define CLOCK_PIN 4
#define SIPO_SDI 13 #define SIPO_LAT 12 #define SIPO_CLK 11
int bitmap[] = {
0b1000000000000000,
0b0100000000000000,
0b0010000000000000,
0b0001000000000000,
0b0000100000000000,
0b0000010000000000,
0b0000001000000000,
0b0000000100000000,
0b0000000010000000,
0b0000000001000000,
0b0000000000100000,
0b0000000000010000,
0b0000000000001000,
0b0000000000000100,
0b0000000000000010,
0b0000000000000001,
};
int delayValue = 100;
int buttonStates = 0;
int prevButtonStates = 0;
int step = 0;
bool seqStat = false;
void setup() {
pinMode(SIPO_SDI, OUTPUT);
pinMode(SIPO_LAT, OUTPUT);
pinMode(SIPO_CLK, OUTPUT);
pinMode(DATA_PIN, INPUT);
pinMode(LOAD_PIN, OUTPUT);
pinMode(CLOCK_PIN, OUTPUT);
digitalWrite(LOAD_PIN, HIGH);
digitalWrite(CLOCK_PIN, LOW);
Serial.begin(9600);
}
void loop() {
readButtons();
handleButtons();
stepRun();
}
void readButtons() {
digitalWrite(LOAD_PIN, LOW);
delayMicroseconds (15);
digitalWrite(LOAD_PIN, HIGH);
prevButtonStates = buttonStates; // 前回のボタン状態を更新
buttonStates = 0;
for (int i = 0; i < 24; i++) {
digitalWrite(CLOCK_PIN, LOW);
delayMicroseconds(15);
int bit = digitalRead(DATA_PIN);
buttonStates |= (bit << i);
digitalWrite(CLOCK_PIN, HIGH);
delayMicroseconds(15);
}
Serial.print("buttonStates: ");
for (int i = 23; i >= 0; i--) {
Serial.print((buttonStates >> i) & 1);
}
}
void handleButtons() {
if (isButtonPressed(0)) toggleSeqStat();
if (isButtonPressed(1)) stepControl(+1);
if (isButtonPressed(2)) stepControl(-1);
if (isButtonPressed(3)) stepControl(-1);
if (isButtonPressed(4)) stepControl(-1);
if (isButtonPressed(5)) stepControl(-1);
if (isButtonPressed(6)) stepControl(-1);
if (isButtonPressed(7)) stepControl(-1);
if (isButtonPressed(8)) stepControl(-1);
if (isButtonPressed(9)) stepControl(-1);
if (isButtonPressed(10)) stepControl(-1);
if (isButtonPressed(11)) stepControl(-1);
if (isButtonPressed(12)) stepControl(-1);
if (isButtonPressed(13)) stepControl(-1);
if (isButtonPressed(14)) stepControl(-1);
if (isButtonPressed(15)) stepControl(-1);
if (isButtonPressed(16)) stepControl(0);
if (isButtonPressed(17)) stepControl(0);
if (isButtonPressed(18)) stepControl(0);
if (isButtonPressed(19)) stepControl(0);
if (isButtonPressed(20)) stepControl(0);
if (isButtonPressed(21)) stepControl(0);
if (isButtonPressed(22)) stepControl(0);
if (isButtonPressed(23)) stepControl(0);
}
bool isButtonPressed(int buttonIndex) {
// ボタンが押されているかつ、前回は押されていなかったかを確認
return (buttonStates & (1 << buttonIndex)) && !(prevButtonStates & (1 << buttonIndex));
}
void toggleSeqStat() {
seqStat = !seqStat;
}
void stepControl(int stepMove) {
step += stepMove;
if (step > 15) {
step = 0;
}
if (step < 0) {
step = 15;
}
ledOut();
}
void stepRun() {
if (seqStat) {
step++;
if (step > 15) {
step = 0;
}
ledOut();
delay(delayValue);
}
}
void ledOut() {
digitalWrite(SIPO_LAT, LOW);
shiftOut(SIPO_SDI, SIPO_CLK, LSBFIRST, bitmap[step]);
shiftOut(SIPO_SDI, SIPO_CLK, MSBFIRST, bitmap[step] >> 8);
digitalWrite(SIPO_LAT, HIGH);
}
この記事が気に入ったらサポートをしてみませんか?