Arduino Duemilanove |
Para construir este juego solo necesitamos una placa Arduino UNO, 3 LEDs, cables, 2 botones, 5 resistencias (3 de 1K ohms para los LEDs y otras 2 de 10k ohms para los botones) y una pequeña breadboard.
Un ejemplo básico entre un botón y un LED |
En la página oficial de Arduino existen diversos tutoriales, donde explican detalladamente los distintos componentes de la placa, instalación y la forma de diseñar y programar. Si ya entiendes los conceptos básicos entonces estamos listos para revisar algo de código:
Mole.h
Este archivo corresponde a la cabecera de nuestro proyecto, contiene las declaraciones de los métodos y atributos.
/*
Mole.h - Library for mole game.
Created by brionescl, July 23, 2012.
Released into the public domain.
*/
#ifndef Mole_h
#define Mole_h
#include "Arduino.h"
#define MAXLEDS 2
#define MAXBUTTONS 2
class Mole {
public:
Mole();
void setLed(int pin);
void setLedError(int pin);
void setButton(int pin);
void switchLed(int ledId, bool state);
void switchLedError(bool state);
void calculateRandomLed();
void captureButtonPress();
int timeLapse;
int maxTimeLapse;
int buttonPress;
int randomLed;
private:
int _ledId;
int _buttonId;
int _leds[MAXLEDS];
int _buttons[MAXBUTTONS];
int _pinLedError;
};
#endif
Mole.cpp
Aquí se encuentra el desarrollo (lógica) de los métodos y uso de los atributos de la clase "Mole".
/*
Mole.cpp - Library for mole game.
Created by brionescl, July 23, 2012.
Released into the public domain.
*/
#include "Arduino.h"
#include "Mole.h"
Mole::Mole() {
_ledId = 0;
_buttonId = 0;
randomLed = -1;
timeLapse = 0;
maxTimeLapse = 1000; // 1 second
buttonPress = -1;
randomSeed(analogRead(0));
}
void Mole::setLed(int pin) {
if (_ledId < MAXLEDS) {
_leds[_ledId++] = pin;
pinMode(pin, OUTPUT);
}
}
void Mole::setButton(int pin) {
if (_buttonId < MAXBUTTONS) {
_buttons[_buttonId++] = pin;
pinMode(pin, INPUT);
}
}
void Mole::setLedError(int pin) {
_pinLedError = pin;
pinMode(pin, OUTPUT);
}
void Mole::switchLed(int ledId, bool state) {
if (state == true) {
digitalWrite(_leds[ledId], HIGH);
} else if (state == false) {
digitalWrite(_leds[ledId], LOW);
}
}
void Mole::switchLedError(bool state) {
if (state == true) {
digitalWrite(_pinLedError, HIGH);
} else if (state == false) {
digitalWrite(_pinLedError, LOW);
}
}
void Mole::calculateRandomLed() {
randomLed = random(0, MAXLEDS);
}
void Mole::captureButtonPress() {
int button = 0;
for (int buttonId = 0; buttonId < MAXBUTTONS; buttonId++) {
button = digitalRead(_buttons[buttonId]);
if (button == HIGH) {
buttonPress = buttonId;
break;
} else {
buttonPress = -1;
}
}
}
Mole.ino
La definición de nuestro sketch
#include <Mole.h>
const bool ON = true;
const bool OFF = false;
Mole mole;
void setup() {
//Serial.begin(9600);
// pin LEDs
mole.setLed(12); // 1 kohms +/-5
mole.setLed(11); // 1 kohms +/-5
// LED error
mole.setLedError(10); // 1 kohms +/-5
// pin Button
// 3.3v
mole.setButton(3); // 10 kohms +/-5
mole.setButton(2); // 10 kohms +/-5
mole.calculateRandomLed();
mole.maxTimeLapse = 2000; // 2 seconds
}
void loop() {
mole.captureButtonPress();
if (mole.buttonPress != -1) {
//Serial.println(mole.buttonPress);
mole.switchLed(mole.randomLed, OFF);
if (mole.randomLed == mole.buttonPress) {
delay(1000);
} else {
mole.switchLedError(ON);
delay(2000);
mole.switchLedError(OFF);
}
mole.timeLapse = 0;
mole.calculateRandomLed();
} else {
//Serial.println("NO PRESS");
}
if (mole.timeLapse == mole.maxTimeLapse) {
mole.timeLapse = 0;
mole.switchLed(mole.randomLed, OFF);
mole.switchLedError(ON);
delay(500);
mole.switchLedError(OFF);
mole.calculateRandomLed();
} else {
mole.switchLed(mole.randomLed, ON);
delay(1);
mole.timeLapse++;
}
}
![]() |
Diseño y cableado del proyecto |
Fuentes