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