Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FastLED.h>
- #define LED_PIN 2
- #define NUM_LEDS 4
- #define BRIGHTNESS 64
- #define LED_TYPE WS2811
- #define COLOR_ORDER GRB
- CRGB leds[NUM_LEDS];
- const int buzzer = 11;
- const int btn1 = 3;
- const int btn2 = 4;
- const int btn3 = 5;
- const int btn4 = 6;
- const int buzzTime = 300;
- const CRGB Colors[] = {
- CRGB::Yellow,
- CRGB::Red,
- CRGB::Green,
- CRGB::Blue
- };
- int sequenceCounter = 1;
- bool hasStartedGame = false;
- void setup() {
- Serial.begin(9600);
- randomSeed(analogRead(0));
- FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
- FastLED.setBrightness(BRIGHTNESS);
- pinMode(btn1, INPUT);
- pinMode(btn2, INPUT);
- pinMode(btn3, INPUT);
- pinMode(btn4, INPUT);
- pinMode(buzzer, OUTPUT);
- }
- void loop() {
- if (!hasStartedGame) {
- tone(buzzer, 600);
- delay(200);
- tone(buzzer, 200);
- delay(200);
- tone(buzzer, 600);
- delay(100);
- tone(buzzer, 700);
- delay(100);
- tone(buzzer, 800);
- delay(100);
- noTone(buzzer);
- print("Press any button to start!");
- while (!inputOn(btn1) && !inputOn(btn2) && !inputOn(btn3) && !inputOn(btn4))
- delay(10);
- print("Started game");
- hasStartedGame = true;
- }
- print("Sequence " + String(sequenceCounter));
- delay(1000);
- CRGB* sequence = generateSequence(sequenceCounter);
- for (int h = 0; h < sequenceCounter; h++)
- flashLEDStrip(sequence[h]);
- int checkIndex = 0;
- bool failed = false;
- while (true) {
- if (sequenceCounter == checkIndex) {
- delay(200);
- tone(buzzer, 600);
- delay(100);
- tone(buzzer, 700);
- delay(100);
- tone(buzzer, 800);
- delay(100);
- noTone(buzzer);
- break;
- }
- bool one = inputOn(btn1);
- bool two = inputOn(btn2);
- bool three = inputOn(btn3);
- bool four = inputOn(btn4);
- if (one || two || three || four) {
- int freq;
- if (one)
- freq = 200;
- if (two)
- freq = 400;
- if (three)
- freq = 600;
- if (four)
- freq = 800;
- if ((one && sequence[checkIndex] != Colors[0]) ||
- (two && sequence[checkIndex] != Colors[1]) ||
- (three && sequence[checkIndex] != Colors[2]) ||
- (four && sequence[checkIndex] != Colors[3])
- ) {
- failed = true;
- break;
- }
- checkIndex++;
- tone(buzzer, freq);
- delay(buzzTime);
- noTone(buzzer);
- }
- }
- free(sequence);
- if (failed) {
- print("You have failed the sequence");
- tone(buzzer, 800);
- delay(500);
- tone(buzzer, 200);
- delay(500);
- noTone(buzzer);
- delay(2000);
- exit(1);
- }
- print("Next sequence");
- sequenceCounter++;
- delay(1000);
- }
- void flashLEDStrip(CRGB color) {
- int toneFreq;
- if (color == CRGB::Yellow)
- toneFreq = 200;
- if (color == CRGB::Red)
- toneFreq = 400;
- if (color == CRGB::Green)
- toneFreq = 600;
- if (color == CRGB::Blue)
- toneFreq = 800;
- for(int i = 0; i < NUM_LEDS; i++)
- leds[i] = color;
- tone(buzzer, toneFreq);
- FastLED.show();
- delay(buzzTime);
- for(int i = 0; i < NUM_LEDS; i++)
- leds[i] = CRGB::Black;
- noTone(buzzer);
- FastLED.show();
- delay(500);
- }
- CRGB* generateSequence(int seqLength) {
- CRGB* arr = (CRGB*)malloc(seqLength * sizeof(CRGB));
- for (int i = 0; i < seqLength; i++)
- arr[i] = Colors[random(0, 4)];
- return arr;
- }
- bool inputOn(int pin) {
- return digitalRead(pin) == HIGH;
- }
- void print(String text) {
- Serial.println(text);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement