Tuesday, 8 December 2015

Arduino Lab 3 Button Control: Mandatory Challenge Completed (part 1&2)

The following code is what I wrote for Arduino Lab 3 mandatory challenge. Visual evidence are also provided.

Code:

(part 1)
int led1 = 0;
int led2 = 1;
int led3 = 2;
int led4 = 3;

int button1 = 8;
int button2 = 9;

void setup()
{
  pinMode(led1,OUTPUT);
  pinMode(led2,OUTPUT);
  pinMode(led3,OUTPUT);
  pinMode(led4,OUTPUT);
  pinMode(button2,INPUT);
  pinMode(button1,INPUT);
}

void loop()
{
  int delaytime = 500;
  if (digitalRead(button1) == HIGH)
  {
    digitalWrite(led2,HIGH);
    digitalWrite(led4,HIGH);
    delay(delaytime);
    digitalWrite(led2,LOW);
    digitalWrite(led4,LOW);
 
  }
  else if (digitalRead(button2) == HIGH)
  {
    digitalWrite(led1,HIGH);
    digitalWrite(led3,HIGH);
    delay(delaytime);
    digitalWrite(led1,LOW);
    digitalWrite(led3,LOW);
  }
  else if (digitalRead(button1) == LOW)
  {
    digitalWrite(led2, LOW);
    digitalWrite(led4, LOW);
  }
  else if (digitalRead(button2)==LOW)
  {
    digitalWrite(led1,LOW);
    digitalWrite(led3,LOW);
  }
  else
  {
    digitalWrite(led1,LOW);
    digitalWrite(led2,LOW);
    digitalWrite(led3,LOW);
    digitalWrite(led4,LOW);
  }
}


(part 2)

int leds[] = {0,1,2,3};
int counter = 0;
int buttons[] = {8,9};

void setup()
{
  for (int a=0; a<5; a+=1)
  {
    pinMode(leds[a],OUTPUT);
  }
  pinMode(8,INPUT);
  pinMode(9,INPUT);
}

void loop()
{
  digitalWrite(0,LOW);
  digitalWrite(1,LOW);
  digitalWrite(2,LOW);
  digitalWrite(3,LOW);
  if (digitalRead(8) == HIGH)
  {
    int counter = counter + 1;
    digitalWrite(0, HIGH);
    digitalWrite(1, HIGH);
    digitalWrite(2, HIGH);
    digitalWrite(3, HIGH);
    delay(4000);
    digitalWrite(0,LOW);
    digitalWrite(1,LOW);
    digitalWrite(2,LOW);
    digitalWrite(3,LOW);
  }
  if (digitalRead(9) == HIGH)
  {
    digitalWrite(leds[counter],HIGH);
  }
}

Visual Evidence:

(part1)





(part 2)






No comments:

Post a Comment