1、玩转ARDUINO有图版玩转ARDUINO项目一点亮你的LED材料清单 ARDUINO LED 330电阻代码/* Blink Turns on an LED on for one second, then off for one second, repeatedly. */ / Pin 13 has an LED connected on most Arduino boards./ give it a name:int led = 13;/ the setup routine runs once when you press reset:void setup() / initialize t
2、he digital pin as an output. pinMode(led, OUTPUT); / the loop routine runs over and over again forever:void loop() digitalWrite(led, HIGH); / turn the LED on (HIGH is the voltage level) delay(1000); / wait for a second digitalWrite(led, LOW); / turn the LED off by making the voltage LOW delay(1000);
3、 / wait for a second线路图项目二读取数字信号材料清单 ARDUINO 按键 10K电阻 面包板 面包线(杜邦线)代码/* DigitalReadSerial Reads a digital input on pin 2, prints the result to the serial monitor */ digital pin 2 has a pushbutton attached to it. Give it a name:int pushButton = 2;/ the setup routine runs once when you press reset:void
4、 setup() / initialize serial communication at 9600 bits per second: Serial.begin(9600); / make the pushbuttons pin an input: pinMode(pushButton, INPUT);/ the loop routine runs over and over again forever:void loop() / read the input pin: int buttonState = digitalRead(pushButton); / print out the sta
5、te of the button: Serial.println(buttonState); delay(1); / delay in between reads for stability线路图项目三读取模拟信号材料清单 ARDUINO 10k电位计 杜邦线代码/* AnalogReadSerial Reads an analog input on pin 0, prints the result to the serial monitor. Attach the center pin of a potentiometer(电位计) to pin A0, and the outside pi
6、ns to +5V and ground. */ the setup routine runs once when you press reset:void setup() / initialize serial communication at 9600 bits per second: Serial.begin(9600);/ the loop routine runs over and over again forever:void loop() / read the input on analog pin 0: int sensorValue = analogRead(A0); / p
7、rint out the value you read: Serial.println(sensorValue); delay(1); / delay in between reads for stability线路图项目四呼吸灯材料清单 ARDUINO LED 220电阻 面包板代码/* Fade This example shows how to fade an LED on pin 9 using the analogWrite() function. */int led = 9; / the pin that the LED is attached toint brightness =
8、 0; / how bright the LED isint fadeAmount = 5; / how many points to fade the LED by/ the setup routine runs once when you press reset:void setup() / declare pin 9 to be an output: pinMode(led, OUTPUT); / the loop routine runs over and over again forever:void loop() / set the brightness of pin 9: ana
9、logWrite(led, brightness); / change the brightness for next time through the loop:/ 控制占空比DUTY CYCLE/DUTY RATIO brightness = brightness + fadeAmount; / reverse the direction of the fading at the ends of the fade: if (brightness = 0 | brightness = 255) fadeAmount = -fadeAmount ; / wait for 30 millisec
10、onds to see the dimming effect delay(30); 补充pulse width modulation(PWM)脉冲调宽线路图项目五读取模拟信号口的电压材料清单 ARDUINO 10K电位计代码/* ReadAnalogVoltage Reads an analog input on pin 0, converts it to voltage, and prints the result to the serial monitor. Attach the center pin of a potentiometer to pin A0, and the outsid
11、e pins to +5V and ground. */ the setup routine runs once when you press reset:void setup() / initialize serial communication at 9600 bits per second: Serial.begin(9600);/ the loop routine runs over and over again forever:void loop() / read the input on analog pin 0: int sensorValue = analogRead(A0);
12、 / Convert the analog reading (which goes from 0 - 1023) to a voltage (0 - 5V): float voltage = sensorValue * (5.0 / 1023.0); / print out the value you read: Serial.println(voltage);线路图项目六Millis()材料清单 ARDUINO LED 220电阻 面包线代码/* Blink without Delay Turns on and off a light emitting diode(LED) connecte
13、d to a digital pin, without using the delay() function. This means that other code can run at the same time without being interrupted by the LED code. The circuit: * LED attached from pin 13 to ground. * Note: on most Arduinos, there is already an LED on the board thats attached to pin 13, so no har
14、dware is needed for this example.*/ constants wont change. Used here to / set pin numbers:const int ledPin = 13; / the number of the LED pin/ Variables will change:int ledState = LOW; / ledState used to set the LEDlong previousMillis = 0; / will store last time LED was updated/ the follow variables
15、is a long because the time, measured in miliseconds,/ will quickly become a bigger number than can be stored in an int.long interval = 1000; / interval(间隔) at which to blink (milliseconds)void setup() / set the digital pin as output: pinMode(ledPin, OUTPUT); void loop() / here is where youd put code
16、 that needs to be running all the time. / check to see if its time to blink the LED; that is, if the / difference between the current time and last time you blinked / the LED is bigger than the interval at which you want to / blink the LED. unsigned long currentMillis = millis(); if(currentMillis -
17、previousMillis interval) / save the last time you blinked the LED previousMillis = currentMillis; / if the LED is off turn it on and vice-versa: if (ledState = LOW) ledState = HIGH; else ledState = LOW; / set the LED with the ledState of the variable: digitalWrite(ledPin, ledState); 线路图项目七用按键点亮LED材料
18、清单 ARDUINO 按键 10k电阻 面包板 面包线代码/* Button Turns on and off a light emitting diode(LED) connected to digital pin 13, when pressing a pushbutton attached to pin 2. The circuit: * LED attached from pin 13 to ground * pushbutton attached to pin 2 from +5V * 10K resistor attached to pin 2 from ground * Note
19、: on most Arduinos there is already an LED on the board attached to pin 13. */ constants常量,这里指的是const语句 wont change. Theyre used/here to / set pin numbers:const int buttonPin = 2; / the number of the pushbutton pinconst int ledPin = 13; / the number of the LED pin/ variables will change:int buttonSt
20、ate = 0; / variable for reading the pushbutton statusvoid setup() / initialize the LED pin as an output: pinMode(ledPin, OUTPUT); / initialize the pushbutton pin as an input: pinMode(buttonPin, INPUT); void loop() / read the state of the pushbutton value: buttonState = digitalRead(buttonPin); / chec
21、k if the pushbutton is pressed. / if it is, the buttonState is HIGH: if (buttonState = HIGH) / turn LED on: digitalWrite(ledPin, HIGH); else / turn LED off: digitalWrite(ledPin, LOW); 线路图项目八电灯开关材料清单 ARDUINO 按键 10K电阻代码/* Debounce 中文:防反跳 Each time the input pin goes from LOW to HIGH (e.g. because of a
22、 push-button press), the output pin is toggled 切换 from LOW to HIGH or HIGH to LOW. Theres a minimum delay between toggles to debounce the circuit (i.e. to ignore noise). The circuit: * LED attached from pin 13 to ground * pushbutton attached from pin 2 to +5V * 10K resistor attached from pin 2 to gr
23、ound * Note: On most Arduino boards, there is already an LED on the board connected to pin 13, so you dont need any extra components for this example. */ constants wont change. Theyre used here to / set pin numbers:const int buttonPin = 2; / the number of the pushbutton pinconst int ledPin = 13; / t
24、he number of the LED pin/ Variables will change:int ledState = HIGH; / the current state of the output pinint buttonState; / the current reading from the input pinint lastButtonState = LOW; / the previous reading from the input pin/ the following variables are longs because the time, measured in mil
25、iseconds,/ will quickly become a bigger number than can be stored in an int.long lastDebounceTime = 0; / the last time the output pin was toggledlong debounceDelay = 50; / the debounce time; increase if the output flickersvoid setup() pinMode(buttonPin, INPUT); pinMode(ledPin, OUTPUT); / set initial
26、 LED state digitalWrite(ledPin, ledState);void loop() / read the state of the switch into a local variable: int reading = digitalRead(buttonPin); / check to see if you just pressed the button / (i.e. the input went from LOW to HIGH), and youve waited / long enough since the last press to ignore any
27、noise: / If the switch changed, due to noise or pressing: if (reading != lastButtonState) / reset the debouncing timer lastDebounceTime = millis(); if (millis() - lastDebounceTime) debounceDelay) / whatever the reading is at, its been there for longer / than the debounce delay, so take it as the act
28、ual current state: / if the button state has changed: if (reading != buttonState) buttonState = reading; / only toggle the LED if the new button state is HIGH if (buttonState = HIGH) ledState = !ledState; / set the LED: digitalWrite(ledPin, ledState); / save the reading. Next time through the loop,
29、/ itll be the lastButtonState: lastButtonState = reading;线路图项目九按三下按键点亮LED材料清单 ARDUINO 按键 10K电阻代码/* State change detection (edge detection边缘检测) Often, you dont need to know the state of a digital input all the time,but you just need to know when the input changes from one state to another. For example, you want to know when a button goes from OFF to ON. This is called state change detection, or edge detection. This example shows how to detect when a button or button changes from off to on and on to off. The circuit: * pushbutton attached to pin 2 from +5V * 10K
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1