Molemi IoT
  • Space farming using food computers for sustainable farming
  • Introduction to Arduino
    • Introduction to Electronics
    • The Breadboard
  • Controlling LEDs with an Arduino
    • The Code
    • Adding a button to control the LEDs
      • The Code
  • Introducing Sensors
    • The CO2 and air quality sensor
    • The Code
      • Explaining the Code
    • Light Intensity Sensor
  • Introducing the NodeMCU
    • Getting Started
      • A simple test for ESP8266
        • The Code
    • A light bulb switch using NodeMCU and the Blynk app
      • Setup Blynk on your Smartphone
      • The Code
    • Controlling a Centurion gate Motor with NodeMCU a 2-channel relay
      • Installing Blynk in Arduino IDE
    • Read and display the water flow sensor on Blynk
      • Setting up Blynk
      • The Code
        • Explaining the code
    • Read and Display temperature sensor readings with NodeMCU.
      • Installing libraries for DS18B20 temperature sensor.
      • Displaying the sensor readings on the serial port
        • Getting temperature readings from different DS18B20 sensors.
          • The Code
            • Display sensor readings on a web server
              • Build the web server
                • Designing and building the web page
                • The Code
    • Display the DHT11 sensor reading on a web server using NodeMCU.
      • Monitoring Room Temp & Humidity using Blynk
      • Installing DHT library on the ESP8266
        • Installing the Asynchronous Web Server library
          • The Code
          • Designing and building the web page
  • Data Science for Farming
    • Getting Started with Colaboratory
    • Introduction to Python for DS using Colab
  • Machine Learning for Farming
  • Molemi Personal Food Computer
    • Bill of Materials
  • Setting up WaziGate on a Raspberry Pi
  • Setting up DHT11 on Raspberry Pi
  • Using Telegram To Control Outputs
Powered by GitBook
On this page

Was this helpful?

  1. Controlling LEDs with an Arduino
  2. Adding a button to control the LEDs

The Code

Start by adding a new variable to the start of the program:

int button = 12; // switch is on pin 12

void setup() {
  // put your setup code here, to run once:

}

void loop() {
  // put your main code here, to run repeatedly:

}

Now, in the setup function, add a new line to declare the switch as an input. I’ve also added a single line to start the traffic lights in the green stage. Without this initial setting, they would be turned off.

int button = 12; // switch is on pin 12

void setup() {
  // put your setup code here, to run once:
  
  pinMode(button, INPUT);
  digitalWrite(green, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:

}

In the loop function add the following:

int button = 12; // switch is on pin 12

void setup() {
  // put your setup code here, to run once:
  
  pinMode(button, INPUT);
  digitalWrite(green, HIGH);

}

void loop() {
  // put your main code here, to run repeatedly:
  
  if (digitalRead(button) == HIGH){
        delay(15); // software debounce
        if (digitalRead(button) == HIGH) {
            // if the switch is HIGH, ie. pushed down - change the lights!
            changeLights();
            delay(15000); // wait for 15 seconds
        }
    }
}

PreviousAdding a button to control the LEDsNextIntroducing Sensors

Last updated 5 years ago

Was this helpful?