Catch the led 2015-04-30

//We always have to include the library
#include "LedControl.h"

/*
 Now we need a LedControl to work with.
 ***** These pin numbers will probably not work with your hardware *****
 pin 12 is connected to the DataIn
 pin 11 is connected to the CLK
 pin 10 is connected to LOAD
 We have only a single MAX72XX.
 */
LedControl lc=LedControl(12,11,10,1);

/* we always wait a bit between updates of the display */
unsigned long delaytime=100;

int analogPin_x = 0;     // potentiometer wiper (middle terminal) connected to analog pin 0
int analogPin_y = 1;     // potentiometer wiper (middle terminal) connected to analog pin 1

int val_x = 0;           // variable to store the value read
int val_y = 0;           // variable to store the value read

int coordinata_x = 0;
int coordinata_y = 0;

int random_x = 0;
int random_y = 0;

int conta_led = 0;
int led_catturato = false;

unsigned long time_start;
unsigned long time_end;
unsigned long time;

void setup() {
Serial.begin(9600);
randomSeed(analogRead(2));
  /*
   The MAX72XX is in power-saving mode on startup,
   we have to do a wakeup call
   */
  lc.shutdown(0,false);
  /* Set the brightness to a medium values */
  lc.setIntensity(0,8);
  /* and clear the display */
  lc.clearDisplay(0);
}



void loop() {
if (led_catturato == false) {
random_x = random(7);
random_y = random(7);
lc.setLed(0,random_y,random_x,true);
led_catturato = true;
}

  val_x = analogRead(analogPin_x)+38;    // read the input pin
  val_y = analogRead(analogPin_y)+12;    // read the input pin
 
  coordinata_x = map(val_x, 0, 1023, 0, 6);
  coordinata_y = map(val_y, 0, 1023, 0, 6);

        lc.setLed(0,coordinata_y,coordinata_x,true);
        lc.setLed(0,coordinata_y,coordinata_x,false);

       
        if (coordinata_y == random_y && coordinata_x == random_x) {
        lc.setLed(0,random_y,random_x,false);
        if (conta_led < 8) {
        lc.setLed(0,7,conta_led,true);}
        else {
        lc.setLed(0,7,conta_led-8,false);
        }
        conta_led = conta_led + 1;
        if (conta_led == 1) {
        time_start = millis();
        }
        led_catturato = false;
        }

if (conta_led == 16){
time_end = millis();
time = (time_end - time_start);
Serial.print("Time: ");
Serial.print(time);
Serial.println(" millisec.");
delay(2500);
conta_led = 0;
lc.clearDisplay(0);

}
}

Nessun commento:

Posta un commento