Sie sind auf Seite 1von 5

Level 3.

More Programming the Arduino: Reading Inputs Notes and Exercises


Project 1. Look for a blue component with a knob on the top and three wires coming out of it. This is a type of potentiometer. A potentiometer is a variable resistor though it sometimes also called a voltage divider. Think of the potentiometer as containing two resistors. As you turn the knob one resistor increases in resistance and the other decreases in resistance (but the total resistance remains the same). The middle terminal is connected between those two internal resistors. If there are 5 volts across the two outside terminals, then the middle pin will read between zero and five volts depending on where the knob is positioned. This project consists of two separate circuits where the value read by one circuit is used to control what the other circuit is doing. When you run it remember to open the Serial Monitor so you can see the values are being generated. const int SENSOR_PIN = 0; digital +5 volts const int LED_PIN = 8; pin 8 void setup(){ pinMode( LED_PIN, OUTPUT ); Serial.begin( 9600 ); } + (longer lead) pot LED analog pin 0

void loop(){ int v = analogRead( SENSOR_PIN ); 330 Serial.println( v ); digitalWrite( LED_PIN, HIGH ); ground delay( v ); digitalWrite( LED_PIN, LOW ); delay( v ); ground } The analog pin reads a voltage and converts it to an integer between 0 and 1023. This number is then used for the delay. If the delay gets too short (less than about 25), then the LED cannot turn off and on that fast and it will stay on all the time unless the value is zero then it is off all the time. Note: by default the analog pins detect a voltage between 0 and 5 volts but this can be changed. Project 2. Create a circuit that has four LEDs connected to four different digital pins. Create another circuit identical to the one above on the right with the potentiometer. As the knob is turned, more lights should go on. If you get a reading of 800 or above, 4 lights should be on. Between 600 and 800, 3 lights should be on. Between 400 and 600, 2 lights are on. Between 200 and 400, 1 light is on. Less than 200, no lights are on. To check if a number is between values, you can use something like this: if ( num >= ________ && num < _________) the && is the logical AND operator Note: there is no need to call the delay function for this project. Lights are either on or off. Spoiler Alert! On the next page is another way to write the code this project.
Arduino Level 3 Notes Page 1

Code Solution for Project 2. I used digital pins 8, 9, 10, and 11 and set their modes to OUTPUT. In the loop function I used a different approach that eliminated the need for the && operator. const int SENSOR_PIN = 0; const int LED_PIN8 = 8; const int LED_PIN9 = 9; const int LED_PIN10 = 10; const int LED_PIN11 = 11; void setup(){ pinMode( LED_PIN8, OUTPUT ); pinMode( LED_PIN9, OUTPUT ); pinMode( LED_PIN10, OUTPUT ); pinMode( LED_PIN11, OUTPUT ); } void loop(){ int v = analogRead( SENSOR_PIN ); if ( v >= 800 ){ digitalWrite( LED_PIN8, HIGH ); digitalWrite( LED_PIN9, HIGH ); digitalWrite( LED_PIN10, HIGH ); digitalWrite( LED_PIN11, HIGH ); } else if ( v >= 600 ){ digitalWrite( LED_PIN8, HIGH ); digitalWrite( LED_PIN9, HIGH ); digitalWrite( LED_PIN10, HIGH ); digitalWrite( LED_PIN11, LOW ); } else if ( v >= 400 ){ digitalWrite( LED_PIN8, HIGH ); digitalWrite( LED_PIN9, HIGH ); digitalWrite( LED_PIN10, LOW ); digitalWrite( LED_PIN11, LOW ); } else if ( v >= 200 ){ digitalWrite( LED_PIN8, HIGH ); digitalWrite( LED_PIN9, LOW ); digitalWrite( LED_PIN10, LOW ); digitalWrite( LED_PIN11, LOW ); } else { digitalWrite( LED_PIN8, LOW ); digitalWrite( LED_PIN9, LOW ); digitalWrite( LED_PIN10, LOW ); digitalWrite( LED_PIN11, LOW ); } }

Rules for using if / else if structures. 1. You must start with an if statement. 2. You may have as many else if statements as you need. Each else if statement must contain an expression that is either true or false. 3. The else statement is there if you need it. If you dont need it, dont include it. 4. When the code is executed, only the section associated with the first true condition is executed. For example, if v is 900, then v >= 800 is true and we turn on the four lights and skip the else if and else sections. Only if v>= 800 if false do we check the next condition, v >= 600, if true. If it is, then we turn 3 lights on and one light off and skip the remaining statements.

Arduino Level 3 Notes

Page 2

Project 3. Look for a long flat component three terminals. It says spectra symbol on it thats the name of the company that makes it. This is another type of potentiometer called a soft potentiometer (softpot). The resistance varies 100 ohms to 10,000 ohms depending on where you press on the string. Redo project 2 but use the softpot instead of the blue square potentiometer.

Project 4. Look for a component with two long leads and small head with what looks a little like a red snake on the top. This is a photo resistor its resistance changes depending on the amount of light detected. Here are some rough values based on some simple tests at home: Lights out but I could still read the multimeter Lights on in the room but nothing really bright Under a bright light So, the brighter the light, the smaller the resistance. Make the following circuit. The analog pins do not detect resistance; they detect the voltage between that point in circuit and ground. If the photo resistor has a resistance of 10 k, then the voltage drop across both resistors should be the same about 2.5 volts. If the amount of light decreases, then the resistance of the photo resistor becomes greater than 10 k and the voltage detected by pin 5 decreases. If the amount of light increases, then the resistance of the photo resistor becomes less than 10 k and the voltage detected by pin 5 increases. Remember pin 5 detects the voltage between that point in the circuit and ground. In other words it is detecting the voltage drop across the 10 k resistor. Heres the code for project. const int SENSOR_PIN = 5; void setup(){ Serial.begin( 9600 ); } void loop(){ int v = analogRead( SENSOR_PIN ); Serial.println( v ); } Remember to open the Serial Monitor window. And this is the last time Ill nag you about this. At least in print. photo-resistor analog pin 5 10 K resistance is about 200 k resistance is about 5.6 k resistance is about 500

Arduino Level 3 Notes

Page 3

When I ran this experiment at home, I usually saw numbers in the 600s. If I covered the sensor with a light paper towel, it dropped into the 200s. If I covered the photo resistor with a piece of dark cardboard, it dropped to between 10 and 20. If I shone a flash light at it, the numbers got into the 900s. Remember, analogRead returns a number between 0 and 1023. Improve this project by adding an LED. If the amount of light detected is low, the LED goes on. If there is enough light then the LED turns off. You decide what the level of light turns the LED on and off.

Project 5. Push buttons! Our mini push buttons have four terminals. To understand how they are connected, create the following circuit. There are two yellow LEDs, one red LED, and a push button.

yellow + gnd + +

yellow

5v 330 red

After you finished the circuit, just connect the Arduino to the computer. There is no programming involved. When the button is not pressed, the red LED should be on and the two yellow LEDs should be off. While the button is pressed, all three LEDs should be on. If you disconnect the power, rotate the button 180 degrees, the circuit should behave the same way. In other words, there is always a connection between points A and B. There is also always a connection between points C and D. When the button is pressed, all four points are connected. A C B D

Arduino Level 3 Notes

Page 4

Project 6. In this project we will learn how to write a program that can detect if the button is pressed down or not. This project consists of two separate circuits. One is our standard LED circuit and the other has a push button. const int PIN_2 = 2; digital 5v const int PIN_11 = 11; pin 8 digital void setup(){ pin 2 Serial.begin( 9600 ); + (longer lead) Serial.print( "HIGH equals " ); LED Serial.println( HIGH ); 10 k pinMode( PIN_2, INPUT ); pinMode( PIN_11, OUTPUT ); } 330 When the button is not void loop(){ pressed, pin 2 may int num = digitalRead( PIN_2 ); detect random noise. Serial.println( num ); // always 0 or 1 The 10k resistor if ( num == HIGH ) ensures that pin 2 digitalWrite( PIN_11, HIGH ); returns zero when the ground else button is not pushed. digitalWrite( PIN_11, LOW ); This is called a pull } down resistor. num should have a value of 1 when the button is pressed. This would indicate that pin 2 detects 5 volts. When the button is not pressed, num should be zero because it should detect zero volts.

Project 7. Design a circuit with two push buttons, three LEDs, three 330 ohm resistors, and two 10 kiloohm resistors. It should work as follows. When both buttons are pressed, all three LEDs should be on. When only button A is pressed, two LEDs should be on. When only button B is pressed, one LED should be on. When no buttons are pressed, no LEDs should be on.

Summary. Arduino pins can detect voltages. The analog pins (A0 to A5) can detect a range of voltage levels through the following function call. int v = analogRead( analog pin ); v, or whatever you want to name the variable, will have a value between 0 and 1023. The digital pins are design to simply detect if the voltage is high (5 volts) or low (0 volts). You will often need to use a pull-down resistor in these circuits. (You could also use a pull-up resistor but I didnt show you that.) To use a digital pin to read input you must first set the pin mode to INPUT in the setup function. For example: pinMode( PIN_2, INPUT ); Then in the loop function you use this function: int num = digitalRead( PIN_2 ); num will have a value of either HIGH (1) or LOW (0).

Arduino Level 3 Notes

Page 5

Das könnte Ihnen auch gefallen