analog and digital LED – week 9

OBJECTIVE(S):

For this assignment, I have to admit, I did the bare minimum which is fulfilling the criteria for this week. I wanted to control the brightness of one LED to fulfill the analog criteria. Then, to cross the digital sensor requirement, I used a button to control when the LED turns on.

 

DIFFICULTIES:

The moment I read the assignment prompt, I knew I had to learn the structure of an Arduino UNO board since I forgot everything we took in class lol. However, that was fruitful since I learned some things about Arduino boards we did not cover in class 🙂 I also faced difficulties with matching the name of the parts like the potentiometer to the pieces we have in our kits since there are different designs of the same pieces.

 

IMPLEMENTATION:

implementing the pieces and writing the code was fairly easy after learning the structure of the Arduino UNO board. I used a blue LED to be controlled by the potentiometer since it’s blue as well, additionally, I used a red LED to be used with the red button to make it more easy/satisfying to use.

 

CODE:

const int analogIn = A0;  
const int analogOut = 5; 
const int buttonPin = 4;     
const int ledPin =  7; 
int sensorValue = 0;        
int output = 0;  
int buttonState = 0;       

void setup() {
  // initialize serial communications
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
  pinMode(buttonPin, INPUT);
  
}

void loop() {
  // read the analong input value
  sensorValue = analogRead(analogIn);
  // map it to the range of 0 to 255
  output = map(sensorValue, 0, 1023, 0, 255);
  // change the analog out value
  analogWrite(analogOut, output);

// delay 2 milliseconds 
  delay(2);

  // read the state of the pushbutton 
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed
  if (buttonState == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

 

🙂

 

Leave a Reply