Proyectos de Arduino (Itfip)
Proyectos de Arduino (Itfip)
I. INTRODUCCIÓN
E N esta actividad pondremos en prectica el
conociminiento adquirido en clase acerca de
arduino una plataforma libre que se utiliza para la
realizacion de proyectos, encontramos dos talleres los
cuales contienen ejercicios basicos que montaremos
demanea fisica y el codigo del controlador sera creado
en el mismo sotfware arduino, luego procedemos a
pasarlo para asi unirlo con el montaje fisico y ver su
correcto funcionemiento.
El objetivo de esta experiencia es comprender las
basesde arduino y como de diferentes maneras de
programar podremos lograr las mismas cosas todo
esto realizado por nosotros msmos para
enriquesernos de experiencia, estos ejercicios son Imagen1: Arduino mega 2560.
probados en el laboratorio .
Arduino es una plataforma de hardware libre, basada III. LABORATORIO
en una placa con un microcontrolador y un entorno de
desarrollo, diseñada para facilitar el uso de la Para esta práctica se utilizó protoboard, resistencias
electrónica en proyectos multidisciplinares. foto-resistencias, leds, parlantes, pulsadores, placa
Arduino, instalación del software Arduino y guías
didácticas.
Taller No.4
1. ARRAYS
Utilizamos el FOR como función principal la cual hace Imagen 3: Conexión LED Arduino.
que las luces conectadas sigan una secuencia
programada esto para aprender el uso y la aplicabilidad Utilizamos la función FOR para realizar la secuencia de
de esta función.
luces del auto fantástico en la cual los leds prende y
apangan en secuencia y luego se regresan
Codigo arduino:
int timer = 100;
int ledPins[] = { 2, 7, 4, 6, 5, 3 };
int pinCount = 6; Codigo arduino:
int timer = 100;
void setup() {
for (int thisPin = 0; thisPin < pinCount; thisPin++) void setup() {
{ for (int thisPin = 2; thisPin < 8; thisPin++)
pinMode(ledPins[thisPin], OUTPUT); { pinMode(thisPin, OUTPUT); }
} }
}
void loop() {
void loop() {
for (int thisPin = 2; thisPin < 8; thisPin++) {
for (int thisPin = 0; thisPin < pinCount; thisPin++) digitalWrite(thisPin, HIGH);
{ delay(timer);
digitalWrite(ledPins[thisPin], HIGH); digitalWrite(thisPin, LOW); }
delay(timer);
digitalWrite(ledPins[thisPin], LOW); for (int thisPin = 7; thisPin >= 2; thisPin--) {
} digitalWrite(thisPin, HIGH);
for (int thisPin = pinCount - 1; thisPin >= 0; thisPin--) delay(timer);
{ digitalWrite(ledPins[thisPin], HIGH);
digitalWrite(thisPin, LOW);
delay(timer);
digitalWrite(ledPins[thisPin], LOW); }
}} }
3
3. SI DECLARACIÓN (DECLARACIÓN
CONDICIONAL) IF 4.Switch (case) Statement, used
with sensor input
if (Serial.available() > 0) {
int inByte = Serial.read();
switch (inByte) {
case 'a':
digitalWrite(2, HIGH);
break;
case 'b':
digitalWrite(3, HIGH);
break;
case 'c':
digitalWrite(4, HIGH);
break;
case 'd':
Imagen 7: visualization serial. digitalWrite(5, HIGH);
break;
case 'e':
5. witch (case) Statement, used digitalWrite(6, HIGH);
break;
with serial input default:
// turn all the LEDs off:
for (int thisPin = 2; thisPin < 7; thisPin++) {
digitalWrite(thisPin, LOW);
}
}
}
}
6. While Loop
Serial.begin(9600);
}
void loop() {
while (digitalRead(buttonPin) == HIGH) {
calibrate();
}
digitalWrite(indicatorLedPin, LOW);
sensorValue = analogRead(sensorPin);
sensorValue = map(sensorValue, sensorMin,
sensorMax, 0, 255);
sensorValue = constrain(sensorValue, 0, 255);
Serial.println(sensorValue);
analogWrite(ledPin, sensorValue);
}
3. Keyboard Reprogram
Código Arduino:
El puerto serial devuelve la respuesta 1 cuando por el Imagen 17: puerto serial.
puerto seria le envió un 0
8
}
}
Código Arduino:
void setup() {
pinMode(upButton, INPUT);
pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
pinMode(rightButton, INPUT);
pinMode(mouseButton, INPUT);
Serial.begin(9600);
Mouse.begin();
Keyboard.begin();
}
void loop() {
if (Serial.available() > 0) {
char inChar = Serial.read(); Imagen 18: puerto serial.
switch (inChar) {
case 'u': El siguiente circuito trabaja como un reemplazo del
Mouse.move(0, -40); mouse haciendo uso de los pulsadores
break;
case 'd': Código Arduino:
Mouse.move(0, 40);
break; #include "Mouse.h"
case 'l': #include "HID.h"
Mouse.move(-40, 0); const int upButton = 2;
break; const int downButton = 3;
case 'r': const int leftButton = 4;
Mouse.move(40, 0); const int rightButton = 5;
break; const int mouseButton = 6;
case 'm': int range = 5;
Mouse.click(MOUSE_LEFT); int responseDelay = 10;
break; void setup() {
} pinMode(upButton, INPUT);
} pinMode(downButton, INPUT);
pinMode(leftButton, INPUT);
// use the pushbuttons to control the keyboard: pinMode(rightButton, INPUT);
if (digitalRead(upButton) == HIGH) { pinMode(mouseButton, INPUT);
Keyboard.write('u'); // initialize mouse control:
} Mouse.begin();
if (digitalRead(downButton) == HIGH) { }
Keyboard.write('d'); void loop() {
} int upState = digitalRead(upButton);
if (digitalRead(leftButton) == HIGH) { int downState = digitalRead(downButton);
Keyboard.write('l'); int rightState = digitalRead(rightButton);
} int leftState = digitalRead(leftButton);
if (digitalRead(rightButton) == HIGH) { int clickState = digitalRead(mouseButton);
Keyboard.write('r'); int xDistance = (leftState - rightState) * range;
} int yDistance = (upState - downState) * range;
if (digitalRead(mouseButton) == HIGH) { if ((xDistance != 0) || (yDistance != 0)) {
Keyboard.write('m'); Mouse.move(xDistance, yDistance, 0);
9
} Mouse.begin();
if (clickState == HIGH) { }
if (!Mouse.isPressed(MOUSE_LEFT)) { void loop() {
Mouse.press(MOUSE_LEFT); } // read the switch:
int switchState = digitalRead(switchPin);
} if (switchState != lastSwitchState) {
else { if (switchState == HIGH) {
if (Mouse.isPressed(MOUSE_LEFT)) { mouseIsActive = !mouseIsActive;
Mouse.release(MOUSE_LEFT); digitalWrite(ledPin, mouseIsActive);
} }
} }
delay(responseDelay); lastSwitchState = switchState;
} int xReading = readAxis(A0);
int yReading = readAxis(A1);
if (mouseIsActive) {
7. Button Mouse Control Mouse.move(xReading, yReading, 0);
}
if (digitalRead(mouseButton) == HIGH) {
if (!Mouse.isPressed(MOUSE_LEFT)) {
Mouse.press(MOUSE_LEFT);
}
}
else {
if (Mouse.isPressed(MOUSE_LEFT)) {
Mouse.release(MOUSE_LEFT);
}
}
delay(responseDelay);
}
int readAxis(int thisAxis) {
int reading = analogRead(thisAxis);
reading = map(reading, 0, 1023, 0, range);
int distance = reading - center;
Código Arduino:
#include "Mouse.h"
#include "HID.h"
const int switchPin = 2;
const int mouseButton = 3;
const int xAxis = A0;
const int yAxis = A1;
const int ledPin = 5;
int range = 12;
int responseDelay = 5; Imagen 20: puerto serial.
int threshold = range / 4;
int center = range / 2; Imprime en el puerto serial las tabas de conversión
boolean mouseIsActive = false;
int lastSwitchState = LOW;
void setup() {
Código Arduino:
pinMode(switchPin, INPUT);
pinMode(ledPin, OUTPUT);
// take control of the mouse: void setup() {
10
Serial.begin(9600);
while (!Serial) {
}
Serial.println("ASCII Table ~ Character Map"); Código Arduino:
}
int thisByte = 33; const int ledPin = 9;
void loop() { void setup() {
Serial.write(thisByte); Serial.begin(9600);
Serial.print(", dec: "); pinMode(ledPin, OUTPUT);
Serial.print(thisByte); }
Serial.print(", hex: "); void loop() {
Serial.print(thisByte, HEX); byte brightness;
Serial.print(", oct: "); if (Serial.available()) {
Serial.print(thisByte, OCT); brightness = Serial.read();
Serial.print(", bin: "); analogWrite(ledPin, brightness);
Serial.println(thisByte, BIN); }
}
if (thisByte == 126) {
while (true) { Código procesing:
continue;
} import processing.serial.*;
} Serial port;
thisByte++; void setup()
} { size(256, 150);
println("Available serial ports:");
9. Dimmer en este ejercicio printArray(Serial.list());
port = new Serial(this, "COM9", 9600);
necesitan processing }
void draw() {
for (int i = 0; i < 256; i++) {
stroke(i); line(i, 0, i, 150); }
port.write(mouseX);
}
10. Graph
Código arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.println(analogRead(A0));
delay(2); Imagen 25: circuito
} En este circuito vemos como envía notas midi desde nuestra
placa arduino a un instrumento a través de una clavija din de 5
Código processing: pines usando el puerto TX e imprime en el puerto serie la
lectura.
import processing.serial.*;
Serial myPort;
int xPos = 1;
void setup () {
printArray(Serial.list());
size: size(400, 300);
myPort = new Serial(this, Serial.list()[1], 9600);
myPort.bufferUntil('\n');
background(0); }
void draw () {}
void serialEvent (Serial myPort) {
String inString = myPort.readStringUntil('\n');
if (inString != null) {
inString = trim(inString); Imagen 26: puerto serie
float inByte = float(inString);
inByte = map(inByte, 0, 1023, 0, height);
stroke(127,34,255); Código arduino:
line(xPos, height, xPos, height - inByte);
if (xPos >= width) { void setup() {
Serial.begin(31250);
xPos = 0;
}
background(0); } void loop() {
else { for (int note = 0x1E; note < 0x5A; note ++) {
xPos++; } } } noteOn(0x90, note, 0x45);
delay(100);
noteOn(0x90, note, 0x00);
delay(100);
}
}
void noteOn(int cmd, int pitch, int velocity) {
Serial.write(cmd);
Serial.write(pitch);
Serial.write(velocity);
}
12
} }
}
void loop() {
Codigo prosessing: while (Serial.available() > 0) {
int red = Serial.parseInt();
void setup() { int green = Serial.parseInt();
size(200, 200); int blue = Serial.parseInt();
boxX = width/2.0;
boxY = height/2.0; if (Serial.read() == '\n') {
rectMode(RADIUS); red = 255 - constrain(red, 0, 255);
printArray(Serial.list()); green = 255 - constrain(green, 0, 255);
port = new Serial(this, "COM3", 9600); } blue = 255 - constrain(blue, 0, 255);
void draw()
{ background(0); analogWrite(redPin, red);
if (mouseX > boxX-boxSize && analogWrite(greenPin, green);
mouseX < boxX+boxSize && mouseY > boxY- analogWrite(bluePin, blue);
boxSize && mouseY < boxY+boxSize) {
mouseOverBox = true; Serial.print(red, HEX);
stroke(255); Serial.print(green, HEX);
fill(153); Serial.println(blue, HEX);
port.write('H'); } }
else { }
stroke(153); }
fill(153);
port.write('L');
mouseOverBox = false; } 15. SerialEvent
rect(boxX, boxY, boxSize, boxSize); }
Código arduino :
void setup() {
Serial.begin(9600);
// make the pins outputs:
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT); Imagen 32: circuito a arduino
pinMode(bluePin, OUTPUT);
14
Codigo Arduino:
void setup() {
Serial.begin(9600);
}
void loop() {
Serial.print(analogRead(redPin));
Serial.print(",");
Serial.print(analogRead(greenPin));
Serial.print(",");
Serial.println(analogRead(bluePin));
}
IV. CONCLUSIONES
I. Ardino es una herramienta muy
necesaria para la realización de proyevtos
universitarios ya que nos ofrece una
plataforma libre en la cual podemos aplicar
nuestra creatividad.