PROJECT Nº2: Lamp with a switch

Learn to build and program a lamp that turns on and off at the push of a button, using the components of the Maker Control Kit.

For this project, you will use the button and the white LED. You will turn the lamp on and off using the button, as if it were a switch.

LEVEL OF DIFFICULTY:  Beginner.

DURATION OF THE EXERCISE: 30 min.

MATERIALS:

  • 1 Button
  • 1 White LED
  • 1 4in1 Build&Code control board
  • 1 USB – Micro USB cable
  • Computer
  • 1 Clear or white plastic cup
  • Material to make the lamp structure
  • Adhesive

What is an LED?

An LED is a semiconductor electrical component (diode). When a small current passes through it, the diode emits light.

What is a button?

A button or switch is an electric operator that, when pushed, allows electrical current to flow, and when it is no longer pressed, it interrupts it.

CONNECTIONS:

  1. Connect the white LED to digital port 9 on the 4in1 Build&Code control board.
  2. Connect the button to digital port 4 on the 4in1 Build&Code control board.

Look at the colors of the cables and the colors of the terminals on the 4in1 Build&Code control board to guide you. Each cable should be connected to its color.

BUILDING THE STRUCTURE:

To build the lamp structure, you will use 150 mm x 18 mm wooden sticks, a clear or white plastic cup, and hot silicone to make all the structure’s joints. Download the quick setup guide and follow the steps shown.

PROGRAMMING CODE:

The program consists in controlling the behavior of the LED and the button. Furthermore, it includes a variable called status variable. The status variable lets you control the pulses that the button makes, in order to know if it should turn the LED on or off.

Every time that you push the button, the value of the status variable increases by 1. If the status variable is equal to 1, the LED turns on. If the status variable is equal to 2, the LED turns off, and the status variable goes back to 0 so that the cycle starts over again.

You can do this activity by using Arduino and Bitbloq software, as well as other compatible block programming software. Below you will find the programming code needed for each software.

Arduino Code

  1. Download the Arduino software and go through the installation process.
int PLED = 9; // LED connected to digital port 9
int PButton = 4, ValueButton; // Button connected to digital port 4 // ValueButton button reading variable
int Status = 0; // Status variable

void setup() {
  // put your setup code here, to run once:
  pinMode (PLED, OUTPUT); // Configure the LED as an output signal
  pinMode (PButton, INPUT); // Configure the button as an input signal
}

void loop() {
  // put your main code here, to run repeatedly:
  ValueButton = digitalRead(PButton); // Read the button value
  if(ValueButton == HIGH) // If the button is pushed
  {
    Status = Status +1; // Status variable increased by +1
    delay (300); // 0.3 sec wait
  }
  if (Status == 1) // If the status variable is equal to 1
  {
    digitalWrite(PLED, HIGH); // LED = ON
  }
  if (Status == 2) // If the status variable is equal to 2
  {
    digitalWrite(PLED, LOW); // LED = OFF
    Status = 0; // Status variable equal to 0
  }
}

Code for compatible block programming software

  1. Download the software and go through the installation process.
  2. Open the program, and once in, copy the following code:                                                                                                             
  3. Configure and load the code, following the instructions given in the First Steps for the 4in1 Build&Code board document.

BitBloq Code

  1. Get the BitBloq software.
  2. Open the BitBloq program, and once in, copy the following code:
    • Hardware

    • Software

3. Configure and load the code, following the instructions indicated in the First Steps for the 4in1 Build&Code board document.

RESULT OF THE EXERCISE:

You have built a lamp that turns on at the push of a button and turns off when the button is pushed again.

0 0

0