Assignment: Digital and Analog Inputs

Concept

The potentiometer controls the brightness of all 4 LEDs. Different switch initiates different blinking behavior.

Behaviour 

Potentiometer controls the brightness of the LEDs. The red switch allows each LED to blink after one another and the green switch allows all 4 LEDs to blink together.

Schematic

Problems

I had no particular problem while setting up the circuit and writing the code but I was quite unsure as to whether setting the LED pins to numbers with the ~ sign next to them would also behave in a digital fashion. It was just a small confusion and uncertainty I had, but setting it up and trying it for myself helped me clear out the concept better. 

On a side note, my blog post on the previous assignment talks about a specific problem I had with the delay() function and how I incorporated millis() instead.

Program

int red = 3;
int blue = 5;
int green = 6;
int yellow = 9;

int redSwitch = 2;
int greenSwitch = 12;

int counter = 5;
int brightness = 0;


void setup() {

  Serial.begin(9600);

  pinMode(3, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  pinMode(9, OUTPUT);

  pinMode(2, INPUT);
  pinMode(13, INPUT);

}

void loop() {

  int sensorValue = analogRead(A0);

  Serial.println(sensorValue);
  delay(1);

  //potentiometer controlling the brightness of the LEDs
  analogWrite(3, sensorValue / 4);
  analogWrite(5, sensorValue / 4);
  analogWrite(6, sensorValue / 4);
  analogWrite(9, sensorValue / 4);


  // red switch
  int redState = digitalRead(redSwitch);

  // if statement for the red switch; all 4 LEDs to blink one after the other in 3 sets
  if (redState == 1) {
    for (int counter = 3; counter > 0 ; counter --) {
      digitalWrite(3, HIGH);
      delay(50);
      digitalWrite(3, LOW);
      delay(50);
      digitalWrite(5, HIGH);
      delay(50);
      digitalWrite(5, LOW);
      delay(50);
      digitalWrite(6, HIGH);
      delay(50);
      digitalWrite(6, LOW);
      delay(50);
      digitalWrite(9, HIGH);
      delay(50);
      digitalWrite(9, LOW);
      delay(50);


    }
  }

  //green switch
  int greenState = digitalRead(greenSwitch);


  // if statement for the green switch; when pressed, all 4 LEDs blink 10 times every 50 milliseconds.
  if (greenState == 1) {
    for (int counter = 10; counter > 0 ; counter --) {
      digitalWrite(3, HIGH);
      digitalWrite(5, HIGH);
      digitalWrite(6, HIGH);
      digitalWrite(9, HIGH);
      delay(50);
      digitalWrite(3, LOW);
      digitalWrite(5, LOW);
      digitalWrite(6, LOW);
      digitalWrite(9, LOW);
      delay(50);

    }



  }
}

Leave a Reply

Your email address will not be published. Required fields are marked *