-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bouncr.ino
196 lines (149 loc) · 3.87 KB
/
Bouncr.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
//
// Bouncer Resonant Autorocker
//
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_LIS3DH.h>
#include <Adafruit_Sensor.h>
#include <Stepper.h>
#include <Adafruit_MCP23017.h>
#include <Adafruit_RGBLCDShield.h>
#include "window.h"
#include "daccel.h"
#include "rocker.h"
#include "display.h"
#include "pins.h"
#if defined(ARDUINO_ARCH_SAMD)
// for Zero, output on USB Serial console, remove line below if using programming port to program the Zero!
#define Serial SerialUSB
#endif
Adafruit_LIS3DH lis = Adafruit_LIS3DH();
Stepper motor(STEPPER_MOTOR_STEPS_PER_REV,
STEPPER_MOTOR_PIN1,
STEPPER_MOTOR_PIN2,
STEPPER_MOTOR_PIN3,
STEPPER_MOTOR_PIN4);
// The shield uses the I2C SCL and SDA pins. On classic Arduinos
// this is Analog 4 and 5 so you can't use those for analogRead() anymore
// However, you can connect other I2C sensors to the I2C bus and share
// the I2C bus.
Adafruit_RGBLCDShield lcd = Adafruit_RGBLCDShield();
void setup() {
// Debugging output
Serial.begin(9600);
lcd.begin(16, 2);
lcd.print(" Baby Bouncer ");
lcd.setCursor(0,1);
lcd.print(" version 0.1 ");
lcd.setBacklight(1);
motor.setSpeed(60);
delay(3000);
}
#define MODE_TEXT_MEASURE "measure "
#define MODE_TEXT_OSCILLATE "oscillate"
#define MODE_TEXT_UNKNOWN "unknown! "
#define MODE_MEASURE 0
#define MODE_OSCILLATE 1
void display_mode(uint8_t mode) {
lcd.setCursor(6,0);
lcd.print(mode==MODE_MEASURE?
MODE_TEXT_MEASURE:mode==MODE_OSCILLATE?
MODE_TEXT_OSCILLATE:MODE_TEXT_UNKNOWN);
}
uint8_t intensity = 0;
uint8_t dutycycle = 1;
float frequency = 1.81;
uint8_t mode=0;
uint8_t menu_item=0;
void initial_menu_value()
{
switch(menu_item) {
case MENU_ITEM_DUTYCYCLE:
display_dutycycle(dutycycle, 5);
break;
case MENU_ITEM_FREQUENCY:
display_frequency(frequency, 4);
lcd.print("Hz");
break;
case MENU_ITEM_INTENSITY:
display_intensity(intensity, 6);
break;
}
}
void loop() {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("mode:");
display_mode(mode);
display_menu(menu_item);
initial_menu_value();
uint8_t buttons = 0;
rocker_init();
while(true) {
buttons = lcd.readButtons();
rocker_step(motor, intensity, dutycycle, frequency);
if(buttons & BUTTON_SELECT) {
mode = 1-mode;
display_mode(mode);
while(buttons & BUTTON_SELECT) {
buttons = lcd.readButtons();
}
}
if(buttons & BUTTON_UP) {
menu_item = menu_item==0 ? 0 : menu_item - 1;
display_menu(menu_item);
initial_menu_value();
while(buttons & BUTTON_UP) {
buttons = lcd.readButtons();
}
}
if(buttons & BUTTON_DOWN) {
menu_item = menu_item==MENU_ITEM_COUNT ? MENU_ITEM_COUNT : menu_item + 1;
display_menu(menu_item);
initial_menu_value();
while(buttons & BUTTON_DOWN) {
buttons = lcd.readButtons();
}
}
if(buttons & BUTTON_LEFT) {
switch(menu_item) {
case MENU_ITEM_DUTYCYCLE:
dutycycle = min(4, dutycycle+1);
display_dutycycle(dutycycle, 4);
break;
case MENU_ITEM_INTENSITY:
intensity = max(0, intensity-1);
display_intensity(intensity, 2);
break;
case MENU_ITEM_FREQUENCY:
frequency = max(0, frequency-0.01);
display_frequency(frequency, 4);
break;
}
uint8_t c=16;
while(buttons & BUTTON_LEFT && c--) {
buttons = lcd.readButtons();
}
}
if(buttons & BUTTON_RIGHT) {
switch(menu_item) {
case MENU_ITEM_DUTYCYCLE:
dutycycle = max(1, dutycycle-1);
display_dutycycle(dutycycle, 4);
break;
case MENU_ITEM_INTENSITY:
intensity = min(50, intensity+1);
display_intensity(intensity, 2);
break;
case MENU_ITEM_FREQUENCY:
frequency = min(10, frequency+0.01);
display_frequency(frequency, 4);
break;
}
uint8_t c=16;
while(buttons & BUTTON_RIGHT && c--) {
buttons = lcd.readButtons();
}
}
}
}