Button Turns on and off a light emitting diode(LED)
Transcript
Button Turns on and off a light emitting diode(LED)
/* 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: on most Arduinos there is already an LED on the board attached to pin 13 created 2005 by DojoDave <http://www.0j0.org> modified 30 Aug 2011 by Tom Igoe This example code is in the public domain. http://www.arduino.cc/en/Tutorial/Button */ // constants won't change. They're used here to // set pin numbers: const int buttonPin = 2; // the number of the pushbutton pin const int ledPin = 13; // the number of the LED pin // variables will change: int buttonState = 0; // variable for reading the pushbutton status void 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); // check 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); } } 1)Dichiarazioni riguardanti i componenti usati ed i pin digitali a cui sono collegati const int buttonPin = 2; const int ledPin = 13; // the number of the pushbutton pin // the number of the LED pin Scritture alternative che non modificano il funzionamento del programma int buttonPin = 2; int ledPin = 13; // the number of the pushbutton pin // the number of the LED pin # buttonPin 2 # ledPin 13 // the number of the pushbutton pin // the number of the LED pin I nomi usati per il pulsante e il led sono arbitrari e possono essere cambiati a piacimento int buttonState = 0; nome della cella di memoria in cui viene conservato lo stato del pulsante, il nome può essere cambiato a piacere lo stesso per il valore iniziale 2) assegnazione delle porte come entrate o uscite void setup() { pinMode(ledPin, OUTPUT); // initialize the LED pin as an output: pinMode(buttonPin, INPUT); // initialize the pushbutton pin as an input: } Sono inseriti i comandi pinMode(…….., OUTPUT); : pinMode(………., INPUT); Questi comandi non possono essere cambiati neppure sostituendo lettere maiuscole o minuscole o viceversa 3) parte operativa del programma void loop(){ // read the state of the pushbutton value: buttonState = digitalRead(buttonPin); // check 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); } } Comandi usati ……….=digitalRead(……….); Lettura del pin di ingresso ed assegnazione del suo valore ad una variabile(cella di memoria) digitalWrite(……, HIGH) digitalWrite(………., LOW) Comandi di scrittura su un pin di uscita del valore 1 o 0 HIGH LOW Funzione usata if….else If (condizione da verificare) { Azione che si compie se la condizione è verificata } else { Azione che si compie se la condizione non è verificata } In qualche caso mancala parte else ……………………… In questo caso il programma attende fino che la condizione non viene verificata
Documenti analoghi
output - Politecnico di Milano
const int buttonPin = 2; // the number of the pushbutton pin
const int ledPin = 13; // the number of the LED pin
int buttonState = 0; // variable for reading the pushbutton
status
void setup() {
pi...
Arduino
6) Applicare >3.3V sul pin 3.3V.
7) Cortocircuitare Vin a GND.
8) Applicare >13 V al reset.
9) Applicare tenzione a 5V e caricare Vin.
10) Eccedere la corrente totale del microcontrollore (200 mA)....
Telecontrollo escavatore LEGO con board Arduino - CARL-O.
Pin 8: Alimentazione per il motore
Pin 9: Attiva/disattiva il secondo motore in base se è HIGH o LOW
Pin 10: Insieme al pin 15 regola il verso di rotazione in base se è HIGH o LOW
Pin 11: Qui colle...