// ------ Reads the state of 2 switches and lights LEDs accordingly ---------------------
//       -Using variables to store pin numbers
//
//    Adafruit Circuit Playground
//    https://www.adafruit.com/product/3000
//    https://learn.adafruit.com/introducing-circuit-playground
//
//  > Red LED is connected on Playground as Digital Pin 13
//  > Push button switches are connected to Arduino Digital Pins 4 (left) and 19 (right)
//    and are pulled down on the board (LOW when open, HIGH when closed)
//    (with USB cable pointing at 12 o'clock)
//---------------------------------------------------------------------------------------
#include <Adafruit_CircuitPlayground.h>
int randLED;
int randRed = 0;
int ledPin = 13;               // create variable for LED pin
int buttonLeftPin = 4;         // create variables for button pins
int buttonRightPin = 19;

boolean buttonLeftState = 0;   // create variables for storing "state" of the buttons
boolean buttonRightState = 0;


void setup() {
  CircuitPlayground.begin();
  pinMode(ledPin, OUTPUT);     // initialize the digital pin as an output (for LED)
  pinMode(buttonLeftPin, INPUT);   // initialize the digital pins as inputs (for switches)
  pinMode(buttonRightPin, INPUT);
}

void loop()  {
  buttonLeftState = digitalRead(buttonLeftPin);   // "Read" the voltage present on the pins (check switches)
  buttonRightState = digitalRead(buttonRightPin);
  delay(20);
  CircuitPlayground.clearPixels();
  randLED = random(10);
  CircuitPlayground.setPixelColor(randLED, randRed, 255-randRed, 150);
  delay(200);
  // If the pin wired to the Left button is HIGH, the switch is closed
  if (buttonLeftState == HIGH) {
  randRed = random(255);
  delay(100);
  }
  // If the pin wired to the Right button is HIGH, the switch is closed
  if (buttonRightState == HIGH) {
    flash(4, 70);
  // otherwise 
  } else {
    digitalWrite(ledPin, LOW);      // turn LED off
  }
}

/*
 * User Defined Functions
 */

// let's make a function that flashes
void flash(int times, int howLong) {
  for (int i = 0; i < times; i++) {
    digitalWrite(ledPin, HIGH);
    delay(howLong/2);
    digitalWrite(ledPin, LOW);
    delay(howLong/2);
  }
}
