week2 : introduction to Arduino

April 28th, 29th, 30th, 10:00-17:00 eLab

DAY 1:

slide_front

Tutorial on Arduino Installation here >>

Blink

Blink

What we cover:
variable
int
pinMode()
digitalWirte

Challenge:
Add second LED
Blink them in alternative sequence
1st LED on >> off >> on >> off
1nd LED off >> on >> off >> on

Push Button

Button

inside of a push button

How to connect push button
pull up resistor (10k ohm or bigger)
digital_switch

button_onoff_conn

What we cover:
digitalRead()
if
else

Challenge:
Push button the first time >> turn on the LED
Push button the second time >> turn off the LED

answer——

// constants won’t change. They’re used here to
// set pin numbers:
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin

// variables will change:
int buttonState = 0; // variable for reading the pushbutton status

int PressCount=0;

int lastButtonState=0;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the pushbutton pin as an input:
pinMode(buttonPin, INPUT);
}

void loop(){
lastButtonState=buttonState;

// read the state of the pushbutton value:
buttonState = digitalRead(2);

// check if the pushbutton is pressed.
// if it is, the buttonState is HIGH:
if (lastButtonState != buttonState){
if (buttonState == HIGH) {
// count up PressCount
PressCount = PressCount+1;
}
}

if (PressCount == 1){
digitalWrite(ledPin,HIGH);
}

if (PressCount==2){
digitalWrite(ledPin,LOW);
PressCount=0;
}

}

———-

if you could do this,
Push button the first time >> turn the 1st LED on
Push button the second time >> turn the 2nd LED on
Push button the third time >> turn off all the LEDs

You can download the Arduino Sketch and breadboard connection on Fritzing we used in the course from here
https://github.com/mikst/101

Leave a Reply