Arduino for Beginners

Assignment: Use an Arduino to read a digital input to control a digital output (Turn on an LED, play a tune on a speaker, turn a motor, etc.)

I worked with a classmate and used a simple code to make a button work on my arduino. The goal was that when we pressed the button the LED would turn on.  I realized in the process of constructing this simple project the importance of collaboration and how useful the internet can be when it comes to sharing ideas and debugging.

Code Used:
int ledPin = 9; // choose the pin for the LED
int inPin = 7; // choose the input pin (for a pushbutton)
int val = 0; // variable for reading the pin status
void setup() {
Serial.begin(9600);
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inPin, INPUT); // declare pushbutton as input
}
void loop(){
Serial.println(val);
val = digitalRead(inPin); // read input value
if (val == HIGH) { // check if the input is HIGH (button released)
digitalWrite(ledPin, LOW); // turn LED OFF
} else {
digitalWrite(ledPin, HIGH); // turn LED ON
}
}
Arduino Circuit Layout
Results:
G_20170420_17472510

Comments

Popular Posts