Passive buzzer and mouse control
Passive buzzer and mouse control
What I wanted to learn, and why it interested me: I wanted to learn how to output sound and control my mouse through a arduino micro just because I've never done anything in those two fields.
Final outcome:
1. a data logger that types the amount of times the button is pressed.
2. sound with varying pitches being played whenever the button is pressed, while moving the mouse around randomly.
Images of final creative/exploratory output
When pin 3 is connected to ground, if the button is pressed, it types "You pressed the button x times."
2. When pin 3 is connected to ground, if the button is pressed, it plays a pitch and moves the mouse around randomly. Frequency of the sound climbs and falls based on an increment of 40Hz, within a range of 500Hz to 1500Hz
Process images from development towards creative/exploratory output
Soldering the Arduino micrro pins
Wiring up the button with 10k resistor to 5V and pin 4, and connecting the passive buzzer to pin 5 and ground.
Process and reflection:
Before the project, I saw Tim's keyboard interaction during presentation and I thought that was something new and cool, so I wanted to learn how to do that. This was how I started exploring the process of making a button that counts itself as it is being pressed. It was confusing to wire the Arduino Micro since it was my first time using it. I learned that Arduino Uno cannot control mouse and keyboard no matter how we code it.
Unexpectedly, figuring out the wiring and soldering connections was the hardest part, and the code was just copying and pasting from the example code. Therefore, I started exploring something different and more fun.
I thought combining sound and button would be fun since I kind of expect a sound when I press a button. I then connected the buzzer and made it make a sound whenever the button is pressed. I learned that passive buzzer changes pitch and active buzzer changes volumn. Later, to create more interaction, I made the mouse move when the sound is played, and continue pressing the button will make the sound play like a siren.
I understood how to use more components through this project and realized that storytelling would make the interaction much more engaging.
Technical details
Electrical schematic
#include <Mouse.h>
#include <Keyboard.h>
#include <KeyboardLayout.h>
#include <Keyboard_da_DK.h>
#include <Keyboard_de_DE.h>
#include <Keyboard_es_ES.h>
#include <Keyboard_fr_FR.h>
#include <Keyboard_hu_HU.h>
#include <Keyboard_it_IT.h>
#include <Keyboard_pt_PT.h>
#include <Keyboard_sv_SE.h>
/*
Basic "keyboard" code demonstrating use of a wire connection to prevent
sending keystrokes when you don't want it to
Wiring: connect pin 3 to ground with a jumper; Arduino will only send
keystrokes to the connected computer while that connection is in place
*/
#include "Keyboard.h"
#include "Mouse.h"
const int KEYBOARD_BAILOUT_PIN = 3;
const int BUTTON_PIN = 4;
const int BUZZER_PIN = 5;
int frequency = 500; // (Gemini)
int step = 40; // (Gemini)
void setup() {
pinMode(KEYBOARD_BAILOUT_PIN, INPUT_PULLUP);
pinMode(BUTTON_PIN, INPUT);
pinMode(BUZZER_PIN, OUTPUT);
Keyboard.begin();
Mouse.begin();
}
void loop() {
int bailoutState = digitalRead(KEYBOARD_BAILOUT_PIN);
if (bailoutState == LOW) {
// Check if the button is pressed
if (digitalRead(BUTTON_PIN) == HIGH) {
tone(BUZZER_PIN, frequency); // (Gemini)
// Increment frequency for the next loop
frequency += step; // (Gemini)
// Reverse direction if we hit the limits (500Hz to 1500Hz)(Gemini)
if (frequency >= 1500 || frequency <= 500) {
step = -step;
}
int bigX = random(-127, 128);
int bigY = random(-127, 128);
Mouse.move(bigX, bigY, 0);
// Small delay to prevent the CPU from screaming // (Gemini)
delay(100);
} else {
noTone(BUZZER_PIN); // Ensure silence when button is released // (Gemini)
}
} else {
// If safety pin is pulled, make sure no sound is stuck on // (Gemini)
noTone(BUZZER_PIN);
}
}