forked from GadgetReboot/Arduino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathX9C_555_Osc_Control.ino
257 lines (202 loc) · 8.38 KB
/
X9C_555_Osc_Control.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*******************************************************************************
This is an example for using the X9Cxxx digital potentiometers,
allowing control over the wiper position from min to max, and
optionally saving the wiper position so it can be recalled
automatically upon next power up.
***This sketch is intended to manipulate the wiper of a pot that is
in a 555 oscillator frequency control situation. By controlling
the pot and changing the oscillator frequency, tones or sound
effects can be generated by the 555***
Uses the Bounce2 library (installed through Arduino library manager)
Requires the X9C potentiometer control library by Phil Bowles
https://github.com/philbowles/Arduino-X9C
Note: Changes required to the original X9C.cpp file from GitHub
(as of Last commit Jun 8 2017)
The following 2 functions should be replaced with this code:
void X9C::_deselectAndSave(){
digitalWrite(_inc,LOW); //***********GADGET REBOOT CHANGED******************
delayMicroseconds(1); //***********GADGET REBOOT CHANGED******************
digitalWrite(_inc,HIGH); //***********GADGET REBOOT CHANGED******************
delayMicroseconds(1); //***********GADGET REBOOT CHANGED******************
digitalWrite(_cs,HIGH); // unselect chip and write current value to NVRAM
}
void X9C::_stepPot(uint8_t amt,uint8_t dir){
uint8_t cnt=(amt > X9C_MAX) ? X9C_MAX:amt-1; //***********GADGET REBOOT CHANGED******************
digitalWrite(_ud,dir); // set direction
digitalWrite(_cs,LOW); // select chip
delayMicroseconds(1);
while(cnt--){
digitalWrite(_inc,LOW); // falling pulse triggers wiper change (xN = cnt)
delayMicroseconds(1);
digitalWrite(_inc,HIGH);
delayMicroseconds(1);
}
delayMicroseconds(100); // let new value settle; (datasheet P7 tIW)
}
This code is released to the public domain.
Gadget Reboot
*******************************************************************************/
#include <X9C.h> // X9C pot library
#include <Bounce2.h> // button debounce library
#define UD 10 // pot up/down mode pin
#define INC 11 // pot increment pin
#define CS 12 // pot chip select pin
#define buttonOne 3 // button to set pot to min point
#define buttonTwo 4 // button to set pot to mid point
#define buttonThree 5 // button to set pot to max point
#define buttonFour 6 // button to inc pot by 10
#define buttonFive 7 // button to dec pot by 10
#define buttonSix 8 // button to inc pot by 1
#define buttonSeven 9 // button to dec pot by 1
// X9C wiring: pin 3[High Terminal] -- R1 -- pin 5[Wiper] -- R2 -- pin 6[Low Terminal]
// The "X9C_UP" direction refers to the amount of resistance being created between the wiper and the "High" terminal
// rather than the position of the wiper itself moving up toward the high terminal
// (which would reduce resistance from wiper to High).
// i.e. setPot(70, false) will set the resistance between the X9C device pins 5 and 3 to 70% of maximum resistance
// where R1 = 70% of max, R2 = 30% of max
const int debounceInterval = 10; // debounce time (ms) for button readings
X9C pot; // instantiate a pot controller
Bounce buttonOneDB = Bounce(); // instantiate a bounce object for each button
Bounce buttonTwoDB = Bounce();
Bounce buttonThreeDB = Bounce();
Bounce buttonFourDB = Bounce();
Bounce buttonFiveDB = Bounce();
Bounce buttonSixDB = Bounce();
Bounce buttonSevenDB = Bounce();
void setup() {
randomSeed(analogRead(0)); // for random tone generation
pot.begin(CS, INC, UD);
pinMode(buttonOne, INPUT_PULLUP);
pinMode(buttonTwo, INPUT_PULLUP);
pinMode(buttonThree, INPUT_PULLUP);
pinMode(buttonFour, INPUT_PULLUP);
pinMode(buttonFive, INPUT_PULLUP);
pinMode(buttonSix, INPUT_PULLUP);
pinMode(buttonSeven, INPUT_PULLUP);
// attach buttons to debouncers
buttonOneDB.attach(buttonOne);
buttonOneDB.interval(debounceInterval); // interval in ms
buttonTwoDB.attach(buttonTwo);
buttonTwoDB.interval(debounceInterval); // interval in ms
buttonThreeDB.attach(buttonThree);
buttonThreeDB.interval(debounceInterval); // interval in ms
buttonFourDB.attach(buttonFour);
buttonFourDB.interval(debounceInterval); // interval in ms
buttonFiveDB.attach(buttonFive);
buttonFiveDB.interval(debounceInterval); // interval in ms
buttonSixDB.attach(buttonSix);
buttonSixDB.interval(debounceInterval); // interval in ms
buttonSevenDB.attach(buttonSeven);
buttonSevenDB.interval(debounceInterval); // interval in ms
}
void loop() {
// update the Bounce instances
buttonOneDB.update();
buttonTwoDB.update();
buttonThreeDB.update();
buttonFourDB.update();
buttonFiveDB.update();
buttonSixDB.update();
buttonSevenDB.update();
// change potentiometer setting if required based on button presses,
// storing the setting in the chip if "true" is passed to the pot controller
pot.setPotMax(false); //Default the pot in a parked wiper position for this experiment when between button actions
// misc effect
if ( buttonOneDB.fell()) {
pot.setPot(90, false); // starting point of tone sweep
for (int cnt = 90; cnt >= 40; cnt-- ) // sweep tone at certain speed for certain time
{
pot.trimPot(1, X9C_DOWN, false);
delay(3);
}
for (int cnt = 10; cnt <= 60; cnt++ )
{
pot.setPot(cnt, false); // set pot wiper randomly to play tones
delay(6);
}
pot.setPot(39, false);
for (int cnt = 39; cnt >= 1; cnt-- ) // sweep tone at certain speed for certain time
{
pot.trimPot(1, X9C_DOWN, false);
delay(5);
}
}
// misc effect
if ( buttonTwoDB.fell()) {
pot.setPot(98, false); // starting point of tone sweep
for (int cnt = 98; cnt >= 50; cnt-- ) // sweep tone at certain speed for certain time
{
pot.trimPot(1, X9C_DOWN, false);
delay(3);
}
for (int cnt = 49; cnt >= 30; cnt-- ) // sweep tone at certain speed for certain time
{
pot.trimPot(1, X9C_DOWN, false);
delay(17);
}
for (int cnt = 29; cnt >= 1; cnt-- ) // sweep tone at certain speed for certain time
{
pot.trimPot(1, X9C_DOWN, false);
delay(12);
}
for (int cnt = 0; cnt <= 100; cnt++ )
{
pot.setPot(random(1, 98), false); // set pot wiper randomly to play tones
delay(3);
}
}
// noise generator (within the tone spectrum of the oscillator)
if ( buttonThreeDB.fell()) {
for (int cnt = 0; cnt <= 400; cnt++ )
{
pot.setPot(random(1, 98), false); // set pot wiper randomly to play tones
delay(3);
}
}
// random tones
if ( buttonFourDB.fell()) {
for (int cnt = 0; cnt <= 40; cnt++ )
{
pot.setPot(random(1, 98), false); // set pot wiper randomly to play tones
delay(30);
}
}
// laser shot
if ( buttonFiveDB.fell()) {
pot.setPot(90, false); // starting point of tone sweep
for (int cnt = 90; cnt >= 40; cnt-- ) // sweep tone at certain speed for certain time
{
pot.trimPot(1, X9C_DOWN, false);
delay(3);
}
for (int cnt = 39; cnt >= 1; cnt-- ) // sweep tone at certain speed for certain time
{
pot.trimPot(1, X9C_DOWN, false);
delay(10);
}
}
// play a sliding up/down tone siren for a few cycles
if ( buttonSixDB.fell()) {
pot.setPot(1, false); // starting point of tone sweep
for (int cnt = 1; cnt <= 50; cnt++ ) // sweep tone one direction
{
pot.trimPot(1, X9C_UP, false); // use trim instead of setPot for a smooth tone slide since setPot moves wiper to 0 each time
delay(50);
}
for (int cnt = 50; cnt >= 1; cnt-- ) // sweep tone other direction
{
pot.trimPot(1, X9C_DOWN, false);
delay(50);
}
}
// play a two tone siren for a few cycles
if ( buttonSevenDB.fell()) {
for (int cnt = 0; cnt <= 3; cnt++ )
{
pot.setPot(20, false); // first tone
delay(500);
pot.setPot(80, false); // second tone
delay(500);
}
}
}