코드_주석
This is an old revision of the document!
예제 03A: Turn on LED when the button is pressed and keep it on after it is released Copy and paste this example into an empty Arduino sketch the pin for the LED the input pin where the pushbutton is connected val will be used to store the state of the input pin 0 = LED off while 1 = LED on tell Arduino LED is an output and BUTTON is an input read input value and store it check if the input is HIGH (button pressed) and change the state turn LED ON 예제 05: Turn on LED when the button is pressed including simple de-bouncing. If the button is held, brightness changes. input pin of the pushbutton stores the state of the input pin stores the previous value of "val" Stores the brightness value when did we begin pressing? yum, fresh check if there was a transition change the state from off to on or vice-versa millis() is the Arduino clock it returns how many milliseconds have passed since the board has been reset. (this line remembers when the button was last pressed) check whether the button is being held down If the button is held for more than 500ms. increment brightness by 1 delay to avoid brightness going up too fast 255 is the max brightness if we go over 255 let’s go back to 0 val is now old, let’s store it turn LED ON at the current brightness level turn LED OFF 예제 03C: Turn on LED when the button is pressed including simple de-bouncing Now with another new and improved formula!! this variable stores the previous value of "val" 0 = LED off and 1 = LED on val is now old, let's store it 예제 04: Fade an LED in and out like on a sleeping Apple computer We’ll use this to count up and down loop from 0 to 254 (fade in) set the LED brightness Wait 10ms because analogWrite is instantaneous and we would not see any change loop from 255 to 1 (fade out) Wait 10ms 예제 06A: Blink LED at a rate specified by the value of the analogue input variable used to store the value coming from the sensor LED is as an OUTPUT Note: Analogue pins are automatically set as inputs read the value from the sensor turn the LED on stop the program for some time turn the LED off 예제 07: Send to the computer the values read from analogue input 0 Make sure you click on "Serial Monitor" after you upload select the input pin for the sensor resistor variable to store the value coming from the sensor open the serial port to send data back to the computer at 9600 bits per second print the value to the serial port wait 100ms between each send 예제 08B: Arduino Networked Lamp variable to store the value coming from the sensor open the serial port read the value from the sensor read the incoming byte: If the marker's found, next 6 characters are the colour accumulate 6 chars store in the buffer move the pointer forward by 1 now we have the 3 numbers stored as hex numbers we need to decode them into 3 bytes r, g and b reset the pointer so we can reuse the buffer Check if there was a transition if the lamp is on turn the leds on at the colour sent by the computer otherwise turn off wait 100ms between each send converts one HEX character into a number 예제 02: Turn on LED while the button is pressed check whether the input is HIGH (button pressed) 예제 03B: Turn on LED when the button is pressed Now with a new and improved formula! 예제 06B: Set the brightness of LED to a brightness specified by the turn the LED on at the brightness set by the sensor 예제 01 : Blinking LED LED connected to digital pin 13 sets the digital pin as output turns the LED on waits for a second turns the LED off 예제 08A: Arduino networked lamp parts of the code are inspired by a blog post by Tod E. Kurt (todbot.com) Copy and paste this example into an empty Processing sketch retrieve feed every 60 seconds; the last time we fetched the content light level measured by the lamp Accumulates characters coming from Arduino we don't need fast updates IMPORTANT NOTE: The first serial port retrieved by Serial.list() should be your Arduino. If not, uncomment the next line by deleting the // before it, and re-run the sketch to see a list of serial ports. Then, change the 0 in between [ and ] to the number of the port that your Arduino is connected to. connect to Arduino Build a colour based on the 3 values Prepare a string to be sent to Arduino write the colour string to the screen this turns 1023 into 100 send data to Arduino check if there is data waiting read one byte if byte is not newline just add it to the buffer newline reached, let's process the data make sure there is enough data chop off the last character, it's a carriage return (a carriage return is the character at the end of a line of text) turn the buffer from string into an integer number clean the buffer for the next read cycle We're likely falling behind in taking readings from Arduino. So let's clear the backlog of incoming sensor readings so the next reading is up-to-date. we use these strings to parse the feed zero the counters An object to represent the URL prepare a connection now connect to the Website this is a bit of virtual plumbing as we connect the data coming from the connection to a buffered reader that reads the data one line at a time. read each line from the feed break it down each chunk of data is made lowercase found "love"? increment love by 1 found "peace"? increment peace by 1 found "arduino"? increment arduino by 1 Set 64 to be the maximum number of references we care about. multiply by 4 so that the max is 255, which comes in handy when building a colour that is made of 4 bytes (ARGB) If there was an error, stop the sketch
코드_주석.1264684655.txt.gz · Last modified: 2013/08/03 05:04 (external edit)