Skip to content Skip to sidebar Skip to footer

Read Pin State on Arduino and Sending Output

On the same line of experiments, let'south now learn near logic states, as well as how to piece of work with data input devices.

!Important Data

Upwards to this betoken, our experience equally been besides one-sided. Blinking LEDs, making audio with the Cablegram, they are all considered output. Allow's change a little bit the approach and read an input signal.

The inputs are signals or external values sent to a system. Some of the common input components are buttons or switches. The keys on a keyboard, for case, are an input for your computer, because they send information to this system.

!Identifying the Components

The figure below shows the buttons on the Interface Nanoshield. They are connected to pin A0 on Arduino (further details on the next projects).

Interface Button

!Code Components

Let's introduce a new function, besides an addendum to if.

Digital Input: digitalRead([pivot])

The digitalRead() function is used to read the logic state at a pin. It is capable to tell wether the voltage at this pivot is high (~ 5V) or low (~ 0V) or, in other words, if the pivot is at logic land 1 or 0 (or HIGH/LOW). Notice that the digitalRead() function does not effectively measures the voltage at the pivot. It but tells us if the logic level is i or 0, which is sufficient when we work with digital systems.

There is only one parameter on digitalRead() - the number of the pivot yous want to read similar, for instance, digitalRead(A0) in order to read the logic state of the pin A0. The digitalRead() works with all Arduino pins from D2 to D13 and from A0 to A5 (the only exceptions are the pins A6 and A7).

On Arduino, by default, all the pins are already pre-configured equally input. This way, it is non necessary to configure it again to use the office digitalRead().

The digitalRead() function is different from the other ones, because it returns a value, which is the logic country of the pin. So, instead of just writing the call to the function and spring to the next line, we really need to use this function forth with other pieces of code.

The digitalRead() function will return ane of the two values: high or low. You can utilise this return value equally part of a conditional test within an if-statement. For instance:

if (digitalRead(pivot) == Depression) {     // The button is pressed, do something! }        

Otherwise, you tin can shop the value of digitalRead() inside a variable: variable_name = digitalRead(2);

If/Else: if( [status] ){ [consequence if truthful] } else { [outcome if false] }

Allow's use the extension of the if-argument announcement, the else. if / else lets united states of america take an action if the status is true and another activity if it is imitation.

Here is the structure of an if / else:

          if ( [condition] ) {     // The status is true, practise something. } else {     // Oops, the condition is simulated, do something else! }        

if / else is really useful when you lot are dealing with digital outputs, which can only exist in one of two states. If a push button is pressed, do something and, if it is not, practise another matter.

!Running on Arduino

Run the code below on your Arduino. After uploading the lawmaking, yous can press the RIGHT button on the Interface Nanoshield to plow on one of the LEDs. On the code below you lot tin check out the comments to empathise better how it works.

          //Pin connected to push button-button          int          buttonPin = A0;          int          led2 = A2;          //Variable to check the state          int          buttonState;          void                      setup          () {// Defining the push pin as inputpinMode(buttonPin,          INPUT);// Defines the green LED as outputpinMode(led2,          OUTPUT); }          void                      loop          ()  {// Checks if the button state has change          buttonState =digitalRead(buttonPin);if          (buttonState ==          LOW)    {// Turns the LED on          digitalWrite(led2,          High);     }else          {// Turns the LED offdigitalWrite(led2,          LOW);    } }        

!Your plough!

Effort to modify the lawmaking above so that the xanthous LED will be always on and, when the push button is pressed, brand the buzzer play a sound.

Answer

          //Pivot continued to push button-push button          int          buttonPin = A0;          int          led1 = A1;          int          led2 = A2;          //Variable to check the state          int          buttonState;          //Pin continued to buzzer          int          buzzer = 5;          void                      setup          () {// Defines the button pin equally inputpinMode(buttonPin,          INPUT);// Defines the Yellow LED pin as OutputpinMode(led1,          OUTPUT);// Defines the Green LED pin every bit OutputpinMode(led2,          OUTPUT);// Defines the Buzzer pin as Output          pinMode(buzzer,OUTPUT);// Turns the Yellow LED ondigitalWrite(led1,          HIGH); }          void                      loop          ()  {// Checks if the push button land has changed          buttonState =digitalRead(buttonPin);if          (buttonState ==          LOW)    {// Turns the green LED on          digitalWrite(led2,          HIGH);// Makes the buzzer play a audio equivalent to the note G, in Hztone(buzzer,392);       }else          {// Turns the dark-green LED offdigitalWrite(led2,          LOW);noTone(buzzer);     } }        

rileysituall.blogspot.com

Source: https://www.circuitar.com/projects/reading-a-button/index.html

Post a Comment for "Read Pin State on Arduino and Sending Output"