Thursday, October 24, 2013

Project 4 - modernization

    We have finished (Caleb and me) our project 4 (modernization)
Algorithm: 
   Explonations: Firstly program will generate random number and will turn on LED, next, what will do this program is checking in the loop which button was pressed. Then move users LED to the correct side and compare: are random LED is on the same position with user LED, if yes - generate new random number and change LED.

Sketch:
// Leds for user

#define LEDU1 8 //Left one
#define LEDU2 7
#define LEDU3 6
#define LEDU4 5
#define LEDU5 4  //Right one

//Leds for random
#define LEDR1 13 //Left one
#define LEDR2 12
#define LEDR3 11
#define LEDR4 10
#define LEDR5 9//Right one

#define BUTTON1 3 //left //Buttons
#define BUTTON2 2 //right

#define MinLED (LEDU5)
#define MaxLED (LEDU1)

int posU = 0; //position of user's led
int posR = 0; //position of random's led

void setup()
{
  pinMode(LEDU1, OUTPUT); //SetUp user LED outputs
  pinMode(LEDU2, OUTPUT);
  pinMode(LEDU3, OUTPUT);
  pinMode(LEDU4, OUTPUT);
  pinMode(LEDU5, OUTPUT);
  
  pinMode(LEDR1, OUTPUT); //SetUp random LED outputs
  pinMode(LEDR2, OUTPUT);
  pinMode(LEDR3, OUTPUT);
  pinMode(LEDR4, OUTPUT);
  pinMode(LEDR5, OUTPUT);

  pinMode(BUTTON1, INPUT); // input for the button
  pinMode(BUTTON2, INPUT); // input for the button
  
  digitalWrite(LEDU1, HIGH);
  posR = random(5);
  digitalWrite(LEDR1, LOW);
  digitalWrite(LEDR2, LOW);
  digitalWrite(LEDR3, LOW);
  digitalWrite(LEDR4, LOW);
  digitalWrite(LEDR5, LOW);
  if(posR == 0)
  {
    digitalWrite(LEDR1, HIGH);
  }
  else if(posR == 1)
  {
    digitalWrite(LEDR2, HIGH);
  }
  else if(posR == 2)
  {
    digitalWrite(LEDR3, HIGH);
  }
  else if(posR == 3)
  {
    digitalWrite(LEDR4, HIGH);
  }
  else if(posR == 4)
  {
    digitalWrite(LEDR5, HIGH);
  }
  
  compare();
}

void loop()
{
  if(digitalRead(BUTTON1) == HIGH) //move left
  {
      if(posU == 0)
      {
        posU = 4;
      }
      else
      {
        posU--;
      }
      
      digitalWrite(LEDU1, LOW);
      digitalWrite(LEDU2, LOW);
      digitalWrite(LEDU3, LOW);
      digitalWrite(LEDU4, LOW);
      digitalWrite(LEDU5, LOW);
      
      if(posU == 0)
      {
        digitalWrite(LEDU1, HIGH);
      }
      else if(posU == 1)
      {
        digitalWrite(LEDU2, HIGH);
      }
      else if(posU == 2)
      {
        digitalWrite(LEDU3, HIGH);
      }
      else if(posU == 3)
      {
        digitalWrite(LEDU4, HIGH);
      }
      else if(posU == 4)
      {
        digitalWrite(LEDU5, HIGH);
      }
  }
  //-------------------------------------------------------------
  if(digitalRead(BUTTON2) == HIGH) //move right
  {
    if(posU == 4)
    {
      posU = 0;
    }
    else
    {
      posU++;
    }
    
    digitalWrite(LEDU1, LOW);
    digitalWrite(LEDU2, LOW);
    digitalWrite(LEDU3, LOW);
    digitalWrite(LEDU4, LOW);
    digitalWrite(LEDU5, LOW);
    
    if(posU == 0)
    {
      digitalWrite(LEDU2, HIGH);
    }
    else if(posU == 1)
    {
      digitalWrite(LEDU3, HIGH);
    }
    else if(posU == 2)
    {
      digitalWrite(LEDU4, HIGH);
    }
    else if(posU == 3)
    {
      digitalWrite(LEDU5, HIGH);
    }
    else if(posU == 4)
    {
      digitalWrite(LEDU1, HIGH);
    }
  }
//---------------------------------------------------------------
  compare();
  delay(200);
}

int compare()
{
  if(posU == posR)
  {
    posR = random(5);
    digitalWrite(LEDR1, LOW);
    digitalWrite(LEDR2, LOW);
    digitalWrite(LEDR3, LOW);
    digitalWrite(LEDR4, LOW);
    digitalWrite(LEDR5, LOW);
    if(posR == 0)
    {
      digitalWrite(LEDR1, HIGH);
    }
    else if(posR == 1)
    {
      digitalWrite(LEDR2, HIGH);
    }
    else if(posR == 2)
    {
      digitalWrite(LEDR3, HIGH);
    }
    else if(posR == 3)
    {
      digitalWrite(LEDR4, HIGH);
    }
    else if(posR == 4)
    {
      digitalWrite(LEDR5, HIGH);
    }
    return 1; //return 1 if user win
  }
  
  return 0; //return 0 if user lose
}

Schematic: 

How it works: 

P.S. Sorry for bad quality of video...

No comments:

Post a Comment