Instantly share code, notes, and snippets.
Created
March 31, 2024 01:59
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save ericfont/0fa054be203d4c7708c4f3821a641a3a to your computer and use it in GitHub Desktop.
InterruptButton.ino
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Teensy 4.1 code to get input from 3 pins and output square wave frequency based on each. | |
#include <Audio.h> | |
#include <Wire.h> | |
#include <SPI.h> | |
#include <SD.h> | |
#include <SerialFlash.h> | |
// GUItool: begin automatically generated code | |
AudioSynthWaveform waveform_note; //xy=433,701 | |
AudioEffectEnvelope envelope_note; //xy=628,699 | |
AudioOutputMQS mqs1; //xy=830,700 | |
AudioConnection patchCord1(waveform_note, envelope_note); | |
AudioConnection patchCord2(envelope_note, 0, mqs1, 0); | |
AudioConnection patchCord3(envelope_note, 0, mqs1, 1); | |
// GUItool: end automatically generated code | |
unsigned long last_timestamp_pin20_press = 0; | |
unsigned long last_timestamp_pin20_release = 0; | |
bool last_state_pin20 = LOW; | |
unsigned long last_timestamp_pin17_press = 0; | |
unsigned long last_timestamp_pin17_release = 0; | |
bool last_state_pin17 = LOW; | |
unsigned long last_timestamp_pin14_press = 0; | |
unsigned long last_timestamp_pin14_release = 0; | |
bool last_state_pin14 = LOW; | |
void note_press( int pin ) { | |
Serial.print("note_press pin "); | |
Serial.println(pin); | |
waveform_note.amplitude(0.75); | |
waveform_note.frequency(pin * 100); | |
envelope_note.noteOn(); | |
} | |
void note_release( int pin ) { | |
Serial.print("note_release pin "); | |
Serial.println(pin); | |
Serial.println(""); | |
envelope_note.noteOff(); | |
} | |
void interruptChangePin20() { | |
unsigned long timestamp = millis(); | |
bool pinvalue = digitalRead(20); | |
if( last_state_pin20 == HIGH && pinvalue == LOW ) // button release (pin transitions to LOW) | |
{ | |
if( timestamp > last_timestamp_pin20_release + 100 ) //check more than so many milliseconds since last event | |
{ | |
note_release(20); | |
last_timestamp_pin20_release = timestamp; | |
last_state_pin20 = LOW; | |
} | |
} | |
else if ( last_state_pin20 == LOW && pinvalue == HIGH ) { // button press (pin transitions HIGH) | |
if( timestamp > last_timestamp_pin20_press + 100 ) //check more than so many milliseconds since last event | |
{ | |
note_press(20); | |
last_timestamp_pin20_press = timestamp; | |
last_state_pin20 = HIGH; | |
} | |
} | |
} | |
void interruptChangePin17() { | |
unsigned long timestamp = millis(); | |
bool pinvalue = digitalRead(17); | |
if( last_state_pin17 == HIGH && pinvalue == LOW ) // button release (pin transitions to LOW) | |
{ | |
if( timestamp > last_timestamp_pin17_release + 100 ) //check more than so many milliseconds since last event | |
{ | |
note_release(17); | |
last_timestamp_pin17_release = timestamp; | |
last_state_pin17 = LOW; | |
} | |
} | |
else if ( last_state_pin17 == LOW && pinvalue == HIGH ) { // button press (pin transitions HIGH) | |
if( timestamp > last_timestamp_pin17_press + 100 ) //check more than so many milliseconds since last event | |
{ | |
note_press(17); | |
last_timestamp_pin17_press = timestamp; | |
last_state_pin17 = HIGH; | |
} | |
} | |
} | |
void interruptChangePin14() { | |
unsigned long timestamp = millis(); | |
bool pinvalue = digitalRead(14); | |
if( last_state_pin14 == HIGH && pinvalue == LOW ) // button release (pin transitions to LOW) | |
{ | |
if( timestamp > last_timestamp_pin14_release + 100 ) //check more than so many milliseconds since last event | |
{ | |
note_release(14); | |
last_timestamp_pin14_release = timestamp; | |
last_state_pin14 = LOW; | |
} | |
} | |
else if ( last_state_pin14 == LOW && pinvalue == HIGH ) { // button press (pin transitions HIGH) | |
if( timestamp > last_timestamp_pin14_press + 100 ) //check more than so many milliseconds since last event | |
{ | |
note_press(14); | |
last_timestamp_pin14_press = timestamp; | |
last_state_pin14 = HIGH; | |
} | |
} | |
} | |
void setup() { | |
Serial.begin(38400); | |
AudioMemory(10); | |
pinMode(20, INPUT_PULLDOWN); | |
attachInterrupt(digitalPinToInterrupt(20), interruptChangePin20, CHANGE); | |
pinMode(17, INPUT_PULLDOWN); | |
attachInterrupt(digitalPinToInterrupt(17), interruptChangePin17, CHANGE); | |
pinMode(14, INPUT_PULLDOWN); | |
attachInterrupt(digitalPinToInterrupt(14), interruptChangePin14, CHANGE); | |
waveform_note.frequency(440); | |
waveform_note.amplitude(1.0); | |
waveform_note.begin(WAVEFORM_SINE); | |
} | |
void loop() | |
{ | |
// int status=0; | |
// Serial.println(status); | |
// delay(2500); | |
// status=1; | |
// Serial.println(status); | |
delay(100); | |
Serial.println(""); | |
} | |
Author
ericfont
commented
Mar 31, 2024
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment