[亮一顆LED]
影片:
程式碼:
/* Analog Input Demonstrates analog input by reading an analog sensor on analog pin 0 and turning on and off a light emitting diode(LED) connected to digital pin 13. The amount of time the LED will be on and off depends on the value obtained by analogRead(). The circuit: * Potentiometer attached to analog input 0 * center pin of the potentiometer to the analog pin * one side pin (either one) to ground * the other side pin to +5V * LED anode (long leg) attached to digital output 13 * LED cathode (short leg) attached to ground * Note: because most Arduinos have a built-in LED attached to pin 13 on the board, the LED is optional. Created by David Cuartielles modified 30 Aug 2011 By Tom Igoe This example code is in the public domain. http://arduino.cc/en/Tutorial/AnalogInput */ int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int a=0; void setup() { // declare the ledPin as an OUTPUT: pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(8, OUTPUT); pinMode(7, OUTPUT); Serial.begin(9600); } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); Serial.println(sensorValue); // turn the ledPin on digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW); digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, LOW); //digitalWrite(ledPin, HIGH); // stop the program for <sensorValue> milliseconds: a=5; for(int i=0;i<13;i++) { if(i<7){ delay(sensorValue); // turn the ledPin off: digitalWrite(i+7, HIGH); digitalWrite(i+6, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); } if(i>=7){ delay(sensorValue); // turn the ledPin off: digitalWrite(i+a, HIGH); digitalWrite(i+a+1, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); a=a-2; } } }
[亮兩顆LED]影片:[亮三顆LED]影片:程式碼:int sensorPin = A0; // select the input pin for the potentiometer int ledPin = 13; // select the pin for the LED int sensorValue = 0; // variable to store the value coming from the sensor int a=0; void setup() { // declare the ledPin as an OUTPUT: pinMode(13, OUTPUT); pinMode(12, OUTPUT); pinMode(11, OUTPUT); pinMode(10, OUTPUT); pinMode(9, OUTPUT); pinMode(8, OUTPUT); pinMode(7, OUTPUT); Serial.begin(9600); } void loop() { // read the value from the sensor: sensorValue = analogRead(sensorPin); Serial.println(sensorValue); // turn the ledPin on digitalWrite(13, LOW); digitalWrite(12, LOW); digitalWrite(11, LOW); digitalWrite(10, LOW); digitalWrite(9, LOW); digitalWrite(8, LOW); digitalWrite(7, LOW); //digitalWrite(ledPin, HIGH); // stop the program for <sensorValue> milliseconds: a=5; for(int i=0;i<13;i++) { if(i<7){ delay(sensorValue); // turn the ledPin off: digitalWrite(i+7, HIGH); digitalWrite(i+8, HIGH); digitalWrite(i+9, HIGH); digitalWrite(i+6, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); } if(i>=7){ delay(sensorValue); // turn the ledPin off: digitalWrite(i+a, HIGH); digitalWrite(i+a-1, HIGH); digitalWrite(i+a-2, HIGH); digitalWrite(i+a+1, LOW); // stop the program for for <sensorValue> milliseconds: delay(sensorValue); a=a-2; } } }
沒有留言:
張貼留言