Add COMBINED_LIGHTSTRIP_CODE
Most recent edition as of 28 April 2025
This commit is contained in:
		
							parent
							
								
									a53d48154e
								
							
						
					
					
						commit
						4c88e7e9cb
					
				| 
						 | 
				
			
			@ -0,0 +1,176 @@
 | 
			
		|||
#include <Arduino.h>
 | 
			
		||||
#include <NeoPixelBus.h>
 | 
			
		||||
 | 
			
		||||
#define NUM_LEDS 300 // How many leds in your strip?
 | 
			
		||||
#define DATA_PIN 10 //
 | 
			
		||||
#define BUTTON_PIN 43 //
 | 
			
		||||
#define CLOCK_PIN 8 //
 | 
			
		||||
 | 
			
		||||
#define white RgbColor(5, 5, 5)
 | 
			
		||||
#define black RgbColor(0, 0, 0)
 | 
			
		||||
 | 
			
		||||
int roundCounter = 0; //# of rounds to be iterated on
 | 
			
		||||
int correctLED = 88; //an arbitrary number
 | 
			
		||||
int maxRounds = 5;
 | 
			
		||||
int mode; //The mode the user wants to do
 | 
			
		||||
int lightOn = 50; // time light will be on in mode 2
 | 
			
		||||
double loopDelay = 1.0;
 | 
			
		||||
unsigned long correctTime = 0;
 | 
			
		||||
unsigned long reactionTime = 0;
 | 
			
		||||
unsigned long startTime = 0;
 | 
			
		||||
unsigned long loopTime = 0;
 | 
			
		||||
volatile bool buttonFlag = false; // idk
 | 
			
		||||
unsigned volatile long buttonPressTime = millis();
 | 
			
		||||
String color = String("Red"); //Let the user eventually set the color
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
//variable rxn time exclusive variables
 | 
			
		||||
unsigned int randomLED;
 | 
			
		||||
unsigned long startMillis;
 | 
			
		||||
unsigned long endMillis;
 | 
			
		||||
unsigned long elapsedMillis;
 | 
			
		||||
unsigned int randomMicrosecondDelay;
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
NeoPixelBus<DotStarBgrFeature, DotStarSpiMethod> strip(NUM_LEDS, CLOCK_PIN, DATA_PIN);
 | 
			
		||||
 | 
			
		||||
void buttonPressed() {
 | 
			
		||||
  unsigned long current = millis(); // change perhaps
 | 
			
		||||
  if(buttonPressTime + 200 < current) {
 | 
			
		||||
    buttonFlag = true;
 | 
			
		||||
    buttonPressTime = current;
 | 
			
		||||
    //vibrations are detected by the power supply and are registered as button presses!
 | 
			
		||||
  } // if
 | 
			
		||||
} // buttonPressed
 | 
			
		||||
 | 
			
		||||
void printPressed(int counter, long delayed, long diff, long correct) {
 | 
			
		||||
  if (abs(diff) < 10){
 | 
			
		||||
     Serial.println("Perfect timing!");
 | 
			
		||||
     Serial.printf("%d, %l, %l \n", counter, delayed, correct);
 | 
			
		||||
  } else if (diff > 0) {
 | 
			
		||||
     Serial.print("Too early! Time difference: ");
 | 
			
		||||
     Serial.printf("%d, %l, %l \n", counter, delayed, abs(diff));
 | 
			
		||||
  } else if (diff < 0) {
 | 
			
		||||
     Serial.print("Too late! Time difference: ");
 | 
			
		||||
     Serial.printf("%d, %l, %l \n", counter, delayed, abs(diff));
 | 
			
		||||
  } // if - else
 | 
			
		||||
      buttonFlag = false;
 | 
			
		||||
} // printPressed
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  Serial.begin(115200);
 | 
			
		||||
  strip.Begin();
 | 
			
		||||
  strip.ClearTo(RgbColor(0, 0, 0));
 | 
			
		||||
  strip.Show();
 | 
			
		||||
 | 
			
		||||
  pinMode(BUTTON_PIN, INPUT_PULLUP);  //use internal pullup resistor
 | 
			
		||||
  randomSeed(analogRead(0)); //make sure analog pin 0 is't connected!!!
 | 
			
		||||
  attachInterrupt(BUTTON_PIN, buttonPressed, FALLING);
 | 
			
		||||
} // setup
 | 
			
		||||
 | 
			
		||||
//assumes mode = 1
 | 
			
		||||
void catLoop() {
 | 
			
		||||
  if (roundCounter > maxRounds) {
 | 
			
		||||
      // turn LEDs off
 | 
			
		||||
      strip.Show();
 | 
			
		||||
      Serial.println("Max rounds reached. Stopping program.");
 | 
			
		||||
      return; // Exit the loop function and stop further execution
 | 
			
		||||
  } // if
 | 
			
		||||
for (int i = 0; i < NUM_LEDS; i++) {
 | 
			
		||||
      //turns light on to white, delays, turns it off(black)
 | 
			
		||||
      strip.SetPixelColor(i, white);
 | 
			
		||||
      strip.Show();
 | 
			
		||||
      delayMicroseconds(loopDelay);
 | 
			
		||||
      strip.SetPixelColor(i, white);
 | 
			
		||||
      if (i == correctLED) {
 | 
			
		||||
        correctTime = millis();
 | 
			
		||||
        strip.Show();
 | 
			
		||||
      } // if
 | 
			
		||||
      if (buttonFlag) { // button press output
 | 
			
		||||
        unsigned long timeDiff = correctTime - buttonPressTime;
 | 
			
		||||
        printPressed(roundCounter, loopDelay, timeDiff, correctTime);
 | 
			
		||||
        buttonFlag = false;
 | 
			
		||||
      } // if
 | 
			
		||||
    roundCounter++;
 | 
			
		||||
    } // for
 | 
			
		||||
} // catLoop
 | 
			
		||||
 | 
			
		||||
//assumes mode = 2
 | 
			
		||||
void fixedRxnTime() {
 | 
			
		||||
     if (roundCounter > maxRounds - 1) {
 | 
			
		||||
      strip.Show();
 | 
			
		||||
      Serial.println("Max rounds reached. Stopping program.");
 | 
			
		||||
      return; // Exit the loop function and stop further execution
 | 
			
		||||
    } // checks for running completion if
 | 
			
		||||
 | 
			
		||||
    //turns light on to white, delays, turns it off(black)
 | 
			
		||||
    strip.SetPixelColor(correctLED, white);
 | 
			
		||||
    strip.Show();
 | 
			
		||||
    correctTime = millis();
 | 
			
		||||
    delay(lightOn);
 | 
			
		||||
    strip.SetPixelColor(correctLED, black);
 | 
			
		||||
    strip.Show();
 | 
			
		||||
    delayMicroseconds(loopDelay);
 | 
			
		||||
    if (!buttonFlag) {
 | 
			
		||||
       Serial.println("Button missed");
 | 
			
		||||
    } else {
 | 
			
		||||
      buttonFlag = false;
 | 
			
		||||
      unsigned long timeDiff = correctTime - buttonPressTime;
 | 
			
		||||
      printPressed(roundCounter, loopDelay, timeDiff, correctTime);
 | 
			
		||||
    } // button press output
 | 
			
		||||
} //fixedRxnTime
 | 
			
		||||
 | 
			
		||||
//assumes mode = 3
 | 
			
		||||
void variableRxnTime() {
 | 
			
		||||
  delayMicroseconds(2000000); // waits for 2 seconds
 | 
			
		||||
  while (roundCounter != maxRounds) {
 | 
			
		||||
    randomMicrosecondDelay = random(3p, 6); // generates a random delay so the user cant predict anything
 | 
			
		||||
    delayMicroseconds(randomMicrosecondDelay * 1000000);
 | 
			
		||||
    randomLED = random(10); // illuminates a random light between the range 0 - x
 | 
			
		||||
    strip.SetPixelColor(randomLED, white);
 | 
			
		||||
    strip.Show(); // displays the light
 | 
			
		||||
    startMillis = millis(); // begins taking the time
 | 
			
		||||
    while (!buttonFlag) { // waits for the button to be pressed
 | 
			
		||||
    }
 | 
			
		||||
    buttonFlag = false; // 
 | 
			
		||||
    endMillis = millis(); // takes the final time
 | 
			
		||||
    elapsedMillis = endMillis - startMillis;
 | 
			
		||||
    strip.SetPixelColor(randomLED, black);
 | 
			
		||||
    strip.Show();
 | 
			
		||||
    Serial.println(elapsedMillis);
 | 
			
		||||
    roundCounter++;
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
  Serial.println("Which mode would you like to run?");
 | 
			
		||||
  Serial.println("1 - CAT");
 | 
			
		||||
  Serial.println("2 - Fixed Reaction Time");
 | 
			
		||||
  Serial.println("3 - Variable Reaction Time");
 | 
			
		||||
  Serial.print("Enter Mode Number:");
 | 
			
		||||
  do {
 | 
			
		||||
    mode = Serial.parseInt();
 | 
			
		||||
    while(Serial.available()) Serial.read();
 | 
			
		||||
  } while (mode == 0);
 | 
			
		||||
  Serial.println(mode);
 | 
			
		||||
 | 
			
		||||
  Serial.println("");
 | 
			
		||||
  Serial.println("");
 | 
			
		||||
  roundCounter = 0; //resets the counter per test after each test concludes
 | 
			
		||||
  switch (mode) {
 | 
			
		||||
    case 1:
 | 
			
		||||
      catLoop();
 | 
			
		||||
      break;
 | 
			
		||||
    case 2:
 | 
			
		||||
      fixedRxnTime();
 | 
			
		||||
      break;
 | 
			
		||||
    case 3:
 | 
			
		||||
      variableRxnTime();
 | 
			
		||||
      break;
 | 
			
		||||
    case 4:
 | 
			
		||||
      //TODO Wtv other tests
 | 
			
		||||
      break;
 | 
			
		||||
    default:
 | 
			
		||||
      break;
 | 
			
		||||
  }
 | 
			
		||||
} // loop
 | 
			
		||||
		Loading…
	
		Reference in New Issue