Advertisement
JustAnotherEntity

Assignment 3 Electronics

May 15th, 2024 (edited)
769
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.61 KB | None | 0 0
  1. #include <FastLED.h>
  2.  
  3. #define LED_PIN     2
  4. #define NUM_LEDS 4
  5. #define BRIGHTNESS  64
  6. #define LED_TYPE    WS2811
  7. #define COLOR_ORDER GRB
  8. CRGB leds[NUM_LEDS];
  9.  
  10. const int buzzer = 11;
  11.  
  12. const int btn1 = 3;
  13. const int btn2 = 4;
  14. const int btn3 = 5;
  15. const int btn4 = 6;
  16.  
  17. const int buzzTime = 300;
  18.  
  19. const CRGB Colors[] = {
  20.   CRGB::Yellow,
  21.   CRGB::Red,
  22.   CRGB::Green,
  23.   CRGB::Blue
  24. };
  25.  
  26. int sequenceCounter = 1;
  27.  
  28. bool hasStartedGame = false;
  29.  
  30. void setup() {
  31.   Serial.begin(9600);
  32.   randomSeed(analogRead(0));
  33.  
  34.   FastLED.addLeds<LED_TYPE, LED_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
  35.   FastLED.setBrightness(BRIGHTNESS);
  36.  
  37.   pinMode(btn1, INPUT);
  38.   pinMode(btn2, INPUT);
  39.   pinMode(btn3, INPUT);
  40.   pinMode(btn4, INPUT);
  41.  
  42.   pinMode(buzzer, OUTPUT);
  43. }
  44.  
  45. void loop() {
  46.   if (!hasStartedGame) {
  47.     tone(buzzer, 600);
  48.     delay(200);
  49.     tone(buzzer, 200);
  50.     delay(200);
  51.     tone(buzzer, 600);
  52.     delay(100);
  53.     tone(buzzer, 700);
  54.     delay(100);
  55.     tone(buzzer, 800);
  56.     delay(100);
  57.     noTone(buzzer);
  58.    
  59.     print("Press any button to start!");
  60.     while (!inputOn(btn1) && !inputOn(btn2) && !inputOn(btn3) && !inputOn(btn4))
  61.       delay(10);
  62.     print("Started game");
  63.     hasStartedGame = true;
  64.   }
  65.  
  66.   print("Sequence " + String(sequenceCounter));
  67.  
  68.   delay(1000);
  69.  
  70.   CRGB* sequence = generateSequence(sequenceCounter);
  71.  
  72.   for (int h = 0; h < sequenceCounter; h++)
  73.     flashLEDStrip(sequence[h]);
  74.  
  75.   int checkIndex = 0;
  76.  
  77.   bool failed = false;
  78.  
  79.   while (true) {
  80.     if (sequenceCounter == checkIndex) {
  81.       delay(200);
  82.       tone(buzzer, 600);
  83.       delay(100);
  84.       tone(buzzer, 700);
  85.       delay(100);
  86.       tone(buzzer, 800);
  87.       delay(100);
  88.       noTone(buzzer);
  89.       break;
  90.     }
  91.    
  92.     bool one = inputOn(btn1);
  93.     bool two = inputOn(btn2);
  94.     bool three = inputOn(btn3);
  95.     bool four = inputOn(btn4);
  96.  
  97.     if (one || two || three || four) {
  98.       int freq;
  99.       if (one)
  100.         freq = 200;
  101.       if (two)
  102.         freq = 400;
  103.       if (three)
  104.         freq = 600;
  105.       if (four)
  106.         freq = 800;
  107.        
  108.       if ((one && sequence[checkIndex] != Colors[0]) ||
  109.           (two && sequence[checkIndex] != Colors[1]) ||
  110.           (three && sequence[checkIndex] != Colors[2]) ||
  111.           (four && sequence[checkIndex] != Colors[3])
  112.       ) {
  113.         failed = true;
  114.         break;
  115.       }
  116.       checkIndex++;
  117.       tone(buzzer, freq);
  118.       delay(buzzTime);
  119.       noTone(buzzer);
  120.     }
  121.   }
  122.  
  123.   free(sequence);
  124.  
  125.   if (failed) {
  126.     print("You have failed the sequence");
  127.     tone(buzzer, 800);
  128.     delay(500);
  129.     tone(buzzer, 200);
  130.     delay(500);
  131.     noTone(buzzer);
  132.     delay(2000);
  133.     exit(1);
  134.   }
  135.  
  136.   print("Next sequence");
  137.   sequenceCounter++;
  138.   delay(1000);
  139. }
  140.  
  141. void flashLEDStrip(CRGB color) {
  142.   int toneFreq;
  143.  
  144.   if (color == CRGB::Yellow)
  145.     toneFreq = 200;
  146.   if (color == CRGB::Red)
  147.     toneFreq = 400;
  148.   if (color == CRGB::Green)
  149.     toneFreq = 600;
  150.   if (color == CRGB::Blue)
  151.     toneFreq = 800;
  152.  
  153.   for(int i = 0; i < NUM_LEDS; i++)
  154.     leds[i] = color;
  155.    
  156.   tone(buzzer, toneFreq);
  157.   FastLED.show();
  158.  
  159.   delay(buzzTime);
  160.  
  161.   for(int i = 0; i < NUM_LEDS; i++)
  162.     leds[i] = CRGB::Black;
  163.  
  164.   noTone(buzzer);
  165.   FastLED.show();
  166.   delay(500);
  167. }
  168.  
  169. CRGB* generateSequence(int seqLength) {
  170.     CRGB* arr = (CRGB*)malloc(seqLength * sizeof(CRGB));
  171.  
  172.     for (int i = 0; i < seqLength; i++)
  173.       arr[i] = Colors[random(0, 4)];
  174.  
  175.     return arr;
  176. }
  177.  
  178. bool inputOn(int pin) {
  179.   return digitalRead(pin) == HIGH;
  180. }
  181.  
  182. void print(String text) {
  183.   Serial.println(text);
  184. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement