-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharduino_pwm_led.ino
405 lines (345 loc) · 12.2 KB
/
arduino_pwm_led.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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
#include <TimerOne.h>
#include <SoftwareSerial.h>
const byte rxPin = 2;
const byte txPin = 3;
SoftwareSerial mySerial(rxPin, txPin, 1);
const int LedPin = 3; // 9 for timer1, 11 for timer2;
int onTime, offTime; // Variables to hold on and off times
volatile int counter = 0; // Counter to track ISR calls
volatile bool ledState = false; // Current state of the LED
bool pinStatus = 0;
long fps = 120;
float duration = 5000.0;
int input;
float dutyCycle = 30.0;
char firstLetter;
// stimulation params
String inputString = ""; //main captured String
char delimiter = ','; // Delimiter to split the string
char stimDelim = '-'; // Delimiter to split the string
// int pulse_interval;
// int pulse_dutyCycle;
bool stimulation_status = false;
const int stimulationPin = 10; // has to be pin 9 or pin 10
const int MAX_VALUES = 100; // Maximum number of values to parse
// String parsedValues[MAX_VALUES]; // Array to store parsed values
String* stimulations; // list of stimulation profiles
// String stimParams[4]; // Array to store [turnOn_times (s), stimulation_durations (ms), pulse_intervals (ms), pulse_dutyCycles (percent)]
int stimOnTime; // sec
int stimDuration; // ms
int stimPulseDur; // ms
int stimPulseDutyCycle; // percent
int stimProfileIndex;
int numStimProfiles;
bool stimTrigger = false;
unsigned long startTime;
unsigned long endTime;
void setup() {
Serial.begin(115200);
digitalWrite(LedPin, LOW);
digitalWrite(stimulationPin, LOW);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LedPin, OUTPUT);
setupTimer2PWM();
}
void loop() {
if (Serial.available() > 0) {
inputString = Serial.readStringUntil('\n');
if (inputString.length() > 0) {
firstLetter = inputString.charAt(0);
// Print the received string
Serial.print("You entered: ");
Serial.println(inputString);
Serial.print("The first letter: ");
Serial.println(firstLetter);
switch (firstLetter) {
case 'D':
decode_stimulation();
break;
case 'V':
stop_stimulation();
break;
case 'S':
start_frame_trigger();
break;
case 'Q':
stop_frame_trigger();
break;
case 'T':
trigger_stimulation();
break;
default:
Serial.print("Unrecognized first letter: ");
Serial.println(firstLetter);
break;
}
} else {
Serial.println("You entered an empty string.");
}
}
endTime = millis();
if (stimTrigger) {
// Serial.print("Cur time: ");
// Serial.println(endTime - startTime);
if (!stimulation_status) {
if ((endTime - startTime) > stimOnTime * 1000) {
start_stimulation();
}
} else {
if ((endTime - startTime) > ((stimOnTime * 1000) + stimDuration)) {
stop_stimulation();
stimProfileIndex++;
if (stimProfileIndex <= numStimProfiles) {
update_stimParams();
} else {
stimTrigger = false;
Serial.println("Stimulations ended.");
}
}
}
}
// delay(100);
}
void decode_stimulation(void) {
stimulations = parseInputString(inputString, delimiter);
numStimProfiles = getValueCount(inputString, delimiter); // Get the count of parsed values
numStimProfiles--;
Serial.print("Num. of stimulations: ");
Serial.println(numStimProfiles);
Serial.println("Stimulation profiles: [turnOn_times (s), stimulation_durations (ms), pulse_intervals (ms), pulse_dutyCycles (percent)]");
for (int i = 1; i <= numStimProfiles; i++) {
Serial.print(i);
Serial.print(". Stimulation: ");
Serial.println(stimulations[i]);
}
}
void update_stimParams(void) {
String* cur_stimParams = parseInputString(stimulations[stimProfileIndex], stimDelim);
// int valueCount = getValueCount(stimulations[stimProfileIndex], stimDelim); // Get the count of parsed values
stimOnTime = cur_stimParams[0].toInt();
stimDuration = cur_stimParams[1].toInt();
stimPulseDur = cur_stimParams[2].toInt();
stimPulseDutyCycle = cur_stimParams[3].toInt();
Serial.println("\nUpdated stimParams:");
Serial.print("stimOnTime: ");
Serial.println(stimOnTime);
Serial.print("stimDuration: ");
Serial.println(stimDuration);
Serial.print("stimPulseDur: ");
Serial.println(stimPulseDur);
Serial.print("stimPulseDutyCycle: ");
Serial.println(stimPulseDutyCycle);
}
void Timer1_ISR(void) {
digitalWrite(stimulationPin, !digitalRead(stimulationPin));
}
void trigger_stimulation(void) {
stimProfileIndex = 1;
update_stimParams();
stimTrigger = true;
startTime = millis();
Serial.println("Stimulation trigger received.");
}
void start_stimulation(void) {
Timer1.initialize(1000 * float(stimPulseDur)); // microsec
Timer1.pwm(stimulationPin, (float(stimPulseDutyCycle) / 100) * 1023);
// Timer1.setPwmDuty(stimulationPin, (stimPulseDutyCycle / 100) * 1023);
// Timer1.attachInterrupt(Timer1_ISR, stimPulseDur * 10);
// Timer1.start();
// Timer1.initialize(1000000 / 10);
// Timer1.pwm(stimulationPin, 512);
stimulation_status = true;
// digitalWrite(stimulationPin, HIGH);
Serial.println("Stimulation started.");
}
void stop_stimulation(void) {
Timer1.disablePwm(stimulationPin);
Timer1.detachInterrupt();
digitalWrite(stimulationPin, 0);
Timer1.stop();
stimulation_status = false;
// digitalWrite(stimulationPin, LOW);
Serial.println("Stimulation stopped.");
}
void start_frame_trigger(void) {
String* parsedValues = parseInputString(inputString, delimiter);
int valueCount = getValueCount(inputString, delimiter); // Get the count of parsed values
// Serial.print("valueCount: ");
// Serial.println(valueCount);
// Serial.println("Parsed values:");
// for (int i = 0; i < valueCount; i++) {
// Serial.println(parsedValues[i]);
// }
fps = parsedValues[1].toInt();
// Serial.print("Received fps: ");
// Serial.println(fps);
// if (pinStatus == 1) {
// stop_frame_trigger();
// }
// // if (pinStatus == 0) {
// Timer1.initialize(1000000 / fps); // 40 us = 25 kHz
// dutyCycle = duration / (1000000 / fps); // calculate duty cycle to have pulse length ~5ms
// Serial.print("Duty cycle: ");
// Serial.println(dutyCycle);
// Timer1.pwm(LedPin, (dutyCycle) * 1023);
// setupBlink_Timer2(int(fps), 50);
// setupBlink_Timer2(int(fps) * 100, 100 / 3); // multiply fps by 100 when using timer2, for 60 fps: duty cycle 30-33%
// setupBlink_Timer2(int(fps) * 100, 100 / 3 * 2); // multiply fps by 100 when using timer2, for 120 fps: duty cycle 66%
setTimer2PWM(float(fps), 50.0);
// setupBlink_Timer2(2, 50);
// setupPWM_Timer2(int(fps));
pinStatus = 1;
Serial.println("Frame trigger started.");
// }
}
void setupTimer2PWM() {
// Set Timer2 PWM pins as outputs
// pinMode(TIMER2_A_PIN, OUTPUT);
// pinMode(TIMER2_B_PIN, OUTPUT);
// Configure Timer2
TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
TCCR2B = _BV(CS22); // Prescaler 64
}
void setTimer2PWM(float fps, float dutyCycle) {
// Constrain inputs
fps = constrain(fps, 10, 120);
dutyCycle = constrain(dutyCycle, 0, 100);
// Calculate prescaler and OCR2A value
uint8_t prescaler;
uint32_t ocr2a;
// 16MHz / (256 * fps) = 62500 / fps
ocr2a = 62500 / fps;
if (ocr2a <= 256) {
prescaler = _BV(CS22); // Prescaler 64
ocr2a = ocr2a - 1;
} else if (ocr2a <= 1024) {
prescaler = _BV(CS22) | _BV(CS21); // Prescaler 256
ocr2a = (ocr2a / 4) - 1;
} else {
prescaler = _BV(CS22) | _BV(CS21) | _BV(CS20); // Prescaler 1024
ocr2a = (ocr2a / 16) - 1;
}
// Set prescaler
TCCR2B = (TCCR2B & 0b11111000) | prescaler;
// Set PWM frequency
OCR2A = ocr2a;
// Set duty cycle
uint8_t pwmValue = (dutyCycle / 100.0) * (OCR2A + 1) - 1;
OCR2B = pwmValue;
Serial.print("FPS: ");
Serial.print(fps);
Serial.print(", Duty Cycle: ");
Serial.print(dutyCycle);
Serial.print("%, OCR2A: ");
Serial.print(OCR2A);
Serial.print(", OCR2B: ");
Serial.println(OCR2B);
}
void setupBlink_Timer2(int FPS, float dutyCycle) {
// Calculate the timer period in microseconds
unsigned long period = 1000000 / FPS; // Full period in microseconds
// Calculate on and off times based on the duty cycle
onTime = int((period * dutyCycle) / 100); // Time LED stays on in microseconds
offTime = period - onTime; // Time LED stays off in microseconds
// Set up Timer2 for CTC mode (Clear Timer on Compare Match)
TCCR2A = (1 << WGM21); // CTC mode
TCCR2B = (1 << CS22) | (1 << CS21) | (1 << CS20); // Prescaler set to 1024
// Set the initial compare match value based on the onTime
OCR2A = ((onTime * 16) / 1024) - 1; // Convert microseconds to timer counts
// Enable Timer2 compare interrupt
TIMSK2 = (1 << OCIE2A);
}
ISR(TIMER2_COMPA_vect) {
counter++;
if (ledState && counter * 1024UL / 16 >= onTime) {
ledState = false; // Turn LED off
OCR2A = ((offTime * 16) / 1024) - 1; // Set next compare to offTime
counter = 0;
} else if (!ledState && counter * 1024UL / 16 >= offTime) {
ledState = true; // Turn LED on
OCR2A = ((onTime * 16) / 1024) - 1; // Set next compare to onTime
counter = 0;
}
digitalWrite(LedPin, ledState); // Update the LED
}
void setupPWM_Timer2(int FPS) {
// Stop the timer while configuring
TCCR2A = 0; // Clear control register A
TCCR2B = 0; // Clear control register B
TCNT2 = 0; // Initialize counter to 0
// Set Fast PWM mode with non-inverted output on OC2A (Pin 11)
TCCR2A |= (1 << WGM21) | (1 << WGM20); // Fast PWM Mode
TCCR2A |= (1 << COM2A1); // Non-inverted PWM
// Calculate the required PWM frequency
long pwmFrequency = FPS;
// Calculate the necessary prescaler based on the desired FPS (f_PWM)
long baseFrequency = 16000000L; // Arduino clock speed is 16 MHz
int prescaler = 1;
if (pwmFrequency < baseFrequency / (256L * 1024)) {
prescaler = 1024;
TCCR2B |= (1 << CS22) | (1 << CS21) | (1 << CS20);
} else if (pwmFrequency < baseFrequency / (256L * 256)) {
prescaler = 256;
TCCR2B |= (1 << CS22) | (1 << CS21);
} else if (pwmFrequency < baseFrequency / (256L * 128)) {
prescaler = 128;
TCCR2B |= (1 << CS22) | (1 << CS20);
} else if (pwmFrequency < baseFrequency / (256L * 64)) {
prescaler = 64;
TCCR2B |= (1 << CS22);
} else if (pwmFrequency < baseFrequency / (256L * 32)) {
prescaler = 32;
TCCR2B |= (1 << CS21) | (1 << CS20);
} else if (pwmFrequency < baseFrequency / (256L * 8)) {
prescaler = 8;
TCCR2B |= (1 << CS21);
} else {
prescaler = 1;
TCCR2B |= (1 << CS20);
}
// Set OCR2A (duty cycle)
OCR2A = 128; // Set duty cycle to 50%
// Now the PWM frequency is set to approximately the given FPS
long actualFrequency = baseFrequency / (prescaler * 256L);
Serial.print("Actual PWM Frequency: ");
Serial.println(actualFrequency);
}
void stop_frame_trigger(void) {
// Timer1.disablePwm(LedPin);
// Timer1.stop();
// Stop the timer and disable PWM by clearing the relevant bits
TCCR2A = 0; // Clear the Timer2 control register A (disables PWM)
TCCR2B = 0; // Clear the Timer2 control register B (stops the timer)
TIMSK2 = 0;
Serial.println("Frame trigger stopped.");
pinStatus = 0;
}
String* parseInputString(String input_str, char delim) {
int valueCount = getValueCount(input_str, delim); // Get the count of parsed values
// Dynamically allocate memory for the parsedValues array
String* parsedValues = new String[valueCount];
int startIndex = 0;
int endIndex = 0;
int currentIndex = 0;
while (endIndex >= 0) {
endIndex = input_str.indexOf(delim, startIndex); // Find the delimiter
if (endIndex == -1) { // If no more delimiter is found, get the last part
parsedValues[currentIndex] = input_str.substring(startIndex);
} else {
parsedValues[currentIndex] = input_str.substring(startIndex, endIndex);
}
startIndex = endIndex + 1; // Move to the next part
currentIndex++;
}
return parsedValues;
}
// Function to count the number of values separated by the delimiter
int getValueCount(String input_str, char delim) {
int count = 1; // At least one value is present
for (int i = 0; i < input_str.length(); i++) {
if (input_str.charAt(i) == delim) {
count++;
}
}
return count;
}