Tuesday 8 December 2015

Arduino Lab 4 Potentiometer (Twist Plot): Mandatory Challenge Completed

The following code is what I wrote to complete Arduino Lab 4 mandatory challenge. Visual evidence are also provided.

Code:

int sensorPin = A0;
int ledPins[] = {6,9,10,11};

void setup()
{
  pinMode(sensorPin,INPUT);
  pinMode(ledPins[1,2,3,4], OUTPUT);
}
void loop()
{
  int a = analogRead(sensorPin)/63.75;
  if (a == 4)
  {
    for (int i = 0; i<5; i+=1)
    {
      analogWrite(ledPins[i], HIGH);
      delay(100);
    }
  }
  else if (a == 3)
  {
    for (int t = 0; t<4; t+=1)
    {
      analogWrite(ledPins[t], HIGH);
      delay(100);
    }
  }
  else if (a == 2)
  {
    for (int x=0; x<3; x+=1)
    {
      analogWrite(ledPins[x], HIGH);
      delay(100);
    }
  }
  else if (a ==1)
  {
    for (int y=0; y<2; y+=1)
    {
      analogWrite(ledPins[y],HIGH);
      delay(100);
    }
  }
  else
  {
    for (int g=0; g<5; g+=1)
    {
      digitalWrite(ledPins[g], LOW);
    }
  }
}

Visual Evidence:







No comments:

Post a Comment