Ardunioの割り込み処理が分かりません。
下記のプログラムを使い
割り込み処理ボタンを押し、割り込み処理によりvoid loop()の処理を停止し
再度ボタンを押すことでもう一度void loop()の作業を行うプログラムを作りたいです。
#include "pitches.h"
int melody[] = {NOTE_G5, NOTE_A5, NOTE_B5, NOTE_A5, NOTE_G5, NOTE_G5, NOTE_A5, NOTE_B5, NOTE_A5, NOTE_G5, NOTE_A5};
int tempo[] = {200, 200, 400, 100, 100, 70, 70, 70, 70, 70, 800};
int onkai[] = {NOTE_C4, NOTE_D4, NOTE_E4, NOTE_F4, NOTE_G4, NOTE_A4, NOTE_B4, NOTE_C5};
#define LEDC_CHANNEL_0 0
#define LEDC_TIMER_13_BIT 13
#define LEDC_BASE_FREQ 5000
#define BZZ 16
#define START_SW 4
#define R_LED 14
#define G_LED 5
#define DI_1() portENTER_CRITICAL_ISR(&mux_1);
#define EI_1() portEXIT_CRITICAL_ISR(&mux_1);
#define SERIAL_SPEED 115200
const byte SW1 = 32;
portMUX_TYPE mux_1 = portMUX_INITIALIZER_UNLOCKED;
int arrayM = sizeof melody / sizeof melody[0];
int arrayO = sizeof onkai / sizeof onkai[0];
void IRAM_ATTR handleInterrupt_1() {
DI_1();
EI_1();
}
void tone_n(int channel, int frq, int time_w) {
ledcWriteTone (channel, frq);
delay(time_w);
ledcWriteTone (channel, 0);
}
void setup() {
Serial.begin(115200);
pinMode(SW1, INPUT_PULLUP);
attachInterrupt (digitalPinToInterrupt(SW1), handleInterrupt_1, FALLING);
//
ledcSetup(LEDC_CHANNEL_0, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
ledcAttachPin(BZZ, LEDC_CHANNEL_0);
delay(500);
// GPIO設定
pinMode(START_SW, INPUT_PULLUP);
pinMode(R_LED, OUTPUT);
pinMode(G_LED, OUTPUT);
digitalWrite(R_LED, 0);
digitalWrite(G_LED, 1);
tone_n(LEDC_CHANNEL_0, onkai[0], 0);
}
void loop() {
int in;
in = digitalRead(START_SW);
if (in == 0) {
for ( int i = 0; i < arrayO; i++) {// C4~C5 if (in == 0){ Serial.println(i); digitalWrite(G_LED, 0); delay(980); digitalWrite(R_LED, 1); tone_n (LEDC_CHANNEL_0, onkai[i], 20); digitalWrite(R_LED, 0); } } delay(1000); if (in == 0){ digitalWrite(G_LED, 1); for (int i = 0; i < arrayM; i++) { tone_n (LEDC_CHANNEL_0, melody[i], tempo[i]); delay(80); }
}
}
}