|
| 1 | +#include "Modulino.h" |
| 2 | + |
| 3 | +#define FREQUENCY_DEFAULT 4000 //Starting frequency in hertz |
| 4 | +#define FREQUENCY_LOWEST 1 //Minimum frequency in hertz |
| 5 | +#define FREQUENCY_HIGHEST 20000 //Maximum frequency in hertz |
| 6 | +#define FREQUENCY_STEP 200 //How much the frequency changes when the knob is turned by 1 click |
| 7 | +#define CYCLE_WAIT_MS 25 //Amount of added delay between loop iterations |
| 8 | + |
| 9 | +//Create objects for modulinos |
| 10 | +ModulinoKnob encoder; |
| 11 | +ModulinoBuzzer buzzer; |
| 12 | + |
| 13 | +//Declare variables for the sketch |
| 14 | +int frequency = FREQUENCY_DEFAULT; //frequency which will be played by the buzzer |
| 15 | +int actual_value = FREQUENCY_DEFAULT; //Current encoder value. |
| 16 | +int previous_value = actual_value; //Previous encoder value. |
| 17 | +bool mute_status = false; //To use the encoder button as an MUTE/PLAY button. |
| 18 | + |
| 19 | +String frequency_text = String(frequency) + " Hz"; |
| 20 | +String encoder_text = "(encoder value " + String(actual_value) + ")"; |
| 21 | +String mute_text = "- now playing"; |
| 22 | + |
| 23 | +void setup() { |
| 24 | + Serial.begin(115200); |
| 25 | + |
| 26 | + //Inizialise modulinos |
| 27 | + Modulino.begin(); |
| 28 | + buzzer.begin(); |
| 29 | + encoder.begin(); |
| 30 | + |
| 31 | + //Wait for modulino encoder inizialization and set starting value |
| 32 | + delay(100); |
| 33 | + encoder.set(FREQUENCY_DEFAULT); |
| 34 | +} |
| 35 | + |
| 36 | +void loop() { |
| 37 | + //Acquire encoder value |
| 38 | + actual_value = encoder.get(); |
| 39 | + |
| 40 | + /*Check if the encoder value has changed with respect to previous |
| 41 | + acquisition, if so increase/decrease the encoder value by the |
| 42 | + desired FREQUENCY_STEP, then update the previous value variable |
| 43 | + */ |
| 44 | + if (previous_value != actual_value) { |
| 45 | + if (actual_value > previous_value) { |
| 46 | + actual_value = actual_value + FREQUENCY_STEP; |
| 47 | + } else { |
| 48 | + actual_value = actual_value - FREQUENCY_STEP; |
| 49 | + } |
| 50 | + |
| 51 | + if (actual_value < FREQUENCY_LOWEST) { |
| 52 | + actual_value = FREQUENCY_LOWEST; |
| 53 | + encoder.set(FREQUENCY_LOWEST); |
| 54 | + } |
| 55 | + if (actual_value > FREQUENCY_HIGHEST) { |
| 56 | + actual_value = FREQUENCY_HIGHEST; |
| 57 | + encoder.set(FREQUENCY_HIGHEST); |
| 58 | + } |
| 59 | + encoder.set(actual_value); |
| 60 | + previous_value = actual_value; |
| 61 | + } |
| 62 | + |
| 63 | + //Check if encoder has been pressed. In that case toggle |
| 64 | + //mute status. Update mute text to diplay the MUTE/PLAY on serial |
| 65 | + if (encoder.isPressed()) { |
| 66 | + mute_status = !mute_status; |
| 67 | + if (mute_status == false) { |
| 68 | + mute_text = "- now playing"; |
| 69 | + } else { |
| 70 | + mute_text = "- muted"; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + //Play the frequency on the buzzer only if the system is not muted. |
| 75 | + if (mute_status == false) { |
| 76 | + buzzer.tone(actual_value, 1000); |
| 77 | + } else { |
| 78 | + buzzer.noTone(); |
| 79 | + } |
| 80 | + |
| 81 | + //Convert the frequency value to string and add Hz |
| 82 | + frequency_text = String(frequency) + " Hz"; |
| 83 | + |
| 84 | + //Convert the encoder value to string |
| 85 | + encoder_text = "(encoder value " + String(actual_value) + ")"; |
| 86 | + |
| 87 | + //Print each of the sensor values on serial |
| 88 | + Serial.print(frequency_text + " "); |
| 89 | + Serial.print(encoder_text + " "); |
| 90 | + Serial.println(mute_text); |
| 91 | + |
| 92 | + delay(CYCLE_WAIT_MS); |
| 93 | +} |
0 commit comments