IOT102 - Lab 1 - Lab5

Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Arduino UNO

Resistors
a few of the most commonly used electronic
symbols in the US.
PROGRAMMING ARDUINO -
BASIC FUNCTIONS
ARDUINO LOGIC

 • setup () - here we put all the commands that must run


once when the unit is turned on. Variable value
initializations are usually entered and, in any case, the
identification of the inputs/outputs that we will use
 • loop () - here we write our program. The commands that
exist will run and when it reaches the end, the loop () will
resume, continuing from the start, and again. This will
happen continuously, as long as Arduino is running or until
the reset key is pressed.
LAB 1: blink LED

Hardware Required
1. Arduino UNO
2. Led 3mm
3. Resistor 330R
4. Breadboard
5. Wire
// LAB 1 - LED Flasher
int ledPin = 13;
void setup()
{ pinMode(ledPin, OUTPUT);
// OUTPUT means we are sending some data out of uC
}
void loop()
{ digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
delay(1000);
LAB 1: blink LED

// LAB 1 - LED Flasher


int ledPin = 13;
void setup()
{
1. pinMode(ledPin, OUTPUT);
// OUTPUT means we are sending some data out of uC
}
void loop()
{
1. digitalWrite(ledPin, HIGH);
2. delay(1000);
3. digitalWrite(ledPin, LOW);
4. delay(1000);
}
LAB 1: blink LED
int led1Pin = 13;
int led2Pin = 12;
void setup()
{
1. pinMode(led1Pin, OUTPUT);
2. pinMode(led2Pin, OUTPUT);
}
void loop()
{
1. digitalWrite(led1Pin, HIGH);
2. digitalWrite(led2Pin, LOW);
3. delay(1000);
4. digitalWrite(led1Pin, LOW);
5. digitalWrite(led2Pin, HIGH);
6. delay(1000);
}
LAB 2: reading the value of analog input
=> change the blink rate of the LED
Hardware Required
1. variable resistor 10K
2. Led 3mm
3. Wire
4. Breadboard
5. Arduino
LAB 2: reading the value of analog input
=> change the blink rate of the LED
 A potentiometer is a variable resistance, which we can read into the
Arduino board as an analog value.

// select the input pin for the potentiometer


int potPin = 2;
// variable to store the value coming from the sensor
int potValude = 0;
// read the value from the sensor
potValude = analogRead(potPin);
LAB 2: reading the value of analog input
and change the blink rate of the LED
// reading the valude of potentiometer
int ledPin = 13;
int potPin = 2;
int potValude = 0;
void setup() {
1. pinMode(ledPin, OUTPUT);
2. // pinMode(potPin, INPUT); default Analog pins are INPUT => skip this instruction
}
void loop() {
1. potValude = analogRead (potPin);
2. digitalWrite(ledPin, HIGH);
3. delay(potValude);
4. digitalWrite(ledPin, LOW);
5. delay(potValude);
}
LAB 3: If Statement (Conditional Statement)
It allows you to make something happen or not, depending on whether a given condition is
true or not.

if (someCondition)
{
// do stuff if the condition is true
}

There is a common variation called if-else that looks like this:

if (someCondition)
{
// do stuff if the condition is true
}
else
{
// do stuff if the condition is false
}
LAB 3: If Statement (Conditional Statement)
There's also the else-if, where you can check a second condition if the first is false:

if (someCondition)
{
// do stuff if the condition is true
}
else if (anotherCondition)
{
// do stuff only if the first condition is false
// and the second condition is true
}
LAB 3: If Statement (Conditional Statement)

 turns on an LED if the value read on an analog input goes above a certain threshold.

 Hardware Required
1. Arduino or Genuino Board
2. Potentiometer
3. Led 3mm
4. Wire
5. Resistor 330R
LAB 3: If Statement (Conditional Statement)
 turns on an LED if the value read on an analog input goes above a certain threshold.
LAB 3: If Statement (Conditional Statement)
 turns on an LED if the value read on an analog input goes above a certain threshold.
const int potPin = A0;
const int ledPin = 13;
const int threshold = 400; // in the range of the analog input
void setup()
{
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop()
{
int analogValue = analogRead(potPin);
if (analogValue > threshold)
{
digitalWrite(ledPin, HIGH);
}
else
{
digitalWrite(ledPin, LOW);
}
Serial.println(analogValue); // skip this comment
delay(1); // delay in between reads for stability
}
LAB 4: Blink Without Delay
 Sometimes you need to do two or many things at once. In this case, you can't
use delay(), because Arduino pauses your program during the delay().
 For example: If the button is pressed while Arduino is paused waiting for
the delay() to pass, your program will miss the button press.

previousMillis currentMillis
to t
t(ms)

T = 1000 ms
to t
t(ms)

T = 1000 ms
LAB 4: Blink Without Delay

 Hardware Required
1. Arduino UNO
2. LED 3mm
3. 330 ohm resistor
LAB 4: Blink Without Delay
1. const int ledPin = 13; 14. if (currentMillis - previousMillis >= interval)
2. int ledState = LOW ; 15. {
3. unsigned long previousMillis = 0; 16. previousMillis = currentMillis;
4. // will store last time LED was updated 17. if (ledState == LOW)
5. const long interval = 1000; 18. {
6. // interval at which to blink (milliseconds) 19. ledState = HIGH;
7. void setup() 20. }
8. { 21. else
9. pinMode(ledPin, OUTPUT); 22. {
10. } 23. ledState = LOW;
11. void loop() 24. }
12. { 25. digitalWrite(ledPin, ledState);
13. unsigned long currentMillis = millis(); 26. }
27. }
LAB 4: Blink Without Delay
1. const int ledPin = 13; 14. if (currentMillis - previousMillis >= interval)
2. const int potPin = A2; 15. {
3. int ledState = LOW ; 16. previousMillis = currentMillis;
4. unsigned long previousMillis = 0; 17. if (ledState == LOW)
5. // will store last time LED was updated 18. {
6. int interval = 0; 19. ledState = HIGH;
7. // interval at which to blink (milliseconds) 20. }
8. void setup() 21. else
9. { 22. {
10. pinMode(ledPin, OUTPUT); 23. ledState = LOW;
11. } 24. }
12. void loop() 25. digitalWrite(ledPin, ledState);
13. { interval = analogRead(potPin); 26. }
14. unsigned long currentMillis = millis(); 27. }
LAB 5: Pushbuttons or switches

 Hardware Required
1. Arduino or Genuino Board
2. 6mm tact switch (pushbutton)
3. 10K ohm resistor
4. hook-up wires
5. breadboard
LAB 5: Pushbuttons or switches

1. int switchPin = 2; // Switch connected to digital pin 2


2. pinMode(switchPin, INPUT);
3. int var = digitalRead(switchPin); Serial.println(digitalRead(switchPin));
4. Serial.println(var); // Read the pin and display the value
5. if (var == LOW) { // check if the button is pressed
6. digitalWrite(ledPin, HIGH); }
7. if (var == HIGH) { // check if the button is not pressed
8. digitalWrite(ledPin, LOW); // turn LED off }

{ statements to perform if
if (test statement) { statements to perform if test is True} else
test is not True}
if ( val == LOW ) { ... } else { ... }
LAB 5: Pushbuttons or switches
1. int switchPin = 2; // Switch connected to digital pin 2
2. int ledPin = 13;
3. void setup ()
4. { pinMode(switchPin, INPUT);
5. Serial.begin(9600); }
6. void loop ()
7. {
8. int var = digitalRead(switchPin);
9. Serial.println(var);
10. if (var == LOW) { // check if the button is pressed
11. digitalWrite(ledPin, HIGH); }
12. if (var == HIGH) { // check if the button is not pressed
13. digitalWrite(ledPin, LOW);} // turn LED off
14. }
LAB 5: Pushbuttons or switches
const int buttonPin = 2;
const int ledPin = 13;
int buttonState = 0;
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(buttonPin, INPUT);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState == HIGH) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}

You might also like