0% encontró este documento útil (0 votos)
23 vistas4 páginas

Arduino: Control de LED y Servo

Prelab 5 de electronica básica, codigo de arduino

Cargado por

Adrian Salvador
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd
0% encontró este documento útil (0 votos)
23 vistas4 páginas

Arduino: Control de LED y Servo

Prelab 5 de electronica básica, codigo de arduino

Cargado por

Adrian Salvador
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como DOCX, PDF, TXT o lee en línea desde Scribd

UNIVERSIDAD SAN FRANCISCO DE QUITO

Electrónica Básica
Adrián Salvador
00320811
PreLab 5
1. Escribir un código para Arduino capaz de encender y apagar un diodo
LED conectado a un puerto digital del microcontrolador, el proceso de
intermitencia debe ejecutarse en intervalos de 4 segundos para encendido
y 1 segundo para apagado (No utilizar la función delay de la librería del
arduino, en su lugar utilizar su reloj interno).

const int LED_PIN = 13; // Replace 13 with the number of


the digital port where the LED is connected
const int ON_TIME = 4000; // Time in milliseconds that the
LED should stay on
const int OFF_TIME = 1000; // Time in milliseconds that the
LED should stay off
unsigned long previousMillis = 0; // Stores the last time the
LED was updated
int ledState = LOW; // Stores the current state of the LED
void setup() {
pinMode(LED_PIN, OUTPUT);
}
void loop() {
unsigned long currentMillis = millis(); // Get the current
time
if (currentMillis - previousMillis >= (ledState == HIGH ?
ON_TIME : OFF_TIME)) {
// If enough time has passed, toggle the LED
previousMillis = currentMillis;
ledState = (ledState == HIGH) ? LOW : HIGH;
digitalWrite(LED_PIN, ledState);
}
}
2. Escribir un código para Arduino capaz de encender y apagar un diodo
LED conectado a un puerto digital del microcontrolador, el proceso de
encendido y apagado se debe realizar al presionar un botón que esta
conectado a otro puerto digital ( se prende y se apaga el diodo LED con el
mismo botón, este funciona como pulsador no como switch).

const int LED_PIN = 13; // Replace 13 with the number of


the digital port where the LED is connected
const int BUTTON_PIN = 7; // Replace 7 with the number
of the digital port where the button is connected
int buttonState = LOW; // Stores the current state of the
button
int lastButtonState = LOW; // Stores the previous state of
the button
int ledState = LOW; // Stores the current state of the LED
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT);
}
void loop() {
buttonState = digitalRead(BUTTON_PIN); // Read the
current state of the button
if (buttonState != lastButtonState) {
// If the button state has changed, toggle the LED
if (buttonState == HIGH) {
ledState = (ledState == HIGH) ? LOW : HIGH;
digitalWrite(LED_PIN, ledState);
}
lastButtonState = buttonState;
}
}
3. Escribir un código para Arduino capaz de leer un puerto analógico y sus
datos sean enviados al puerto serial, el puerto analógico debe leerse cada
segundo y sus datos deben entregarse formando columnas con el siguiente
formato: n ́umero de lectura, Lectura análoga (valorMedidoSinConversion
0-1023), voltaje (ValorMedidoTrasformadoAvoltios 0-5V).

const int ANALOG_PIN = A0; // Replace A0 with the number


of the analog port where the sensor is connected
const float VOLTAGE_DIVIDER_RATIO = 5.0 / 1023.0; // Ratio
to convert raw value to voltage
int readingNumber = 0; // Stores the current reading
number
void setup() {
[Link](9600); // Begin serial communication at 9600
bits per second
}
void loop() {
int rawValue = analogRead(ANALOG_PIN); // Read the raw
value from the analog port
float voltage = rawValue * VOLTAGE_DIVIDER_RATIO; //
Convert the raw value to voltage
[Link](readingNumber); // Print the reading number
[Link]("\t"); // Print a tab character to separate
columns
[Link](rawValue); // Print the raw value
[Link]("\t"); // Print a tab character to separate
columns
[Link](voltage, 3); // Print the voltage with 3
decimal places and a newline character
readingNumber++; // Increment the reading number
delay(1000); // Wait for 1 second before taking the next
reading
}
4. Escribir un código para Arduino capaz de entregar una señal digital
modulada en ancho (PWM), la modulación debe ser ajustada por un
potenciómetro conectado a un puerto análogo del arduino y la salida debe
controlar un servomotor.

#include <Servo.h>
Servo servo;
[Link](SERVO_PIN);
int potValue = analogRead(POT_PIN);
int servoValue = map(potValue, 0, 1023, 0, 180);
[Link](servoValue);
delay(15);
#include <Servo.h>
const int SERVO_PIN = 9;
const int POT_PIN = A0;
Servo servo;
void setup() {
[Link](SERVO_PIN);
}
void loop() {
int potValue = analogRead(POT_PIN);
int servoValue = map(potValue, 0, 1023, 0, 180);
[Link](servoValue);
delay(15);
}

También podría gustarte