Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MadderDash coding example.
- //make sure green is 0 & red is 1
- int whichLED[4] = { 10, 11, 12, 13};; int randPattern[8]; int playerPattern[8]; int dblPress[4];
- int patternCheck = 0;
- int patternLength = 2;
- int buttonControl[4] = { 3, 4, 5, 6};
- int winTest = 0;
- bool Button = true;
- const int speedGov = 250;
- void setup() {
- Serial.begin(9600);
- for (byte count = 0; count < 4; count++) {
- pinMode(whichLED[count], OUTPUT);
- pinMode(buttonControl[count], INPUT);
- }
- randomSeed(analogRead(0));
- }
- // If we win, we blink green, if loose, we blink red.
- void winLoss(bool outcome) {
- digitalWrite(whichLED[outcome], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[outcome], LOW);
- delay(speedGov);
- }
- void loop() {
- for (int i = 0; i < 8; i++) {
- playerPattern[i] = 0;
- }
- for (int i = 0; i < 4; i++) {
- dblPress[i] = 0;
- }
- if (patternLength == 7) {
- patternLength = 2;
- }
- patternLength++;
- patternCheck = 0;
- winTest = 0;
- for (int i = 0; i < 8; i++) {
- if (random(101) < 50) {
- randPattern[i] = 0;
- } else {
- randPattern[i] = 1;
- patternCheck++;
- }
- }
- for (int i = 0; i < 8; i++) {
- if (patternLength < patternCheck) {
- if (randPattern[i] == 1) {
- randPattern[i] = 0;
- patternCheck--;
- }
- } else if (patternLength > patternCheck) {
- if (randPattern[i] == 0) {
- randPattern[i] = 1;
- patternCheck++;
- }
- } else {
- i = 8;
- }
- }
- for (int i = 0; i < 8; i++) {
- switch (i) {
- case 0:
- case 1:
- case 2:
- case 3:
- if (randPattern[i] == 1) {
- digitalWrite(whichLED[i], HIGH);
- delay(analogRead(A0) + speedGov);
- digitalWrite(whichLED[i], LOW);
- delay(analogRead(A0) + speedGov);
- }
- break;
- case 4:
- case 5:
- case 6:
- case 7:
- if (randPattern[i] == 1) {
- digitalWrite(whichLED[(i - 4)], HIGH);
- delay(analogRead(A0) + speedGov);
- digitalWrite(whichLED[(i - 4)], LOW);
- delay(analogRead(A0) + speedGov);
- }
- break;
- }
- }// we blink the led's so we know the game started.
- for (byte count = 0; count < 4; count++) {
- for (byte innerCountOn = 0; innerCountOn < 4; innerCountOn++) {
- digitalWrite(whichLED[innerCountOn], HIGH);
- delay(speedGov);
- for (byte innerCountOff = 0; innerCountOff < 4; innerCountOff++) {
- digitalWrite(whichLED[innerCountOff], LOW);
- delay(speedGov);
- }
- }
- }
- for (int i = 0; i < patternLength; i++) {
- while (digitalRead(buttonControl[0]) == LOW && digitalRead(buttonControl[1]) == LOW && digitalRead(buttonControl[2]) == LOW && digitalRead(buttonControl[3]) == LOW) {
- Button = !Button; // << that would be the bool flip.
- }
- if (digitalRead(buttonControl[0] == HIGH) && Button == false) { // << true or false its up to you the logic
- Button = false; // << that would be the bool flip.
- if (dblPress[0] == 0) {
- playerPattern[0] = 1;
- dblPress[0] = 1;
- digitalWrite(whichLED[0], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[0], LOW);
- } else if (dblPress[0] == 1) {
- playerPattern[4] = 1;
- digitalWrite(whichLED[0], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[0], LOW);
- }
- }
- if (digitalRead(buttonControl[1] == HIGH)) {
- Button = false;
- if (dblPress[1] == 0) {
- playerPattern[1] = 1;
- dblPress[1] = 1;
- digitalWrite(whichLED[1], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[1], LOW);
- } else if (dblPress[1] == 1) {
- playerPattern[5] = 1;
- digitalWrite(whichLED[1], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[1], LOW);
- }
- }
- if (digitalRead(buttonControl[2]) == HIGH) {
- Button = false;
- if (dblPress[2] == 0) {
- playerPattern[2] = 1;
- dblPress[2] = 1;
- digitalWrite(whichLED[2], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[2], LOW);
- } else if (dblPress[2] == 1) {
- playerPattern[6] = 1;
- digitalWrite(whichLED[2], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[2], LOW);
- }
- }
- if (digitalRead(buttonControl[3]) == HIGH) {
- Button = false;
- if (dblPress[3] == 0) {
- playerPattern[3] = 1;
- dblPress[3] = 1;
- digitalWrite(whichLED[3], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[3], LOW);
- } else if (dblPress[3] == 1) {
- playerPattern[7] = 1;
- digitalWrite(whichLED[3], HIGH);
- delay(speedGov);
- digitalWrite(whichLED[3], LOW);
- }
- }
- }
- for (int i = 0; i < 8; i++) {
- if (randPattern[i] == playerPattern[i]) {
- winTest++;
- }
- }
- if (winTest != patternLength) {
- patternLength = 2;
- winLoss(1);
- } else {
- winLoss(0);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement