Advertisement
Maderdash

Simon Says example code

Nov 8th, 2021
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //MadderDash coding example.
  2. //make sure green is 0 & red is 1
  3. int whichLED[4] = { 10, 11, 12, 13};; int randPattern[8]; int playerPattern[8]; int dblPress[4];
  4. int patternCheck = 0;
  5. int patternLength = 2;
  6. int buttonControl[4] = { 3, 4, 5, 6};
  7. int winTest = 0;
  8. bool Button = true;
  9. const int speedGov = 250;
  10. void setup() {
  11.   Serial.begin(9600);
  12.   for (byte count = 0; count < 4; count++) {
  13.     pinMode(whichLED[count], OUTPUT);
  14.     pinMode(buttonControl[count], INPUT);
  15.   }
  16.   randomSeed(analogRead(0));
  17. }
  18. // If we win, we blink green, if loose, we blink red.
  19. void winLoss(bool outcome) {
  20.   digitalWrite(whichLED[outcome], HIGH);
  21.   delay(speedGov);
  22.   digitalWrite(whichLED[outcome], LOW);
  23.   delay(speedGov);
  24. }
  25. void loop() {
  26.   for (int i = 0; i < 8; i++) {
  27.     playerPattern[i] = 0;
  28.   }
  29.   for (int i = 0; i < 4; i++) {
  30.     dblPress[i] = 0;
  31.   }
  32.   if (patternLength == 7) {
  33.     patternLength = 2;
  34.   }
  35.   patternLength++;
  36.   patternCheck = 0;
  37.   winTest = 0;
  38.   for (int i = 0; i < 8; i++) {
  39.     if (random(101) < 50) {
  40.       randPattern[i] = 0;
  41.     } else {
  42.       randPattern[i] = 1;
  43.       patternCheck++;
  44.     }
  45.   }
  46.   for (int i = 0; i < 8; i++) {
  47.     if (patternLength < patternCheck) {
  48.       if (randPattern[i] == 1) {
  49.         randPattern[i] = 0;
  50.         patternCheck--;
  51.       }
  52.     } else if (patternLength > patternCheck) {
  53.       if (randPattern[i] == 0) {
  54.         randPattern[i] = 1;
  55.         patternCheck++;
  56.       }
  57.     } else {
  58.       i = 8;
  59.     }
  60.   }
  61.   for (int i = 0; i < 8; i++) {
  62.     switch (i) {
  63.       case 0:
  64.       case 1:
  65.       case 2:
  66.       case 3:
  67.         if (randPattern[i] == 1) {
  68.           digitalWrite(whichLED[i], HIGH);
  69.           delay(analogRead(A0) + speedGov);
  70.           digitalWrite(whichLED[i], LOW);
  71.           delay(analogRead(A0) + speedGov);
  72.         }
  73.         break;
  74.       case 4:
  75.       case 5:
  76.       case 6:
  77.       case 7:
  78.         if (randPattern[i] == 1) {
  79.           digitalWrite(whichLED[(i - 4)], HIGH);
  80.           delay(analogRead(A0) + speedGov);
  81.           digitalWrite(whichLED[(i - 4)], LOW);
  82.           delay(analogRead(A0) + speedGov);
  83.         }
  84.         break;
  85.     }
  86.   }// we blink the led's so we know the game started.
  87.   for (byte count = 0; count < 4; count++) {
  88.     for (byte innerCountOn = 0; innerCountOn < 4; innerCountOn++) {
  89.       digitalWrite(whichLED[innerCountOn], HIGH);
  90.       delay(speedGov);
  91.       for (byte innerCountOff = 0; innerCountOff < 4; innerCountOff++) {
  92.         digitalWrite(whichLED[innerCountOff], LOW);
  93.         delay(speedGov);
  94.       }
  95.     }
  96.   }
  97.   for (int i = 0; i < patternLength; i++) {
  98.     while (digitalRead(buttonControl[0]) == LOW && digitalRead(buttonControl[1]) == LOW && digitalRead(buttonControl[2]) == LOW && digitalRead(buttonControl[3]) == LOW) {
  99.       Button = !Button; // << that would be the bool flip.
  100.     }
  101.     if (digitalRead(buttonControl[0] == HIGH) && Button == false) { // << true or false its up to you the logic
  102.       Button = false; // << that would be the bool flip.
  103.       if (dblPress[0] == 0) {
  104.         playerPattern[0] = 1;
  105.         dblPress[0] = 1;
  106.         digitalWrite(whichLED[0], HIGH);
  107.         delay(speedGov);
  108.         digitalWrite(whichLED[0], LOW);
  109.       } else if (dblPress[0] == 1) {
  110.         playerPattern[4] = 1;
  111.         digitalWrite(whichLED[0], HIGH);
  112.         delay(speedGov);
  113.         digitalWrite(whichLED[0], LOW);
  114.       }
  115.     }
  116.     if (digitalRead(buttonControl[1] == HIGH)) {
  117.       Button = false;
  118.       if (dblPress[1] == 0) {
  119.         playerPattern[1] = 1;
  120.         dblPress[1] = 1;
  121.         digitalWrite(whichLED[1], HIGH);
  122.         delay(speedGov);
  123.         digitalWrite(whichLED[1], LOW);
  124.       } else if (dblPress[1] == 1) {
  125.         playerPattern[5] = 1;
  126.         digitalWrite(whichLED[1], HIGH);
  127.         delay(speedGov);
  128.         digitalWrite(whichLED[1], LOW);
  129.       }
  130.     }
  131.     if (digitalRead(buttonControl[2]) == HIGH) {
  132.       Button = false;
  133.       if (dblPress[2] == 0) {
  134.         playerPattern[2] = 1;
  135.         dblPress[2] = 1;
  136.         digitalWrite(whichLED[2], HIGH);
  137.         delay(speedGov);
  138.         digitalWrite(whichLED[2], LOW);
  139.       } else if (dblPress[2] == 1) {
  140.         playerPattern[6] = 1;
  141.         digitalWrite(whichLED[2], HIGH);
  142.         delay(speedGov);
  143.         digitalWrite(whichLED[2], LOW);
  144.       }
  145.     }
  146.     if (digitalRead(buttonControl[3]) == HIGH) {
  147.       Button = false;
  148.       if (dblPress[3] == 0) {
  149.         playerPattern[3] = 1;
  150.         dblPress[3] = 1;
  151.         digitalWrite(whichLED[3], HIGH);
  152.         delay(speedGov);
  153.         digitalWrite(whichLED[3], LOW);
  154.       } else if (dblPress[3] == 1) {
  155.         playerPattern[7] = 1;
  156.         digitalWrite(whichLED[3], HIGH);
  157.         delay(speedGov);
  158.         digitalWrite(whichLED[3], LOW);
  159.       }
  160.     }
  161.   }
  162.   for (int i = 0; i < 8; i++) {
  163.     if (randPattern[i] == playerPattern[i]) {
  164.       winTest++;
  165.     }
  166.   }
  167.   if (winTest != patternLength) {
  168.     patternLength = 2;
  169.     winLoss(1);
  170.  
  171.   } else {
  172.     winLoss(0);
  173.   }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement