|
| 1 | +// Basic voice changer code. This version is specific to the Adafruit |
| 2 | +// MONSTER M4SK board using a PDM microphone. Connect an amplified speaker |
| 3 | +// or headphones to the M4SK audio jack. "Inner" button raises pitch, |
| 4 | +// "outer" button lowers pitch, center button resets pitch to 1:1. |
| 5 | +// SCREENS ARE OFF BY DEFAULT. THIS IS NORMAL. |
| 6 | + |
| 7 | +#include <Adafruit_seesaw.h> |
| 8 | + |
| 9 | +// The code in this file just sets up seesaw and handles button presses. |
| 10 | +// All the voice-handling stuff is in the other tab, accessed through |
| 11 | +// these functions and variables: |
| 12 | +extern bool voiceSetup(void); |
| 13 | +extern float voicePitch(float p); |
| 14 | +extern void voiceGain(float g); |
| 15 | +extern volatile uint16_t voiceLastReading; |
| 16 | + |
| 17 | +Adafruit_seesaw seesaw; |
| 18 | +#define SEESAW_BACKLIGHT_PIN 5 // Left eye TFT backlight |
| 19 | +#define BACKLIGHT_PIN 21 // Right eye TFT backlight |
| 20 | +#define SPEAKER_ENABLE_PIN 20 // Set LOW to enable speaker out |
| 21 | + |
| 22 | +// Crude error handler. Prints message to Serial Monitor, blinks LED. |
| 23 | +static void fatal(const char *message, uint16_t blinkDelay) { |
| 24 | + Serial.println(message); |
| 25 | + for(bool ledState = HIGH;; ledState = !ledState) { |
| 26 | + digitalWrite(LED_BUILTIN, ledState); |
| 27 | + delay(blinkDelay); |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +void setup() { |
| 32 | + pinMode(SPEAKER_ENABLE_PIN, OUTPUT); |
| 33 | + digitalWrite(SPEAKER_ENABLE_PIN, HIGH); // Speaker OFF |
| 34 | + |
| 35 | + Serial.begin(115200); |
| 36 | + //while(!Serial); |
| 37 | + |
| 38 | + // Screens off for now, not used by this sketch |
| 39 | + pinMode(BACKLIGHT_PIN, OUTPUT); |
| 40 | + analogWrite(BACKLIGHT_PIN, 0); |
| 41 | + if(!seesaw.begin()) fatal("Seesaw init fail", 1000); |
| 42 | + seesaw.analogWrite(SEESAW_BACKLIGHT_PIN, 0); |
| 43 | + |
| 44 | + if(!voiceSetup()) fatal("Voice init fail", 250); |
| 45 | + digitalWrite(SPEAKER_ENABLE_PIN, LOW); // Speaker on |
| 46 | + |
| 47 | + // Configure Seesaw pins 9,10,11 as inputs |
| 48 | + seesaw.pinModeBulk(0b111000000000, INPUT_PULLUP); |
| 49 | +} |
| 50 | + |
| 51 | +static float pitch = 1.0; |
| 52 | + |
| 53 | +void loop() { |
| 54 | + uint32_t buttonState = seesaw.digitalReadBulk(0b111000000000); |
| 55 | + if((buttonState & 0b111000000000) != 0b111000000000) { |
| 56 | + if( !(buttonState & 0b001000000000)) { // Seesaw pin 9 |
| 57 | + Serial.println("Higher"); |
| 58 | + pitch = voicePitch(pitch * 1.1); |
| 59 | + } else if(!(buttonState & 0b010000000000)) { // Seesaw pin 10 |
| 60 | + Serial.println("1:1"); |
| 61 | + pitch = voicePitch(1.0); |
| 62 | + } else if(!(buttonState & 0b100000000000)) { // Seesaw pin 11 |
| 63 | + Serial.println("Lower"); |
| 64 | + pitch = voicePitch(pitch * 0.9); |
| 65 | + } |
| 66 | + Serial.println(pitch); |
| 67 | + while(seesaw.digitalReadBulk(0b111000000000) != 0b111000000000); |
| 68 | + } |
| 69 | +} |
0 commit comments