LLMind/apply_stimulation/apply_stimulation.ino
2024-10-19 05:05:34 +03:00

84 lines
2.3 KiB
C++

#include <avr/pgmspace.h>
#include "stimulation_pattern.h"
const bool DEBUG = true; // Set to true to enable serial output
const bool USE_DIGITAL = true; // Set to false to use analog output
const int stimulationPin = 5; // PWM-capable pin
const int buttonPin = 2; // Digital pin for button input
const unsigned long delayBetweenEpochs = 1000; // microseconds (1ms delay for more stable serial transmission)
const int threshold = 50; // Threshold for converting analog to digital
const unsigned long debounceDelay = 50; // Debounce time in milliseconds
int lastButtonState = HIGH; // Assuming a pull-up resistor is used
unsigned long lastDebounceTime = 0;
void setup() {
pinMode(stimulationPin, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
if (DEBUG) {
Serial.begin(115200); // Increased baud rate for faster output
}
}
// Function to convert analog value to digital
bool analogToDigital(uint8_t analogValue) {
return analogValue >= threshold;
}
// Function to write output (digital or analog)
void writeOutput(uint8_t value) {
if (USE_DIGITAL) {
digitalWrite(stimulationPin, analogToDigital(value) ? HIGH : LOW);
} else {
analogWrite(stimulationPin, value);
}
}
// Function to send one complete epoch
void sendEpoch() {
unsigned long startTime = micros();
for (uint16_t i = 0; i < PATTERN_SIZE; i++) {
uint8_t epochValue = pgm_read_byte(&stimulation_pattern[i]);
writeOutput(epochValue);
// Print the value based on the mode, only if DEBUG is true
if (DEBUG) {
if (USE_DIGITAL) {
Serial.println(analogToDigital(epochValue) ? 255 : 0);
} else {
Serial.println(epochValue);
}
}
delayMicroseconds(delayBetweenEpochs);
}
if (DEBUG) {
unsigned long endTime = micros();
Serial.print("Epoch complete. Duration (ms): ");
Serial.println((endTime - startTime) / 1000.0);
}
}
void loop() {
int reading = digitalRead(buttonPin);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading == LOW) { // Button is pressed (assuming pull-up resistor)
sendEpoch();
// Wait for button release
while (digitalRead(buttonPin) == LOW) {
delay(10);
}
}
}
lastButtonState = reading;
}