1 / 11

Digital & Analog Inputs

Digital & Analog Inputs. Review. Fundamental parts of an Arduino program are … Setting output types using pinMode . Declaring variables Can write a digital signal (0/1) to a pin using digitalWrite . Can read a digital signal (0/1) from a pin using digitalRead .

yuma
Download Presentation

Digital & Analog Inputs

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Digital & Analog Inputs

  2. Review • Fundamental parts of an Arduino program are … • Setting output types using pinMode. • Declaring variables • Can write a digital signal (0/1) to a pin using digitalWrite. • Can read a digital signal (0/1) from a pin using digitalRead. • Must have a variable to store the value

  3. IF, IF-ELSE, and IF-ELSE-IF if (someCondition) { // do stuff if the condition is true } if (someCondition) { // do stuff if the condition is true } else { // do stuff if the condition is false } if (someCondition) { // do stuff if the condition is true } else if (anotherCondition) { // do stuff only if the first condition is false // and the second condition is true }

  4. Your turn • Challenges: Complete in order • Push the button and have it display an LED pattern • Fireworks! • Push the button and have it turn on alternate LEDs • Button HIGH – Yellow • Button LOW – Red • Push the button twice to turn on LED • Need a COUNTER -- if you get to this stage, ask one of us for help 

  5. Analog Inputs

  6. Analog Signals • What is an analog signal and how does it differ from a digital signal? 0 1 1 0 1 1 1 0

  7. Analog Signals & Arduinos • The Arduino can read an analog signal on one of the analog input pins. • analogRead • The signal that can be read must be between 0 and 5 V. • The Arduino converts the signal to a number between 0 and 1023. • How many values are there for a digital signal? How many values are there for an analog signal on the Arduino?

  8. Program Time! • We’re going to write a program that will turn on the Arduino’s LED when “threshold” value is reached. How: • Use a Voltage Divider to change the voltage on the analog input pin. • Use the Arduino to detect when the voltage has reached a certain level. • Turn on the LED! Analog Input Potentiometer

  9. Analog Signal Detector • Step 1 – Declare Variables: intinputPin= 0; // select the input pin for the potentiometer // ( can be 0 through 5). intledPin = 13; // select the pin for the Arduino’sLED intinputValue= 0; // variable to store the value coming from // the voltage divider. • Step 2 – Setup: void setup() { pinMode(ledPin, OUTPUT); //Declare the LED pin as an //output. } • We do NOT have to declare the analog pin as an input. Why?

  10. Analog Signal Detector • Step 3 – The Loop: void loop() { inputValue = analogRead(inputPin); // Read the analog //input, what are // the possible values. if(inputValue > 511) // Check our threshold. { digitalWrite(ledPin, HIGH); // Turn on the LED. } else { digitalWrite(ledPin, LOW); // Turn off the LED. } }

  11. Your turn • Challenges: Complete in order • Design a program to make the LED flash at a speed dependent on the value on the analog input pin. • Turn on a number of LEDs that is proportional to the analog input value.

More Related