/* ============================================
code is placed under the MIT license
Copyright (c) 2024 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
#include <Toggle.h> // https://github.com/Dlloydev/Toggle
constexpr size_t stepCountPerStimulus = 3; // change to 30 if you want 30 x 4 steps in the sequence
constexpr unsigned int lowFrequency = 300;
constexpr unsigned int highFrequency = 1000;
constexpr unsigned int minDelay = 500;
constexpr unsigned int maxDelay = 2000;
constexpr byte rightArmBuzzerPin = 5;
constexpr byte leftArmBuzzerPin = 6;
constexpr byte highFreqButtonPin = 2;
constexpr byte lowFreqButtonPin = 3;
Toggle highFreqButton;
Toggle lowFreqButton;
enum : byte {LEFT_ARM, RIGHT_ARM};
enum : byte {LOW_FREQ, HIGH_FREQ};
enum : byte {LOW_FREQ_BUTTON, HIGH_FREQ_BUTTON, NO_RESPONSE};
enum : byte {IDLE, DELAY, ACTION, NEXT, SEQUENCE_TERMINATED,} state = IDLE;
struct __attribute__ ((packed)) Step { // 7 bytes
byte location: 1;
byte frequency: 1;
byte response: 2;
uint16_t initialDelay;
uint32_t reactionTime;
};
constexpr size_t sequenceLength = 4 * stepCountPerStimulus;
Step sequence[sequenceLength]; // 840 bytes
size_t sequencePosition;
uint32_t startTime;
void fisherYatesShuffle(Step arr[], size_t size) {
for (size_t i = size - 1; i > 0; --i) {
size_t j = random(i + 1);
Step temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
void generateReport() {
Serial.println(F("|Trial|Location|Prompt|Answer|Reaction time(ms)|Delay(ms)|"));
Serial.println(F("|-|-|-|-|-|-|"));
for (size_t i = 0; i < sequenceLength; i++) {
Serial.write('|'); Serial.print(i + 1);
Serial.write('|'); Serial.print(sequence[i].location == LEFT_ARM ? F("Left") : F("Right"));
Serial.write('|'); Serial.print(sequence[i].frequency == LOW_FREQ ? F("Low") : F("High"));
Serial.write('|'); Serial.print(sequence[i].response == LOW_FREQ_BUTTON ? F("Low") : F("High"));
Serial.write('|'); Serial.print(sequence[i].reactionTime);
Serial.write('|'); Serial.print(sequence[i].initialDelay);
Serial.println('|');
}
Serial.println();
}
void generateSequence() {
// initialize the array
randomSeed(analogRead(A0));
for (size_t i = 0; i < sequenceLength; i++) {
if (i >= 3 * stepCountPerStimulus) {
sequence[i].location = LEFT_ARM;
sequence[i].frequency = LOW_FREQ;
}
else if (i >= 2 * stepCountPerStimulus) {
sequence[i].location = LEFT_ARM;
sequence[i].frequency = HIGH_FREQ;
}
else if (i >= stepCountPerStimulus) {
sequence[i].location = RIGHT_ARM;
sequence[i].frequency = LOW_FREQ;
}
else {
sequence[i].location = RIGHT_ARM;
sequence[i].frequency = HIGH_FREQ;
}
sequence[i].initialDelay = random(minDelay, maxDelay + 1);
sequence[i].response = NO_RESPONSE;
sequence[i].reactionTime = 0;
}
// shuffle the array
fisherYatesShuffle(sequence, sequenceLength);
}
void startAction(byte loc, byte freq) {
byte buzzerPin = (loc == LEFT_ARM) ? leftArmBuzzerPin : rightArmBuzzerPin;
unsigned int frequency = (freq == LOW_FREQ) ? lowFrequency : highFrequency;
tone(buzzerPin, frequency);
}
void stopAction(byte loc, byte freq) {
byte buzzerPin = (loc == LEFT_ARM) ? leftArmBuzzerPin : rightArmBuzzerPin;
noTone(buzzerPin);
}
void setup() {
highFreqButton.begin(highFreqButtonPin);
lowFreqButton.begin(lowFreqButtonPin);
pinMode(rightArmBuzzerPin, OUTPUT);
pinMode(leftArmBuzzerPin, OUTPUT);
Serial.begin(115200);
Serial.println(F("➜ Press a button to get started."));
}
void loop() {
highFreqButton.poll();
lowFreqButton.poll();
switch (state) {
case IDLE:
if (highFreqButton.onPress() || lowFreqButton.onPress()) {
// one of the two button is pressed, we start
generateSequence();
sequencePosition = 0;
startTime = millis();
state = DELAY;
}
break;
case DELAY:
if (millis() - startTime >= sequence[sequencePosition].initialDelay) {
// we waited long enough, action
startAction(sequence[sequencePosition].location, sequence[sequencePosition].frequency);
startTime = millis();
state = ACTION;
}
break;
case ACTION:
if (highFreqButton.onPress()) {
sequence[sequencePosition].reactionTime = millis() - startTime;
sequence[sequencePosition].response = HIGH_FREQ_BUTTON;
state = NEXT;
}
else if (lowFreqButton.onPress()) {
sequence[sequencePosition].reactionTime = millis() - startTime;
sequence[sequencePosition].response = LOW_FREQ_BUTTON;
state = NEXT;
}
break;
case NEXT:
stopAction(sequence[sequencePosition].location, sequence[sequencePosition].frequency);
sequencePosition++;
if (sequencePosition >= sequenceLength) {
state = SEQUENCE_TERMINATED;
} else {
startTime = millis();
state = DELAY;
}
break;
case SEQUENCE_TERMINATED:
generateReport();
state = IDLE;
Serial.println(F("➜ Press a button to start a new session."));
break;
}
}
LEFT ARM
RIGHT ARM
LOW
FREQUENCY
HIGH
FREQUENCY
WIRES HIDDEN FOR READABILITY