explain
This commit is contained in:
@@ -4,12 +4,20 @@
|
||||
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 unsigned long delayBetweenStimulations = 1000; // microseconds (1ms delay for more stable serial transmission)
|
||||
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);
|
||||
Serial.begin(115200); // Increased baud rate for faster output
|
||||
pinMode(buttonPin, INPUT_PULLUP);
|
||||
if (DEBUG) {
|
||||
Serial.begin(115200); // Increased baud rate for faster output
|
||||
}
|
||||
}
|
||||
|
||||
// Function to convert analog value to digital
|
||||
@@ -26,21 +34,50 @@ void writeOutput(uint8_t value) {
|
||||
}
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Function to send one complete epoch
|
||||
void sendEpoch() {
|
||||
unsigned long startTime = micros();
|
||||
for (uint16_t i = 0; i < PATTERN_SIZE; i++) {
|
||||
uint8_t stimulationValue = pgm_read_byte(&stimulation_pattern[i]);
|
||||
uint8_t epochValue = pgm_read_byte(&stimulation_pattern[i]);
|
||||
|
||||
writeOutput(stimulationValue);
|
||||
writeOutput(epochValue);
|
||||
|
||||
// Print the value based on the mode
|
||||
if (USE_DIGITAL) {
|
||||
Serial.println(analogToDigital(stimulationValue) ? 255 : 0);
|
||||
} else {
|
||||
Serial.println(stimulationValue);
|
||||
// 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(delayBetweenStimulations);
|
||||
delayMicroseconds(delayBetweenEpochs);
|
||||
}
|
||||
|
||||
// Remove the long delay at the end to continuously send data
|
||||
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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user