diff --git a/examples/BuzzerSweep/BuzzerSweep.ino b/examples/BuzzerSweep/BuzzerSweep.ino new file mode 100644 index 0000000..e933394 --- /dev/null +++ b/examples/BuzzerSweep/BuzzerSweep.ino @@ -0,0 +1,36 @@ +#include "Modulino.h" + +#define FREQUENCY_LOWEST 100 //Lowest frequency in hertz +#define FREQUENCY_HIGHEST 8000 //Highest frequency in hertz +#define FREQUENCY_STEP 10 //Each iteration changes frequency by this number + +ModulinoBuzzer buzzer; + +int frequency = FREQUENCY_LOWEST; +String frequency_text = String(frequency) + " Hz"; + +void setup() { + Serial.begin(115200); + + //Inizialise modulinos + Modulino.begin(); + buzzer.begin(); + + delay(100); +} + +void loop() { + //Increase played frequency by the desired step. + //Start again if highest frequency has been played + frequency = frequency + FREQUENCY_STEP; + if (frequency > FREQUENCY_HIGHEST) { + frequency = FREQUENCY_LOWEST; + } + + //Play the frequency on the buzzer + buzzer.tone(frequency, 1000); + + //Convert the frequency value to string and add Hz + frequency_text = String(frequency) + " Hz"; + Serial.println(frequency_text); +} diff --git a/examples/EncoderSound/EncoderSound.ino b/examples/EncoderSound/EncoderSound.ino new file mode 100644 index 0000000..11eb097 --- /dev/null +++ b/examples/EncoderSound/EncoderSound.ino @@ -0,0 +1,93 @@ +#include "Modulino.h" + +#define FREQUENCY_DEFAULT 4000 //Starting frequency in hertz +#define FREQUENCY_LOWEST 1 //Minimum frequency in hertz +#define FREQUENCY_HIGHEST 20000 //Maximum frequency in hertz +#define FREQUENCY_STEP 200 //How much the frequency changes when the knob is turned by 1 click +#define CYCLE_WAIT_MS 25 //Amount of added delay between loop iterations + +//Create objects for modulinos +ModulinoKnob encoder; +ModulinoBuzzer buzzer; + +//Declare variables for the sketch +int frequency = FREQUENCY_DEFAULT; //frequency which will be played by the buzzer +int actual_value = FREQUENCY_DEFAULT; //Current encoder value. +int previous_value = actual_value; //Previous encoder value. +bool mute_status = false; //To use the encoder button as an MUTE/PLAY button. + +String frequency_text = String(frequency) + " Hz"; +String encoder_text = "(encoder value " + String(actual_value) + ")"; +String mute_text = "- now playing"; + +void setup() { + Serial.begin(115200); + + //Inizialise modulinos + Modulino.begin(); + buzzer.begin(); + encoder.begin(); + + //Wait for modulino encoder inizialization and set starting value + delay(100); + encoder.set(FREQUENCY_DEFAULT); +} + +void loop() { + //Acquire encoder value + actual_value = encoder.get(); + + /*Check if the encoder value has changed with respect to previous + acquisition, if so increase/decrease the encoder value by the + desired FREQUENCY_STEP, then update the previous value variable + */ + if (previous_value != actual_value) { + if (actual_value > previous_value) { + actual_value = actual_value + FREQUENCY_STEP; + } else { + actual_value = actual_value - FREQUENCY_STEP; + } + + if (actual_value < FREQUENCY_LOWEST) { + actual_value = FREQUENCY_LOWEST; + encoder.set(FREQUENCY_LOWEST); + } + if (actual_value > FREQUENCY_HIGHEST) { + actual_value = FREQUENCY_HIGHEST; + encoder.set(FREQUENCY_HIGHEST); + } + encoder.set(actual_value); + previous_value = actual_value; + } + + //Check if encoder has been pressed. In that case toggle + //mute status. Update mute text to diplay the MUTE/PLAY on serial + if (encoder.isPressed()) { + mute_status = !mute_status; + if (mute_status == false) { + mute_text = "- now playing"; + } else { + mute_text = "- muted"; + } + } + + //Play the frequency on the buzzer only if the system is not muted. + if (mute_status == false) { + buzzer.tone(actual_value, 1000); + } else { + buzzer.noTone(); + } + + //Convert the frequency value to string and add Hz + frequency_text = String(frequency) + " Hz"; + + //Convert the encoder value to string + encoder_text = "(encoder value " + String(actual_value) + ")"; + + //Print each of the sensor values on serial + Serial.print(frequency_text + " "); + Serial.print(encoder_text + " "); + Serial.println(mute_text); + + delay(CYCLE_WAIT_MS); +} diff --git a/examples/LedChain/LedChain.ino b/examples/LedChain/LedChain.ino new file mode 100644 index 0000000..22d724c --- /dev/null +++ b/examples/LedChain/LedChain.ino @@ -0,0 +1,128 @@ +#include "Modulino.h" + +#define NUMBER_OF_BOARDS 3 //How many boards +#define NUMBER_OF_LED_PER_BOARD 8 //How many leds per board. +#define FIRST_ADDRESS 10 //First address of the chain. Address list should have no missing numbers +#define LAST_ADDRESS 12 //Last address of the chain. Address list should have no missing numbers + +//Define a new, custom color +ModulinoColor YELLOW(255, 75, 0); + +//Allocate memory for as many boards as desired +ModulinoPixels* boards_chain = (ModulinoPixels*)malloc(sizeof(ModulinoPixels) * NUMBER_OF_BOARDS); + +int i = 0; //Defined to count stuff + +void setup() { + Modulino.begin(); //Initialize library + + //Inizialize each object of the boards with its own address + for (i = 0; i < NUMBER_OF_BOARDS; i++) { + boards_chain[i] = ModulinoPixels(FIRST_ADDRESS + i); + boards_chain[i].begin(); + } + //Set all leds at brigthness 0, shutting them off + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, RED, 0); +} + +void loop() { + //Switch on all leds with the same color at brightness 10 + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, YELLOW, 10); + delay(200); + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, WHITE, 10); + delay(200); + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, VIOLET, 10); + delay(200); + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, RED, 10); + delay(200); + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, GREEN, 10); + delay(200); + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, BLUE, 10); + delay(200); + + //From the last led to the first one, one by one, switch color to red and brightness to 100 + for (i = (NUMBER_OF_BOARDS * NUMBER_OF_LED_PER_BOARD - 1); i >= 0; i--) { + ledOn(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, RED, 100, i); + delay(25); + } + + //From the first led to the last one, one by one, switch color to white and brightness to 10 + //shut off all the others so that one led is travelling the chain + for (i = 0; i < (NUMBER_OF_BOARDS * NUMBER_OF_LED_PER_BOARD); i++) { + ledOnOthersOff(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, WHITE, 10, i); + delay(25); + } + + //Breathe all the leds in blue color + for (i = 0; i <= 100; i = i + 1) { + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, BLUE, i); + delay(10); + } + for (i = 100; i >= 0; i = i - 1) { + ledAllsame(boards_chain, NUMBER_OF_BOARDS, NUMBER_OF_LED_PER_BOARD, BLUE, i); + delay(10); + } +} + +//Switch ON all LEDs on all boards +void ledAllsame(ModulinoPixels* board_array, int number_of_boards, int number_of_leds, ModulinoColor rgb, int brightness) { + int i_board = 0; //Local variable to count each board + int i_led = 0; //Local variable to count each led on each board + //For each board + for (int i_board = 0; i_board < number_of_boards; i_board++) { + //For each led on the current board + for (i_led = 0; i_led < number_of_leds; i_led++) { + //Set the current led on the current board to desired color and brightness + board_array[i_board].set(i_led, rgb, brightness); + } + //Update the current board status + board_array[i_board].show(); + } +} + +//Switch ON just the desired LED, do nothing to the others +//Considers the chain of boards as a single, long board +void ledOn(ModulinoPixels* board_array, int number_of_boards, + int number_of_leds, ModulinoColor rgb, int brightness, int ledOn) { + int i_board = 0; //Local variable to count each board + int i_led = 0; //Local variable to count each led on each board + int chain_led = 0; //Local variable to check if the desired led is reached + //For each board + for (int i_board = 0; i_board < number_of_boards; i_board++) { + //For each led on the current board + for (i_led = 0; i_led < number_of_leds; i_led++) { + //Set the current led on the current board to desired color and brightness + if (chain_led == ledOn) { + board_array[i_board].set(i_led, rgb, brightness); + } + chain_led++; + } + //Update the current board status + board_array[i_board].show(); + } +} + +//Switch ON just the desired LED, shut off the others +//Considers the chain of boards as a single, long board +void ledOnOthersOff(ModulinoPixels* board_array, int number_of_boards, + int number_of_leds, ModulinoColor rgb, int brightness, int ledOn) { + int i_board = 0; //Local variable to count each board + int i_led = 0; //Local variable to count each led on each board + int chain_led = 0; //Local variable to check if the desired led is reached + //For each board + for (int i_board = 0; i_board < number_of_boards; i_board++) { + //For each led on the current board + for (i_led = 0; i_led < number_of_leds; i_led++) { + //Set the current led on the current board to desired color and brightness + if (chain_led == ledOn) { + board_array[i_board].set(i_led, rgb, brightness); + } else { + //Shut off the other leds + board_array[i_board].set(i_led, RED, 0); + } + chain_led++; + } + //Update the current board status + board_array[i_board].show(); + } +}