In the previous lesson, we have talk about how to get input from push button using Arduino. Today we are going to talk about work with Arduino toggle switch and Arduino variable types. Let’s get started.
You might see this toggle switch. Toggle switch is very common
switch type in robotics and electronics. Below is the circuit diagram for toggle
switch Arduino projects. This circuit diagram is much more similar with before circuit
diagram.
Code is below. You can get the code by pressing code button.
You can tryout how this thing is going to work by pressing start simulation button.
Also, you can tryout some different codes using below iframe.
In the void setup of this code, we initialized 2nd
pin as input and 8th and 9th pins as outputs. So, 2nd
pin is connected to toggle switch and 8th and 9th pins
are connected to LEDs to get outputs. Inside the void loop, I created integer
variable named state for getting toggle switch digital readings. You already
know, if condition is must needed for this project. In the if condition, I put
to make 9th pin low and 8th pin high. So, when I turn on
the toggle switch 8th pin blink and 9th pin LED goes off.
In the else part, it says about what to do when toggle switch off statement. I
put to turn on 9th pin LED and turn off 8th pin LED in
that part. Result is 9th pin LED is blinking always and when you
turn on the toggle switch 8th pin LED is turn on and 9th
pin LED is going off. And at last, I put 1 millisecond delay for make more
smoother actions.
And Those are about main Arduino switch. There are other
switches in this world. But most Arduino switch types are those things. Now you
know how to control switch like push button, toggle switch. Now we are going to
talk about Arduino variables.
Variables
What is variable?
Variable is small room to store some data. Actually, it’s not
room. But some space. You can make variable named “mypin” in Arduino and put
that “mypin” in every place of code where it needs. Then you can define “mypin”
is the 9th pin of Arduino in one place. Now you understood the basic
use of Arduino variables. There are many other uses for Arduino variables. You
can put any kind of value to variables in “PHP” programming language. But you
can’t do it in Arduino. You have to define the variable type. Here are some Arduino
variable types.
- int – int is named as integers. Integers are used for storing numbers. It stores 16-bit value. Its number range is -32,768 to 32,767. So, this integer has limits. But it’s good more small works like digital read.
- char – Char is used to store single character. You can’t put two numbers in it. You can put 1,2,3,4,5,6,7,8,9,a,b,c,d,e,f,g,h,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z in this char variable.
- boolean – boolean is known as bool in Arduino. Bool is used to store only true or false conditions. You can’t store any other thing in this variable.
- float – float is used to store any kind of numbers in Arduino. It has higher range such as -3.4028235e+38 to 3.4028235e+38.
- byte – byte is used to store binary values. You can store any kind of binary value in this variable type.
- double – double is actually same as float. Its capacity is also same as float. But only different is double can store decimals up to 15 digits. And float can only 6.
- string – string is very common variable type in Arduino. It used to store any kind of word. English letters and etc.