This extends the previous lesson by adding a button to control the LED’s state.

The Grove Beginner Kit For Arduino includes an Arduino Uno board with preconnected devices. The Grove lessons supporting this present coding using the Arduino IDE. These pages present the same lessons using the Codecraft IDE that uses the Block style of coding with specific Grove Arduino blocks.

The first thing we need to know is that the input of the button is a digital signal, and there are only two states, 0 or 1, so we can control the output based on those two states.

  • Practice: Use button to turn ON and OFF the LED module

Grove Beginners Kits Components

Lesson3

                Component                   Interface                   Pins/Address                  
LED Digital D4
Buzzer Digital D5
OLED Display 0.96” I2C I2C, 0x78(default)
Button Digital D6
Rotary Potentiometer Analog A0
Light Analog A6
Sound Analog A2
Temperature & Humidity Sensor Digital D3
Air Pressure Sensor I2C I2C, 0x77(default) / 0x76(optional)
3-Axis Accelerator I2C I2C, 0x19(default)

Components used for this lesson are highlighted.

  • Components Involved

    1. Grove Beginner Kit
    2. Grove LED
    3. Grove Button

2. Implement the code block.

Lesson2

How to create this …

Watch the video on Youtube:

3. Run the app

Download the app to the board, as in previous lesson.

Discussion

We use an if..then..else code block:
if...then...else code block)

The conditional part:
if button pressed

is equivalent to the following code:

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);```
   if (buttonState == HIGH) {

Note again, there is some implicit code here, the buttonState variable.

So the if…then…else code block
if button press then ... else ...

is equivalent to the following code:

 if (buttonState == HIGH) {
  digitalWrite(ledPin, HIGH);
  } else {
  digitalWrite(ledPin, LOW);
  }
}

A short delay is added to each loop so that the microprocessor isn’t always busy doing nothing.

Outcome:

Pressing the button will turn the LED module on.


 TopicSubtopic
   
 This Category Links 
Category:Grove Arduino Index:Grove Arduino
  Next: > Grove Beginner Kit For Arduino
<  Prev:   Grove Beginner Kit For Arduino