Tuesday, 15 November 2011

Tiny.cc/ArdiunoGuide - Circ - 07

//PART 1
int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0, time = 500;

void setup()
{
  for (int i = 0; i < 4; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
 
  pinMode (2, INPUT);
  pinMode (3, INPUT);
}

void loop()
{
  buttonA = digitalRead(2);
  buttonB = digitalRead(3);
 
    if ((buttonA == HIGH) && (buttonB == LOW))
    {
      evenLED();
    }
   
    else if ((buttonB == HIGH) && (buttonA == LOW))
    {
      oddLED();
    }
}

void evenLED()
{
  for(int i = 0; i < 4; i +=2)
  {
    digitalWrite(ledPin[i], HIGH);
    delay(time);
    digitalWrite(ledPin[i], LOW);
    delay(time);
  }
}

void oddLED()
{
  for(int i = 1; i < 4; i +=2)
  {
    digitalWrite(ledPin[i], HIGH);
    delay(time);
    digitalWrite(ledPin[i], LOW);
    delay(time);
  }
}
     
//PART 2
int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0;
int time = 500;
int counter;

void setup()
{
  for (int i = 0; i < 4; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
 
  pinMode (2, INPUT);
  pinMode (3, INPUT);
}

void loop()
{
  buttonA = digitalRead(2);
  buttonB = digitalRead(3);
  counter = 0;
 
  while (!((buttonA == HIGH) && (buttonB == HIGH)))     //Will not run while both buttons are pressed
  {
    if ((buttonA == HIGH) && buttonB == HIGH))
    {
      counter++;
     
      digitalWrite(ledPin[0], HIGH);
      delay(time);
      digitalWrite(ledPin[0], LOW);
      delay(time);
    }
   
    else if ((buttonB == HIGH) && (buttonA == LOW))
    {
      if (counter > 4)
      {
        allLED();
      }
     
      else if (counter == 0)
      {
        delay(1000);
      }
     
      else
      {
        counter--;                        //For approptiat index
        digitalWrite(ledPin[counter], HIGH);
        delay (time);
        digitalWrite(ledPin[counter], LOW);
        delay (time);
      }
    }
  }
}

//PART 3

int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0, time = 500;
void allLED()
{
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], HIGH);
  }
 
  delay(time);
 
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], LOW);
  }
 
  delay(time);
}
int ledPin [] = {13, 12, 11, 10};
int buttonA = 0, buttonB = 0;
int time = 500;
int counter;

void setup()
{
  for (int i = 0; i < 4; i++)
  {
    pinMode(ledPin[i], OUTPUT);
  }
 
  pinMode (2, INPUT);
  pinMode (3, INPUT);
}

void loop()
{
  buttonA = digitalRead(2);
  buttonB = digitalRead(3);
  counter = 0;
 
  while (!((buttonA == HIGH) && (buttonB == HIGH)))     //Will not run while both buttons are pressed
  {
    if (buttonA == HIGH)
    {
      counter++;
     
      digitalWrite(ledPin[0], HIGH);
      delay(time);
      digitalWrite(ledPin[0], LOW);
      delay(time);
    }
   
    else if (buttonB == HIGH)
    {
      if (counter > 4)
      {
        allLED();
      }
     
      else if (counter == 0)
      {
        delay(1000);
      }
     
      else
      {
        counter--;                        //For approptiat index
        digitalWrite(ledPin[counter], HIGH);
        delay (time);
        digitalWrite(ledPin[counter], LOW);
        delay (time);
      }
    }
  }
}

void allLED()
{
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], HIGH);
  }
 
  delay(time);
 
  for(int i = 0; i < 4; i++)
  {
    digitalWrite(ledPin[counter], LOW);
  }
 
  delay(time);
}

No comments:

Post a Comment