1
MiniMo - simple firmware for momentary (electronic) switches (source code)
3
Fri, 01/31/2014 - 18:47
5
Lights with electronic switches are becoming quite popular, I also like them,
6
because they give more flexibility regarding the UI. Similar to MiniDrv, a
7
small firmware for clickies, I made a minimalistic firmware for momentary
8
buttons, named it MiniMo (MINImalistic driver for MOmentary switches) and
9
decided (not lightly; I hope people still buy some drivers from me so I can buy
10
some flashlights...) to share it.
12
The UI is simple but consistent: A long press (>0.4s, so it's not that long)
13
switches the light on and off (with memory), a short press switches to the next
14
mode (circular) when on.
16
To configure, just add/delete/edit the values in the modes[]={...} line.
18
//MiniMo -- minimalistic driver firmware for momentary buttons (no blinkies) -- DrJones 2014
19
#define F_CPU 4800000 //use fuses low:0x75 high:0xff
21
#include <util/delay.h>
22
//change modes here; just change/add/delete values. The "0" is 'off'
23
uint8_t modes[]={0, 8,90,255}; //PWM values, 5..255 - LEAVE THE "0" THERE
25
DDRB=2; PORTB=8; //define PB1 as output and pull-up switch on PB3
26
TCCR0A=0b00100001; TCCR0B=0b00000001; //PWM setup, 9kHz
27
uint8_t count=0,mode=0,lastmode=1,waspressed=0; //define some variables used below
29
while(1) { //endless loop
31
if ((PINB&8)==0) { //when the button is pressed (PB3 pulled down)
32
count++; //count length of button press
33
if (count==16) { //pressed long (8*50ms)
34
if (mode>0) {lastmode=mode; mode=0;} //was on? -> off, but remember the mode
35
else mode=lastmode; //was off? -> on, with previous mode.
37
waspressed=1; //remember that the button was pressed, see below
39
else { //button not pressed
40
if (waspressed) { //but it was pressed, so it has just been released!
41
waspressed=0; //reset that
42
if (count<16 && mode>0) { //really was a short press AND the light is on
43
mode++; if (mode>=sizeof(modes)) mode=1; //next mode
45
count=0; //reset counter
49
OCR0B=modes[mode]; //set PWM level (0 is off)
50
_delay_ms(25); //wait a bit before checking again, important for counting
54
I think this may help a couple of projects here.
57
EDIT Since people seem to like Werner's UI (short press for next mode, long
58
press for previous mode, like in mobydrv; but OFF is just one of these modes)
59
and since it is already implemented elsewhere, i thought I'd just show how
60
easily it can be implemented in MiniMo, too. Only 4 lines had to be edited,
61
marked with **, and the lastmode variable declaration was omitted.
63
I also changed the timing a bit (25ms delay in the main loop) to make it
64
respond a bit quicker.
66
//MiniMo -- minimalistic driver firmware for momentary buttons (no blinkies) -- DrJones 2014
67
//Up&Down variant ("Werner's UI")
68
#define F_CPU 4800000 //use fuses low:0x75 high:0xff
70
#include <util/delay.h>
71
//change modes here; just change/add/delete values. The "0" is 'off'
72
uint8_t modes[]={0, 8,90,255}; //PWM values, 5..255 - LEAVE THE "0" THERE
74
DDRB=2; PORTB=8; //define PB1 as output and pull-up switch on PB3
75
TCCR0A=0b00100001; TCCR0B=0b00000001; //PWM setup, 9kHz
76
uint8_t count=0,mode=0,waspressed=0; //define some variables used below
78
while(1) { //endless loop
80
if ((PINB&8)==0) { //when the button is pressed (PB3 pulled down)
81
count++; //count length of button press
82
if (count==16) { //pressed long (16*25ms=0.4s)
83
if (mode>0) mode--; // ** decrease mode
84
else mode=sizeof(modes)-1; // ** wraparound
86
waspressed=1; //remember that the button was pressed, see below
88
else { //button not pressed
89
if (waspressed) { //but it was pressed, so it has just been released!
90
waspressed=0; //reset that
91
if (count<16) { // ** really was a short press
92
mode++; if (mode>=sizeof(modes)) mode=0; // ** next mode
94
count=0; //reset counter
99
OCR0B=modes[mode]; //set PWM level (0 is off)
100
_delay_ms(25); //wait a bit before checking again, important for counting