Documentos de Académico
Documentos de Profesional
Documentos de Cultura
pinMode(pin,[INPUT,OUTPUT])
digitalWrite(pin,valor)
Se usa para activar o desactivar un pin digital. Entre paréntesis
se debe indicar qué pin modificar, y qué valor darle.
digitalRead(pin);
Para poder usar el valor del estado para algún fin debes
guardarlo en una variable:
1/7
05_ENTRADAS /SALIDAS DIGITALES
miVariable = digitalRead(pin);
Si quieres realizar una comparación puedes escribir el comando
directamente en la sentencia:
if (digitalRead(pin)==LOW){
sentencia1;
sentencia2;
…
}
Programa02
2/7
05_ENTRADAS /SALIDAS DIGITALES
//prog02_secuencia_v1
//Declaracion de variables
int retardo; //tiempo de encendido del led en milisegundos
void setup()
{
//configuracion de los pines digitales como salida
pinMode(ledVerde, OUTPUT);
pinMode(ledAmarillo, OUTPUT);
pinMode(ledRojo, OUTPUT);
void loop()
{
//se enciende el led verde durante el tiempo asignado en retardo
digitalWrite(ledVerde, HIGH);
delay(retardo);
//se apaga el led verde y se enciende el led amarillo
//durante el tiempo asignado en retardo
digitalWrite(ledVerde, LOW);
digitalWrite(ledAmarillo, HIGH);
delay(retardo);
//se apaga el led amarillo y se enciende el led rojo
//durante el tiempo asignado en retardo
digitalWrite(ledAmarillo, LOW);
digitalWrite(ledRojo, HIGH);
delay(retardo);
//se apaga el led rojo
digitalWrite(ledRojo, LOW);
}
3/7
05_ENTRADAS /SALIDAS DIGITALES
Programa 03
4/7
05_ENTRADAS /SALIDAS DIGITALES
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);
5/7
05_ENTRADAS /SALIDAS DIGITALES
Programa 04
/*
State change detection (edge detection)
*/
// this constant won't change:
const int buttonPin = 2; // the pin that the pushbutton is attached to
const int ledPin = 3; // the pin that the LED is attached to
void setup() {
// initialize the button pin as a input:
pinMode(buttonPin, INPUT);
// initialize the LED as an output:
pinMode(ledPin, OUTPUT);
// initialize serial communication:
Serial.begin(9600);
}
void loop() {
// read the pushbutton input pin:
buttonState = digitalRead(buttonPin);
// compare the buttonState to its previous state
if (buttonState != lastButtonState) {
// if the state has changed, increment the counter
if (buttonState == HIGH) {
// if the current state is HIGH then the button
// wend from off to on:
buttonPushCounter++;
Serial.println("on");
Serial.print("number of button pushes: ");
Serial.println(buttonPushCounter);
}
else {
// if the current state is LOW then the button
// wend from on to off:
Serial.println("off");
}
}
// save the current state as the last state, for next time through the loop
lastButtonState = buttonState;
6/7
05_ENTRADAS /SALIDAS DIGITALES
// turns on the LED every four button pushes by checking the modulo of the
//button push counter.
// the modulo function gives you the remainder of the division of two numbers:
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
7/7