ESP32-Pill Reaction Time Tester

by GrunclMichal in Circuits > Arduino

334 Views, 0 Favorites, 0 Comments

ESP32-Pill Reaction Time Tester

IMG_20230525_080025.jpg

Hello

This is a tutorial on how to make a reaction time tester using an ESP32S3-Pill. The basic idea is that we'll have a board with two buttons, one LED and one OLED display. You'll have to press one of the buttons to start a test than the LED will blink three times and it'll go dark. As soon as the LED lights up again you can press a the other button to measure your reaction time which will be displayed on OLED display.

Supplies

Connecting

Screenshot 2023-05-24 at 13-30-18 Circuit design Start Simulating Tinkercad.png

Bread Board:

(Check the image to see how to connect things on bread board with ESP32)


In this example button on the left starts the game.


OLED -> ESP32 / Bread Board:

  • SDA -> PIN 14
  • SCK -> PIN 12
  • VDD -> 5V / 3.3V(BB)
  • GND -> GND (BB)


Than just connect your ESP32 to USB-C and we're all set and ready to move to next step.

Code

We'll be using ArduinoIDE. But before we start writing a code let's set some things up.


Click on Tools->Board->Boards Manager. Than search for esp32 by Espressif and hit install.

Click on Tools->Board->esp32 and select ESP32S3 Dev Module

Click on Tools->Manage libraries and install Adafruit GFX Library and Adafruit SSD 1306


And now let's start coding.


(Everything marked with "//" is comment. That means it's not a code, but a text written to help you understand it better. Try to read it and see if you understand what's going on)


//imports libraries
#include <ctime>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>


#define LedPIN 5 //stes up LED pin
#define ButtonPIN 16 //sets up Button pin
#define RSTButtonPIN 17 //sets up Button to start a game pin


#define I2C_SDA 14 //sets up SDA pin
#define I2C_SCL 12 //sets up SCL(SCK) pin
#define SCREEN_WIDTH 128 //OLED display width, in pixels
#define SCREEN_HEIGHT 64 //OLED display height, in pixels


TwoWire I2C = TwoWire(0);


//sets up display
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &I2C, -1);


// this methode is called first and sets up everything
void setup() {
//sets up serial console
Serial.begin(9600);


//sets up I2C and display
I2C.begin(I2C_SDA, I2C_SCL);
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("SSD1306 allocation failed"));
for(;;);
}
delay(2000);
display.clearDisplay();
display.setTextSize(2);
display.setTextColor(WHITE);
display.setCursor(0, 20);


//sets up all the pins
pinMode(LedPIN, OUTPUT);
pinMode(ButtonPIN, INPUT);
pinMode(RSTButtonPIN, INPUT);
}

//this methode is called as second and loops forever
void loop()
{


//if RSTButton is pressed star the game
if(digitalRead(RSTButtonPIN) == true)
{
//sets up RNG
srand(time(NULL));
//blinks LED three times
for(int i = 1; i <= 3; i++)
{
digitalWrite(LedPIN, HIGH);
delay(1000);
digitalWrite(LedPIN, LOW);
delay(1000);
}


//stops program for raandom time
delay(((rand() % 18) + 3) * 1000);


//lights up LED and starts counting time
digitalWrite(LedPIN, HIGH);
unsigned long pastTime = millis();


//loops forever
while(true)
{
//if button is pressed measure time
//write it on display and break of loop
if(digitalRead(ButtonPIN) == HIGH)
{
unsigned long time = millis();
display.clearDisplay();
display.setCursor(0, 20);
display.print(time - pastTime);
display.println(" ms");
display.display();
break;
}
}


//turns off LED
digitalWrite(LedPIN, LOW);
}
}


Now just hit upload and you're done


Great Job :D