DIY "beep beep" clock module (based on arduino)
Mar 25, 2020 11:03:14 GMT
admin, NightMachines, and 7 more like this
Post by bea5t on Mar 25, 2020 11:03:14 GMT
DIY "beep beep" clock module (based on arduino)
My intention: I didn't want to waste an LFO on this task.
Features:
I think this is a great project for beginners.
Pictures:
Circuit diagram:
Video:
Coming soon
Code (quick and dirty):
You are welcome to customize it.
My intention: I didn't want to waste an LFO on this task.
Features:
- upper controller: select BPM
- lower controller: select pulse width
- beat divider (1/2/4): right port 1-3
- blue switch or left port 1: tab BPM
- red switch or left port 2: reset counter (signal to right port 4)
I think this is a great project for beginners.
Pictures:
Circuit diagram:
Video:
Coming soon
Code (quick and dirty):
You are welcome to customize it.
#include <SimpleTimer.h>
#include <Bounce2.h>
#include <CircularBuffer.h>
#define PIN_OUT_1 2
#define PIN_OUT_2 3
#define PIN_OUT_4 4
#define PIN_OUT_RESET 5
#define PIN_TAB_BPM 10
#define PIN_TAB_RESET 11
#define PIN_ROT_BPM A0
#define PIN_ROT_DUR A1
#define TAB_QUEUE 5
#define TAB_TIMEOUT 4000
SimpleTimer timer;
Bounce TAB_RESET = Bounce(PIN_TAB_RESET, 10);
Bounce TAB_BPM = Bounce(PIN_TAB_BPM, 10);
CircularBuffer<long, TAB_QUEUE> TAB_BUFFER;
bool cycle_initiated = false;
bool bpm_tab = false;
int min_bpm = 40;
int max_bpm = 300;
int cycle_count = 0;
int bpm;
float bpm_queue;
float pulse_width;
long tab_now;
long tab_last;
int tab_time;
int rot_last = min_bpm;
void setup()
{
pinMode(PIN_OUT_1, OUTPUT); // COUNT 1
pinMode(PIN_OUT_2, OUTPUT); // COUNT 2
pinMode(PIN_OUT_4, OUTPUT); // COUNT 4
pinMode(PIN_OUT_RESET, OUTPUT); // RESET
pinMode(PIN_TAB_BPM, INPUT_PULLUP); // TAB
pinMode(PIN_TAB_RESET, INPUT_PULLUP); // RESET
}
void cycle_off()
{
if (cycle_count == 99)
{
delay(500);
cycle_count = 0;
}
digitalWrite(PIN_OUT_1, LOW);
digitalWrite(PIN_OUT_2, LOW);
digitalWrite(PIN_OUT_4, LOW);
digitalWrite(PIN_OUT_RESET, LOW);
cycle_count++;
if (cycle_count == 4)
{
cycle_count = 0;
}
}
void set_outputs()
{
switch (cycle_count)
{
case 0:
digitalWrite(PIN_OUT_1, HIGH);
break;
case 1:
digitalWrite(PIN_OUT_1, HIGH);
digitalWrite(PIN_OUT_2, HIGH);
break;
case 2:
digitalWrite(PIN_OUT_1, HIGH);
break;
case 3:
digitalWrite(PIN_OUT_1, HIGH);
digitalWrite(PIN_OUT_2, HIGH);
digitalWrite(PIN_OUT_4, HIGH);
break;
case 99:
digitalWrite(PIN_OUT_RESET, HIGH);
break;
}
}
void cycle_on()
{
int rot_bpm = analogRead(PIN_ROT_BPM);
int rot_pulse = analogRead(PIN_ROT_DUR);
if (bpm_tab == true)
{
bpm = bpm_queue;
}
else
{
if (abs(rot_last - rot_bpm) > 1)
{
bpm = map(rot_bpm, 0, 1023, min_bpm, max_bpm);
}
}
rot_last = rot_bpm;
pulse_width = map(rot_pulse, 5, 1023, 1, 95);
int cycletime = (60000 / bpm);
float cycle_start = cycletime;
float cycle_stop = (cycletime *(pulse_width / 100));
timer.setTimeout(cycle_start, cycle_on);
timer.setTimeout(cycle_stop, cycle_off);
set_outputs();
}
void loop()
{
TAB_RESET.update();
TAB_BPM.update();
if (cycle_initiated == false)
{
cycle_on();
cycle_initiated = true;
}
timer.run();
if (TAB_RESET.rose())
{
cycle_count = 99;
}
long bmp_tmp;
if (TAB_BPM.rose())
{
tab_now = millis();
tab_time = tab_now - tab_last;
tab_last = tab_now;
TAB_BUFFER.push(tab_time);
for (int i = 0; i < TAB_BUFFER.size(); i++) {}
bmp_tmp = 0;
for (int i = 0; i < TAB_BUFFER.size(); i++)
{
bmp_tmp = bmp_tmp + TAB_BUFFER[i];
}
if (TAB_BUFFER.size() == TAB_QUEUE)
{
bpm_queue = 60000 / (bmp_tmp / TAB_BUFFER.size());
if (bpm_queue<= max_bpm && bpm_queue >= min_bpm)
{
bpm_tab = true;
}
else
{
bpm_tab = false;
}
}
}
tab_now = millis();
if (tab_now - tab_last > 4000 && bpm_tab == true)
{
bpm_tab = 0;
}
}