Digital Input/Output Lab, Analog Input Lab

Digital Input/Output Lab

In this lab, I practiced using digital inputs and outputs using LEDs. Here is the code provided by the lab:

void setup() {
  // put your setup code here, to run once:
  pinMode(2, INPUT);    // set the pushbutton pin to be an input
  pinMode(3, OUTPUT);   // set the yellow LED pin to be an output
  pinMode(4, OUTPUT);   // set the red LED pin to be an output

}

void loop() {
     // read the pushbutton input:
   if (digitalRead(2) == HIGH) {
     // if the pushbutton is closed:
     digitalWrite(3, HIGH);    // turn on the yellow LED
     digitalWrite(4, LOW);     // turn off the red LED
   }
   else {
     // if the switch is open:
     digitalWrite(3, LOW);     // turn off the yellow LED
     digitalWrite(4, HIGH);    // turn on the red LED
   }
}

And here is a video of my completed work:



Analog Input lab

In this lab, I practiced using analog inputs by using a potentiometer to control an LED. I did not have a force sensor, so I could only do this part of the lab. Here is the code provided by the lab:

const int ledPin = 9;       // pin that the LED is attached to
int analogValue = 0;        // value read from the pot
int brightness = 0;         // PWM pin that the LED is on.


void setup() {
// initialize serial communications at 9600 bps:
    Serial.begin(9600);
    // declare the led pin as an output:
    pinMode(ledPin, OUTPUT);
}

void loop() {
analogValue = analogRead(A0);    // read the pot value
    brightness = analogValue /4;       //divide by 4 to fit in a byte
    analogWrite(ledPin, brightness);   // PWM the LED with the brightness value
    Serial.println(brightness);        // print the brightness value back to the serial

}

And here is a video of my completed work:




Comments

Popular posts from this blog

PROJECT 1 (2.0!) : LOCK BOX (For 3/26/19)

Project one observations