Buenas tardes,
Acabo de descubrir Arduino. No se donde había vivido yo toda la vida sin conocer este sistema...
Todo empezó por que quiero hacer un fotomatón casero para la boda de un familiar. Pensaba hacerlo con usa serie de relés de una manera muy simple y entonces encontré Arduino. En su día oí hablar de Arduino pero no me paré mirar ni que era.
Para este primer proyecto con Arduino, me he basado en el código de DIY Machines.
Básicamente el funcionamiento es el siguiente:
Tengo un botón, que al pulsarlo empieza una cuenta atrás de 10 segundos que se muestra en una pantalla led Matrix 8x32 con controlador MAX7219. Desués de esos 10 segundos activa un relé (que será el disparador de la cámara) para hacer otras 2 cuentas atrás de 5 segundos con el mismo procedimiento. En total se harán 3 fotos. Entre cada foto, se muestran diferentes mensajes. Este es el código:
/*
* Arduino controlled Photo Booth - DIY Machines
This is an easy to build Arduino powered photo booth. You can customise the housing to suit your event/wedding and as it's controlled by an Arduino Nano you don't need anyone to 'man it' throughout the night.
==========
More info: https://diymachines.co.uk/
3D printed parts can be downloaded from here: https://www.thingiverse.com/DIY_Machines/designs
Project video: https://youtu.be/Fu5Gbpv4EYs
==========
* SAY THANKS:
Buy me a coffee to say thanks: https://ko-fi.com/diymachines
Support us on Patreon: https://www.patreon.com/diymachines
SUBSCRIBE:
■ https://www.youtube.com/channel/UC3jc4X-kEq-dEDYhQ8QoYnQ?sub_confirmation=1
INSTAGRAM: https://www.instagram.com/diy_machines/?hl=en
FACEBOOK: https://www.facebook.com/diymachines/
*/
int shutterPin = 12;
int buttonPin = 8;
int buttonVal = 0; //somewhere to store the button state
int BUTTONLED = 9;
#define NUM_MAX 4
#define ROTATE 90
#define DIN_PIN 11 //
#define CS_PIN 10 //
#define CLK_PIN 13 //
#include "max7219.h"
#include "fonts.h"
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // open the serial port at 9600 bps:
pinMode(shutterPin, OUTPUT);
digitalWrite(shutterPin, HIGH);
pinMode(BUTTONLED, OUTPUT);
digitalWrite(BUTTONLED, HIGH);
pinMode(buttonPin, INPUT);
digitalWrite(buttonPin, HIGH); //enable internal pull up resistor
initMAX7219();
sendCmdAll(CMD_SHUTDOWN,1);
sendCmdAll(CMD_INTENSITY,15);
}
void loop() {
// put your main code here, to run repeatedly:
buttonVal = digitalRead(buttonPin);
Serial.println(buttonVal);
delay(200);
if (buttonVal == LOW) {
digitalWrite(BUTTONLED, LOW);
takePhoto1();
printStringWithShift("OTRA",0);
delay(2000);
takePhoto2();
printStringWithShift("Ultima",0);
delay(2000);
takePhoto2();
printStringWithShift("LISTO ",0);
delay(3000);
printStringWithShift(" ",0);
digitalWrite(BUTTONLED, HIGH);
}
}
int showChar(char ch, const uint8_t *data)
{
int len = pgm_read_byte(data);
int i,w = pgm_read_byte(data + 1 + ch * len);
for (i = 0; i < w; i++)
scr[NUM_MAX*8 + i] = pgm_read_byte(data + 1 + ch * len + 1 + i);
scr[NUM_MAX*8 + i] = 0;
return w;
}
// =======================================================================
void printCharWithShift(unsigned char c, int shiftDelay) {
if (c < ' ' || c > MAX_CHAR) return;
c -= 32;
int w = showChar(c, font);
for (int i=0; i<w+1; i++) {
delay(shiftDelay);
scrollLeft();
refreshAll();
}
}
// =======================================================================
void printStringWithShift(const char* s, int shiftDelay){
while (*s) {
printCharWithShift(*s++, shiftDelay);
}
}
// =======================================================================
void takePhoto1() {
printStringWithShift(" 10 9 8 7 6 5 4 3 2 1 ",20);
printStringWithShift("SONRIE!",0);
delay(1000);
digitalWrite(shutterPin, LOW); // turn the LED off by making the voltage LOW
printStringWithShift(" ",0);
delay(1000);
digitalWrite(shutterPin, HIGH);
}
void takePhoto2() {
printStringWithShift(" 5 4 3 2 1 ",20);
printStringWithShift("SONRIE!",0);
delay(1000);
digitalWrite(shutterPin, LOW); // turn the LED off by making the voltage LOW
printStringWithShift(" ",0);
delay(1000);
digitalWrite(shutterPin, HIGH);
}
Como se puede ver, el loop está esperando a la orden del botón y cuando este se pulsa comienza a ejecutarse todo el código.
Lo que quiero hacer, es mostrar un mensaje mientras no se esté ejecutando el código y que al pulsarse el botón se ejecute el código como hasta ahora.
El problema es que mientras se muestra el mensaje de espera, no captura la pulsación del botón, ya que el procediminto del mensaje hace que no se esté ejecutando el loop.
¿Hay alguna manera para solucionar este "problema"?
No se si hay que hacer un loop múltiple o como se podría hacer. No he encontrado la solución tampoco con San Google.
Muchas gracias!