Two buttons, one Arduino

Two buttons, one Arduino

Red green button, red green light

Theo van der Sluijs
·May 25, 2022·

4 min read

Subscribe to my newsletter and never miss my upcoming articles

Play this article

What is an Arduino

Arduino is an open-source electronics platform with a full software suite. Arduino boards are used for a wide range of purposes, including electronics projects, robotics, and wearables. It is an open-source hardware and software platform that was created in 2005.

Arduino boards are among the most popular platforms for hardware hacking and electronics prototyping. They are easy to use and provide an inexpensive way to get started in the world of physical computing. The Arduino software is free and open-source.

The Arduino board can be programmed from the Arduino IDE in many different programming languages, including C++ and Python. The IDE runs on all major operating systems, so programmers can develop on their computer without having to use a different OS than they're used to or worry about compatibility issues with other software they might need to use.

What can you do with an Arduino

The Arduino board is a microcontroller board. It has an on-board processor, memory, and input/output pins. Arduino boards are programmed using the Arduino programming language (based on Wiring) and the Arduino development environment (based on Processing). The boards can be built by hand or purchased preassembled; either way, the end result is a circuit board with an Atmel 8-bit AVR microprocessor as its core.

The main difference between Arduino boards and other microcontrollers like the PIC or AVR families, is that they are designed to be cheap in price but easy to use for both novice and

The microcontroller on the Arduino board can be programmed what to do by sending simple commands over a serial connection. The board can be powered by either the USB cable or an external power supply.

Arduino boards are used for a wide range of purposes, including electronics projects, robotics, and wearables.

The Arduino board can be programmed from the Arduino IDE in many different programming languages, including C++ and Python. The IDE runs on all major operating systems, so programmers can develop on their computer without having to use a different OS than they're used to or worry about compatibility issues with other software they might need to use.

Building towards a Solar Inverter connector

So I'm quite into energy pricing these days. I've got a complete variable contract so prices change every hour. Even to minus pricing amounts. Yes! Energy can be cheap, for free or you even get money to use it. There is so much energy every now and then that the price can go below zero!

So I've started soldering last week to be able to solder connectors onto an Arduino (or raspberry pi). So the next step is to actually to be able to turn on or turn off anything. Easiest way to do this is with an Arduino, a breadboard 1 led and a button.

There is an example in the Arduino IDE to turn on or off a led with a button.

But as I have two inverters I wanted more! And I created a small script to turn 2 leds on and off.

The next script works by pressing one of the buttons, and when releasing the button it goes off again.

I will change the script next week to press for turning the light on, press again to turn it off.

So here's my script

const int buttonPinRed = 2; 
const int buttonPinGreen = 4;
const int ledPinRed = 11;  
const int ledPinGreen = 12;

// variables will change:
int buttonStateRed = 0;         // variable for reading the pushbutton status
int buttonStateGreen = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPinRed, OUTPUT);
  pinMode(ledPinGreen, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPinRed, INPUT);
  pinMode(buttonPinGreen, INPUT);
}

void loop() {
  // read the state of the pushbutton value:
  buttonStateGreen = digitalRead(buttonPinGreen);
  buttonStateRed = digitalRead(buttonPinRed);

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonStateGreen == HIGH) {
    // turn LED on:
    digitalWrite(ledPinGreen, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPinGreen, LOW);
  }

  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (buttonStateRed == HIGH) {
    // turn LED on:
    digitalWrite(ledPinRed, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPinRed, LOW);
  }
}

Did you find this article valuable?

Support Theo van der Sluijs by becoming a sponsor. Any amount is appreciated!

See recent sponsors Learn more about Hashnode Sponsors
 
Share this