Changes

Jump to: navigation, search

Classes/2010/VIS147A/Lab7

638 bytes added, 16:11, 23 February 2010
Part 3 - Programming
=== Part 3 - Programming ===
==== Digital Out code ====
* In the simple blink example, this code
<code>
* The <code>digitalWrite(ledPin, HIGH)</code> turns on the LED
* <code>digitalWrite(ledPin, LOW)</code> turns off the LED.
* What is this actually doing? The <code>HIGH</code> is setting that output pin to High, 1, True, or +5V. You can measure this with your multimeter while the example is running. * similaritySimilarly, the <code>LOW</code> is setting that output pin to Low, 0, False, 0V LOW or GND(0V). Again, this is measurable with the multimeter.
* This is where your programmable, internal world of the microcontroller is interfacing with the hardware, LED world of intro to electronics.
* Other code: We needed to do some setup first <code>pinMode(ledPin, OUTPUT)</code>* and then it so the microcontroller knows whether to use those pins as inputs or outputs. After that, output is simply a matter of <code>digitalWrite</code> calls.==== Digital In code ====* Similarly, in In the digital_in Button example above, we do some setup <code>
pinMode(ledPin, OUTPUT); // declare LED as output
pinMode(inputPin, INPUT); // declare pushbutton as input
</code>
* And then we read inputs and make outputs with <code>digitalRead</code> and <code>digitalWrite</code> calls.
* If you ever seen anything unfamiliar in a program, and you want to learn more about what it does, for example digital read<code>digitalRead()</code>, hilight highlight the term, and right-click->Find in Reference (or select Help->Find in referencein the menu).** This will pull up the arduino reference Arduino help files. There is a lot to be found here. Very useful. * You In this example, you could insert your own code, for instance:
<code>
digitalWrite(ledPin, HIGH); // sets the LED on
delay(1000); // waits for one second
</code>
* to get a "long-short-long-short" blinking. ** Try this out. * These kinds of patterned blinkings suggest the idea of coded communications. For instance Morse code is a very simple way to encode information as on/off pulses (LONG and SHORT),and transmit them over a distance. What are some other examples of codes of that sort? * As you add desire more complicated behavior, things are your program is going to get ridiculous very soon, you will have tons with endless lists of special cases, lines <code>digitalWrite()</code> and lines of <code>if</code> statements. There are more efficient ways to do this...* What are more efficient ways to do thisfor instance using <code>for</code> loops, <code>switch</code> statements, use For loops or and arrays.
=== Loops ===

Navigation menu