This weeks slides are on this link: Presentation_Week2
This is a helpful link for understanding color coding of resistors: http://www.instructables.com/id/Resistor-Color-Coding/
You can also use this link to calculate your resistor value based on it is colors: http://www.dannyg.com/examples/res2/resistor.htm
This week’s assignments are these 4 labs:
Making Multiple LED’s Blink
What do you need:
- Arduino
- Breadboard
- 2 LED’s
- 2 220 Ohm Resistors
- Wires
Code:
// the setup function runs once when you press reset or power the board void setup() { // initialize digital pin 13 as an output. pinMode(13, OUTPUT); pinMode(12, OUTPUT); } // the loop function runs over and over again forever void loop() { //Lets turn on/off the first LED now. digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(13, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second //Lets turn on/off the second LED now. digitalWrite(12, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(12, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second }
Switch and an LED
What do you need:
- Arduino
- Breadboard
- 1 LED
- 1 220 Ohm Resistor
- 1 10 Kilohm Resistor
- 1 switch
- Wires
Code:
// set pin numbers: int buttonPin = 2; // the number of the pushbutton pin int ledPin = 13; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pin as an output: pinMode(ledPin, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin, HIGH); } else { // turn LED off: digitalWrite(ledPin, LOW); } }
Switch and Three LED’s
What do you need:
- Arduino
- Breadboard
- 3 LED’s
- 3 220 Ohm Resistors
- 1 10 Kilohm Resistors
- 1 switch
- Wires
/* This example is very similar to previous switch example. The difference is when you press the switch (HIGH) your lights will light up from left to right on your breadboard. If you don't press on your switch (LOW), your LED's will light up from right to left. Feel free to play with your delay times, the order of LED's. Mustafa */ // set pin numbers: int buttonPin = 2; // the number of the pushbutton pin int ledPin0 = 13; // the number of the first LED pin int ledPin1 = 12; // the number of the second LED pin int ledPin2 = 11; // the number of the third LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void setup() { // initialize the LED pins as an output: pinMode(ledPin0, OUTPUT); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); // initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); } void loop() { // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check if the pushbutton is pressed. // if it is, the buttonState is HIGH: if (buttonState == HIGH) { // turn LED on: digitalWrite(ledPin0, HIGH); delay(500); digitalWrite(ledPin1, HIGH); delay(500); digitalWrite(ledPin2, HIGH); delay(500); // turn LED off: digitalWrite(ledPin0, LOW); delay(500); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin2, LOW); delay(500); } else { // turn LED on: digitalWrite(ledPin1, HIGH); delay(500); digitalWrite(ledPin1, HIGH); delay(500); digitalWrite(ledPin0, HIGH); delay(500); // turn LED off: digitalWrite(ledPin2, LOW); delay(500); digitalWrite(ledPin1, LOW); delay(500); digitalWrite(ledPin0, LOW); delay(500); } }
3 Switchs and a RGB LED
What do you need:
- Arduino
- Breadboard
- 1 RGB LED
- 3 220 Ohm Resistors
- 3 10 Kilohm Resistors
- 3 switch
- Wires
RGB LED that’s included in your Arduino kit has a common cathode (ground). You can read more about it here.
Here is what RGB LED pins in Arduino Starter Kit look like:
Code:
// set pin numbers: int buttonPin0 = 7; // the number of the first pushbutton pin int buttonPin1 = 6; // the number of the second pushbutton pin int buttonPin2 = 5; // the number of the third pushbutton pin int ledPin0 = 13; // the number of the first LED pin int ledPin1 = 12; // the number of the second LED pin int ledPin2 = 11; // the number of the third LED pin int buttonState0 = 0; //the first button state int buttonState1 = 0; //the second button state int buttonState2 = 0; //the third button state // variables will change: void setup() { // initialize the LED pins as an output: pinMode(ledPin0, OUTPUT); pinMode(ledPin1, OUTPUT); pinMode(ledPin2, OUTPUT); // initialize the pushbutton pins as an input: pinMode(buttonPin0, INPUT); pinMode(buttonPin1, INPUT); pinMode(buttonPin2, INPUT); } void loop() { buttonState0 = digitalRead(buttonPin0); //read the first button state buttonState1 = digitalRead(buttonPin1); //read the second button state buttonState2 = digitalRead(buttonPin2); //read the third button state if (buttonState0 == 1) { //if the first button is pressed digitalWrite(ledPin0, HIGH); //turn on the first LED } else { //if the first button is not pressed digitalWrite(ledPin0, LOW); //turn off the first LED } if (buttonState1 == 1) { //if the second button is pressed digitalWrite(ledPin1, HIGH);//turn on the second LED } else { //if the second button is not pressed digitalWrite(ledPin1, LOW);//turn off the second LED } if (buttonState2 == 1) { //if the third button is pressed digitalWrite(ledPin2, HIGH);//turn on the third LED } else { //if the third button is not pressed digitalWrite(ledPin2, LOW);//turn off the second LED } }