Hello everyone, after a long while I didn't post anything, its time to get back to the routine.
In today's tutorial, I will teach you how to use buttons on Arduino.
I know that some of you are not familiar with Arduino, so I recommend you to go on arduino.cc website and start learning this fantastic language.
" Arduino is an open-source electronics (raised, flat supporting surface) based on easy-to-use hardware and software. Arduino boards can read inputs - light on a sensor, a finger on a button, or a Twitter message - and turn it into an output - activating a motor, turning on an LED, publishing something online. You can tell your board what to do by sending a set of instructions to the microcontroller on the board. To do so you use the Arduino programming language (based on Wiring), and the Arduino Software (IDE), based on Processing. "
information about Arduino, was brought from arduino.cc website
Be aware of the fact that youll need this cable either (must have):
There are several kinds of cables, but in this tutorial I am going to use this one.
Cabling board
Cables
Button
this button has 2 legs, most of the buttons in the market have 4. In this tutorial you'll understand how to wire a button with 2 legs, but it is the same with 4 legs.
the resistor colors are not matters in THIS VERY TUTORIAL
red is +, black is -. (Doesnt realy matters, just remember this).
Ok, so the red cable is connected to the 5V (default), place on the Arduino board.
And the black one, is connected to the ground (GND).
and put it like so:
As you can see, your resistor is now connected to the RED row, (+), which means it gets 5Volts.
We connected the resistor to the left leg of the button.
Put the bigger leg on the right, and the smaller on the left. And remember it. It is important.
Put them like so:
The orange one is connected to 'PORT D', hall number 7.
The blue cable, is connected to the black ROW (-), which means its connected to the ground.
I recommend code your own sketch, before continuing.
GOOD! Lets start.
First thing youll see is:
Ok, I recommend first to define the '7' pin as a button.
#define button 7
this line of code comes before every void, function, or integers you wrote.
This line of code is actually defining 7 as button. They are the same thing now.
You will understand later on.
void setup() {
// put your setup code here, to run once:
DDRD = 0xFF;
Serial.begin(9600);
pinMode(button, INPUT);
}
This is your default settings, it will run only 1 time. (We dont need it to run more then that).
The void loop, is going to run reapeatedly (non-stop).
I hope you learned IF-Else statemantes, if you didnt so learn it because it is important for this project.
First of all, we define a new integer.
but = short for a button
void loop() {
// put your main code here, to run repeatedly:
int but = digitalRead(button);
if (but == HIGH) {
Serial.println(" PUSH IT ! JUST DO IT ! ");
} else {
Serial.println(" PLS STOP ! YOU ARE PUSHING TOO HARD. ");
}
}
Buttons are very important in Arduino. For example, drinking machine, can be also be working by buttons.
Buttons are awesome, how you learned something.