Wednesday, February 3, 2016

Day 6 - Intro to Sensors and Microcontroller inputs and outputs


  • Microcontrollers, input and outputs
    •  Demonstrate Push button LED using if/else statement
      • Here is how it was built in the video and what the end result was.


      • In addition here is the code that helped achieve this


#define buttonPin 8 //# of pushbutton pin
#define ledPin 6    //# of LED pin
int buttonState = 0; //variable for reading the pushbutton status
void setup() {
  pinMode(ledPin, OUTPUT);  //initialize the LED pin as output:
  pinMode(buttonPin, INPUT);  //initialize push button as input:
}
void loop() {
buttonState = digitalRead(buttonPin); //read the state of the pushbutton value:
// check if push buton is pressed. If it is, the buttonState is HIGH:
if (buttonState == HIGH) {
    digitalWrite(ledPin, HIGH); // turn LED on:
}
else {
digitalWrite(ledPin, LOW); // turn LED off
}
}

    • Demonstrate LDR controlling LEDs
      • Here is how it was built in the video and what the end result was.

      • In addition here is the code that helped achieve this
#define motorPin 9 //Motor connected to dig. pin 9
void setup() {
  // nothing happens in set up

}

void loop() {
analogWrite (motorPin, 90);//set motor to 20%
delay (1000);
analogWrite (motorPin, 50);//set motor to 90%
delay (1000);

}

No comments:

Post a Comment