0% found this document useful (0 votes)
15 views

Assignment1 IOT102

Assignment1_IOT102

Uploaded by

ngohonghai712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Assignment1 IOT102

Assignment1_IOT102

Uploaded by

ngohonghai712
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Name: Ngo Hong Hai

Mssv: CE171752

Link project Tinkercad: https://www.tinkercad.com/things/2bvOkYJULzy-assignment-1-ngo-hong-hai-


ce171752?sharecode=zAm9MoABYEeppf3twPtxCvpJJErYuOQyUmySaJGk3bU

Circuit

Code
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to

// Variables will change:


int timer = 100; // The higher the number, the slower the timing.
int buttonState = 0; // Current state of the button
int lastButtonState = 0; // Previous state of the button
int buttonPushCounter = 0; // Counter for the number of button presses

void setup() {
// Initialize the push button pin as input
pinMode(buttonPin, INPUT);

// use a for loop to initialize each pin as an output:


for (int thisPin = 3; thisPin < 9; thisPin++) {
pinMode(thisPin, OUTPUT);
}

// Initialize serial communication at 9600 baud rate


Serial.begin(9600);

// Turn off all LEDs initially


turnOffLEDs();
}

void loop() {
// Read the state of the push button
buttonState = digitalRead(buttonPin);

// Check for a change in the button state


if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
// Increment the button press counter and print a message to the
serial monitor
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button presses: ");
Serial.println(buttonPushCounter);
} else {
Serial.println("off");
}
// delay a little bit
delay(50);
}
// Save the current state as the last state, for next time through the
loop
lastButtonState = buttonState;

// Check the button press counter to determine LED patterns or "Knight


Rider" mode
if (buttonPushCounter >= 6) {
// reset the button push counter and turn off all LEDs
buttonPushCounter = 0;
turnOffLEDs();
} else if (buttonPushCounter == 1) {
// Turn on all LEDs
turnOnLEDs();
} else if (buttonPushCounter == 2) {
// turn off all LEDs
turnOffLEDs();
} else if (buttonPushCounter == 3) {
// turn on all odd LEDs
turnOnOddLEDs();
} else if (buttonPushCounter == 4) {
// turn on all even LEDs
turnOnEvenLEDs();
} else if (buttonPushCounter == 5) {
// turn off all LEDs before running "Knight Rider" mode
turnOffLEDs();
// run "Knight Rider" mode
knightRider();
}
}

/* Function to turn off all LEDs */


void turnOffLEDs() {
// loop from the lowest pin to the highest:
for (int thisPin = 3; thisPin < 9; thisPin++) {
// turn the pin off:
digitalWrite(thisPin, LOW);
}
}

/* Function to turn on all LEDs */


void turnOnLEDs() {
// loop from the lowest pin to the highest:
for (int thisPin = 3; thisPin < 9; thisPin++) {
// turn the pin on:
digitalWrite(thisPin, HIGH);
}
}

/* Function to turn on all odd LEDs */


void turnOnOddLEDs() {
for (int thisPin = 3; thisPin < 9; thisPin += 2) {
digitalWrite(thisPin, HIGH);
}
}

/* Function to turn on all even LEDs */


void turnOnEvenLEDs() {
// turn off all odd LEDs
for (int thisPin = 3; thisPin < 9; thisPin += 2) {
digitalWrite(thisPin, LOW);
}
// turn on all even LEDs
for (int thisPin = 4; thisPin < 9; thisPin += 2) {
digitalWrite(thisPin, HIGH);
}
}
/* Function to toggle the LEDs in "Knight Rider" mode */
void knightRider() {
bool knightRiderMode = true; // Variable to check "Knight Rider" mode
while(knightRiderMode) {
// loop from the lowest pin to the highest:
for (int thisPin = 3; thisPin < 9; thisPin++) {
// Regularly check the button status, then compare with the previous
status
// to update knight rider mode. When the two states are different,
// immediately exit the loop and execute the function later
buttonState = digitalRead(buttonPin);
knightRiderMode = (buttonState == lastButtonState) ? true :
false;
if (!knightRiderMode) break;
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);

// loop from the highest pin to the lowest:


for (int thisPin = 8; thisPin >= 3; thisPin--) {
// Regularly check the button status, then compare with the previous
status
// to update knight rider mode. When the two states are different,
// immediately exit the loop and execute the function later
buttonState = digitalRead(buttonPin);
knightRiderMode = (buttonState == lastButtonState) ? true :
false;
if (!knightRiderMode) break;
// turn the pin on:
digitalWrite(thisPin, HIGH);
delay(timer);
// turn the pin off:
digitalWrite(thisPin, LOW);

You might also like