Arduino code for shift register game


#define go_easy 8
#define data 9
#define rclock  10
#define latch 11
#define clr 12
#define led 13

int place = 0;  // No of button pushes
int pins = 0;  // memory - register
int mydigits[] ={128,64,32,16,8,4,2,1};  //  pin 0 maps to 8(dot), 1 maps to 7(center bar)
int scrambled[] = {8,32,1,128,4,16,64,2}; 
bool clock_once = true;  // debounce
bool latch_once = true;  // debounce
bool easy = true;

void setup() {

  DDRD = B11111111;   // portD all outputs
  DDRB = B11100000;   // portB  pin 8,9,10,11,12 inputs, rest output
  PORTD = B00000000;  // portD set low

  if (digitalRead(go_easy) == HIGH) {
    easy = false;
  }
  for (pins=0; pins < 8; pins++) {
    PORTD = mydigits[pins];
    delay(200);
  }
  delay(200);
  PORTD = B00000000;
  pins = 0;

}

void loop() {


  if ((digitalRead(rclock) == HIGH) && (clock_once)) {
    clock_once = false;    
    if (digitalRead(data) == HIGH) {  //  1000 0000
      if (easy) {
        pins += mydigits[place];  // move bits to the left and
      } else {
        pins += scrambled[place];
      }
    } 
    place++;   // how many pins are set?
    if (place > 8) {
      place = 0;
    }
    delay(50);
  }

  if (place == 8) {
    digitalWrite(led, HIGH);
  } else {
    digitalWrite(led, LOW);
  }
  
  if ((digitalRead(latch) == HIGH) && (latch_once)) {
    latch_once = false;
    if (place == 8) {
      PORTD = pins;
    }
    delay(50);
  }
  if (digitalRead(clr) == HIGH) {
    PORTD = B00000000;  // all D ports set LOW
    pins = 0;
    place = 0;
  }
  //  reset debounce check
  if (digitalRead(rclock) == LOW) {
    delay(10);
    if (digitalRead(rclock) == LOW) {
        clock_once = true;
    }
  }
  if (digitalRead(latch) == LOW) {
    delay(10);
    if (digitalRead(latch) == LOW) {
      latch_once = true;
    }
  }

  
}  //loop