Group B

Q5: Write a program using Arduino to control LED (One or more ON/OFF) or Blinking.

On off LED Control & Blinking LED

Solution and implementation for Q5 from Internet of Things (iotl).

on_off.c++ Download
// a) On / OFF LED 

#include <stdio.h>
#include <stdlib.h>

const int button = 8;    // GPIO 8 for the button
const int led = 7;       // GPIO 7 for the LED

int ledFlag = 0;         // LED status flag

void setup() {
    pinMode(button, INPUT);     // Define button as input
    pinMode(led, OUTPUT);       // Define LED as output
    digitalWrite(led, LOW);     // Ensure LED is off at startup
}

void loop() {
    if (digitalRead(button) == HIGH) {  // If button is pressed
        if (ledFlag == 0) {
            ledFlag = 1;                // Set flag
            digitalWrite(led, HIGH);    // Turn LED on
        } else {
            ledFlag = 0;                // Reset flag
            digitalWrite(led, LOW);     // Turn LED off
        }
        delay(1000);                    // Debounce delay
    }
}
blinking.c++ Download
// b) Blinking Led

int ledPin = 13;

void setup() {
  pinMode(ledPin, OUTPUT);
}

void loop() {
  digitalWrite(ledPin, HIGH);
  delay(1000);

    // 1000 milliseconds = 1 seconds

  digitalWrite(ledPin, LOW);
  delay(1000);
}

Other Questions in Internet of Things

See All Available Questions
Download