Embedded System Experiment #2
Embedded System Experiment #2
STRUCTURES
LEARNING OUTCOMES:
After completing this experiment the students will be able to:
1. Apply the millis() function on a Arduino program.
2. Implement a debounce to validate a button press
3. Use edge detection in reading analog data.
4. Demonstrate INPUT_PULLUP application in Arduino.
5. Implement the different control structures available in the Arduino programming
language.
PROCEDURE
1. Attach a 220-ohm resistor to pin 13 of your Arduino board.
2. Connect the positive side of the LED to the resistor, and the negative side of the LED to
the ground.
3. Plug your Arduino board to your computer and start the Arduino program.
4. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
CODE:
const int ledPin = 13; if(currentMillis -
previousMillis > interval) {
int ledState = LOW; previousMillis = currentMillis;
long previousMillis = 0;
long interval = 1000; if (ledState == LOW)
ledState = HIGH;
void setup() { else
pinMode(ledPin, OUTPUT); ledState = LOW;
}
digitalWrite(ledPin, ledState);
void loop() }
{ }
unsigned long currentMillis = millis();
PROCEDURE:
1. Connect three wires to the Arduino board.
CODE:
const int buttonPin = 2; Serial.print("number of button
const int ledPin = 13; pushes: ");
Serial.println(buttonPushCounter);
int buttonPushCounter = 0; }
int buttonState = 0; else {
int lastButtonState = 0; Serial.println("off");
void setup() { }
pinMode(buttonPin, INPUT); }
pinMode(ledPin, OUTPUT); lastButtonState = buttonState;
Serial.begin(9600);
} if (buttonPushCounter % 4 == 0) {
void loop() { digitalWrite(ledPin, HIGH);
buttonState = digitalRead(buttonPin); } else {
digitalWrite(ledPin, LOW);
if (buttonState != lastButtonState) { }
if (buttonState == HIGH) { }
buttonPushCounter++;
Serial.println("on");
PROCEDURE:
1. Connect three wires to the Arduino board. The first goes from one leg of the pushbutton
through a pull-down resistor (here 10k) to ground. The second goes from the
corresponding leg of the pushbutton to the 5 volt supply. The third connects to a digital
I/O pin 2 of the Arduino board which reads the button's state.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.
CODE:
const int buttonPin = 2;
const int ledPin = 13; void loop() {
intledState = HIGH; int reading = digitalRead(buttonPin);
intbuttonState;
intlastButtonState = LOW; if (reading != lastButtonState) {
lastDebounceTime = millis();
longlastDebounceTime = 0; }
longdebounceDelay = 50;
if ((millis() - lastDebounceTime)
void setup() { >debounceDelay) {
pinMode(buttonPin, INPUT); if (reading != buttonState) {
pinMode(ledPin, OUTPUT); buttonState = reading;
digitalWrite(ledPin, ledState); if (buttonState == HIGH) {
} ledState = !ledState;
PROCEDURE:
1. Connect two wires to the Arduino board. The black wire connects ground to one leg of
the pushbutton. The second wire goes from digital pin 2 to the other leg of the
pushbutton.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.
CODE:
void setup(){ pinMode(2, INPUT_PULLUP);
Serial.begin(9600); pinMode(13, OUTPUT);
PROCEDURE:
1. Connect three wires to the Arduino board. The black wire connects ground to leftmost
leg of the potentiometer. The second wire goes from the 5V to the rightmost leg of the
potentiometer and the last wire connects the center leg of the potentiometer to the
analog pin 0 of the Arduino board.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.
CODE:
const int analogPin = A0; int analogValue = analogRead(analogPin);
const int ledPin = 13; if (analogValue > threshold) {
const int threshold = 400 digitalWrite(ledPin, HIGH);
} else {
void setup() { digitalWrite(ledPin, LOW);
pinMode(ledPin, OUTPUT); }
Serial.begin(9600); Serial.println(analogValue);
} delay(1);
void loop() { }
Figure 11. Circuit diagram of Experiment 2F Figure 12. Circuit diagram of Experiment 2F
PROCEDURE:
1. Connect the positive side of each of the LED to the 2, 3, 4, 5 and 6 digital I/O pins of the
Arduino in series through 220 resistors.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.
CODE:
void setup() { if (Serial.available() > 0) {
Serial.begin(9600); int inByte = Serial.read();
for (int thisPin = 2; thisPin < 7; switch (inByte) {
thisPin++) { case 'a':
pinMode(thisPin, OUTPUT); digitalWrite(2, HIGH);
} break;
} case 'b':
digitalWrite(3, HIGH);
void loop() { break;
Figure 13. Circuit diagram of Experiment 2G Figure 14. Circuit diagram of Experiment 2G
PROCEDURE:
1. Connect the photoresistor to the analog pin 0 using a voltage divider circuit, A 10k
resistor makes up the other side of the voltage divider, running from analog 0 to ground.
The other side of the photoresistor is connected to the 5V pin of the Arduino board.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.
Figure 15. Circuit diagram of Experiment 2H Figure 16. Circuit diagram of Experiment 2H
PROCEDURE:
1. Connect the positive side of each of the LED to the 2, 3, 4, 5, 6 and 7 digital I/O pins of
the Arduino in series through 220 resistors.
2. Plug your Arduino board to your computer and start the Arduino program.
CODE:
int timer = 100; digitalWrite(thisPin, HIGH);
delay(timer);
void setup() { digitalWrite(thisPin, LOW);
for (int thisPin = 2; thisPin < 8; }
thisPin++) {
pinMode(thisPin, OUTPUT); for (int thisPin = 7; thisPin >= 2;
} thisPin--) {
} digitalWrite(thisPin, HIGH);
delay(timer);
void loop() { digitalWrite(thisPin, LOW);
for (int thisPin = 2; thisPin < 8; }
thisPin++) { }
Figure 17. Circuit diagram of Experiment 2I Figure 18. Circuit diagram of Experiment 2I
CODE:
const int sensorPin = A2; sensorValue = map(sensorValue, sensorMin,
const int ledPin = 9; sensorMax, 0, 255);
const int indicatorLedPin = 13; sensorValue = constrain(sensorValue, 0,
const int buttonPin = 2; 255);
Figure 19. Circuit diagram of Experiment 2J Figure 20. Circuit diagram of Experiment 2J
PROCEDURE:
1. Connect the positive side of each of the LED to the 2, 3, 4, 5, 6 and 7 digital I/O pins of
the Arduino in series through 220 resistors.
2. Plug your Arduino board to your computer and start the Arduino program.
3. Choose the Arduino board that you are you using in the Tools > Boards menu, then
choose the COM port you are using in the Tools > Serial Port menu.
4. Encode and verify the program below, if no errors occurred download the program into
your Arduino board.
CODE:
int timer = 100; for (int thisPin = 0; thisPin < pinCount;
int ledPins[] = {2, 7, 4, 6, 5, 3}; thisPin++) {
int pinCount = 6; digitalWrite(ledPins[thisPin], HIGH);
delay(timer);
void setup() { digitalWrite(ledPins[thisPin], LOW);
for (int thisPin = 0; thisPin < pinCount; }
thisPin++) { for (int thisPin = pinCount - 1;
pinMode(ledPins[thisPin], OUTPUT); thisPin >= 0; thisPin--) {
} digitalWrite(ledPins[thisPin], HIGH);
} delay(timer);
digitalWrite(ledPins[thisPin], LOW);
void loop() { }
}