IOT102 - Lab 1 - Lab5
IOT102 - Lab 1 - Lab5
IOT102 - Lab 1 - Lab5
Resistors
a few of the most commonly used electronic
symbols in the US.
PROGRAMMING ARDUINO -
BASIC FUNCTIONS
ARDUINO LOGIC
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
if (someCondition)
{
// do stuff if the condition is true
}
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
{ 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);
}
}