0% found this document useful (0 votes)
989 views

Algoritmos para Arduino

The document contains 9 examples of Arduino code using for loops and functions to control LEDs by turning them on and off at different time intervals, fading the brightness of an LED, reading the value of a potentiometer, and controlling an LED with a button. The examples demonstrate basic Arduino programming techniques including using for loops, functions, if/else statements, analogRead, digitalRead/Write, and delay.
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
989 views

Algoritmos para Arduino

The document contains 9 examples of Arduino code using for loops and functions to control LEDs by turning them on and off at different time intervals, fading the brightness of an LED, reading the value of a potentiometer, and controlling an LED with a button. The examples demonstrate basic Arduino programming techniques including using for loops, functions, if/else statements, analogRead, digitalRead/Write, and delay.
Copyright
© © All Rights Reserved
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 9

Este es el 1 ejemplo con for:

/*
Blink
Turns on an LED on for one second, then off for one second, repeatedly.
This example code is in the public domain.
*/
// Pin 13 has an LED connected on most Arduino boards.
/* give it a name:
int led12 = 12;
int led11 = 11;
int led10 = 10;
int led9 = 9;
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
for (int i=9; i < 13; i++) {
pinMode(i, OUTPUT);
}
}
// the loop routine runs over and over again forever:
void loop() {
for(int i=9; i < 13; i++) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50);
// wait for a second
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
}
}

este es el 2do ejemplo para for:


// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
for (int i=9; i < 13; i++) {
pinMode(i, OUTPUT);
}
}
// the loop routine runs over and over again forever:
void loop() {
for(int i=9; i < 13; i++) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50);
// wait for a second
}
for(int i=9; i < 13; i++) {
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
}
}

este es el 3er ejemplo de arduino


// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
for (int i=9; i < 13; i++) {
pinMode(i, OUTPUT);
}
}
// the loop routine runs over and over again forever:
void loop() {
a();
}
void a() {
for(int i=9; i < 13; i++) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
}
delay(50);
// wait for a second
for(int i=9; i < 13; i++) {
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
}
delay(50); // wait for a second
}

4to ejemplo del programa arduino


// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
for (int i=9; i < 13; i++) {
pinMode(i, OUTPUT);
}
}
// the loop routine runs over and over again forever:
void loop() {
a();
b();
}
void a() {
for(int i=9; i < 13; i++) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
}
delay(50);
// wait for a second
for(int i=9; i < 13; i++) {
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
}
delay(50); // wait for a second
}
void b() {
for(int i=9; i < 13; i++) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000);
// wait for a second
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
delay(1000); // wait for a second
}
}

5to ejemplo para arduino


// the setup routine runs once when you press reset:
void setup() {
// initialize the digital pin as an output.
for (int i=9; i < 13; i++) {
pinMode(i, OUTPUT);
}
}
// the loop routine runs over and over again forever:
void loop() {
a();
b();
c();
}
void a() {
for(int i=9; i < 13; i++) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
}
delay(50);
// wait for a second
for(int i=9; i < 13; i++) {
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
}
delay(50); // wait for a second
}
void b() {
for(int i=9; i < 13; i++) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50);
// wait for a second
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
}
}
void c() {
for(int i=12; i > 8; i--) {
digitalWrite(i, HIGH); // turn the LED on (HIGH is the voltage level)
delay(50);
// wait for a second
digitalWrite(i, LOW); // turn the LED off by making the voltage LOW
delay(50); // wait for a second
}
}

ejemplo 6 de arduino
int led = 9;
int brillo= 0;
int incremento = 5;
const int minimo=0;
const int maximo=255;
int tiempo=50;
void setup() {
pinMode(led, OUTPUT);
}
void loop() {
analogWrite(led,brillo);
brillo=brillo + incremento;
if (brillo == minimo || brillo == maximo) {
incremento = -incremento;
}
delay(50);
}

ejemplo numero 7
para medir el ponteciomentro
void setup() {
Serial.begin(9600);
}
void loop() {
int sensorValue = analogRead (A0);
Serial.println(sensorValue);
delay(100);
}
para entrar al monitor serial
control m o en herrramienta

ejemplo 8 la luz se apaga con el boton


int boton = 2;
int tiempo=1;
int veloc_serial=9600;
const int led=9;
void setup () {
Serial.begin(veloc_serial);
pinMode(boton, INPUT);
pinMode(led,OUTPUT);
}
void loop() {
int estado = digitalRead(boton);
Serial.println(estado);
digitalWrite(led,estado);
delay(tiempo);
}

ejemplo 9 para programar prendiendo la luz con el boton


int boton = 2;
int tiempo=1;
int veloc_serial=9600;
const int led=9;
void setup () {
Serial.begin(veloc_serial);
pinMode(boton, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
if (digitalRead(boton)== HIGH) {
digitalWrite(led,LOW);
} else {
digitalWrite(led,HIGH);
};
delay(tiempo);
}

You might also like