Le code est tout simple, on écoute le port série (115200 bauds) jusqu'à avoir reçu une commande (fin de ligne doit être réglé sur LF ('\n') ou CR+LF (le '\r' est ignoré). On analyse le premier caractère de la commande dans un switch pour appeler la bonne fonction, en passant éventuellement le chiffre qui suivant le code commande.
/* ============================================
code is placed under the MIT license
Copyright (c) 2020 J-M-L
For the Arduino Forum : https://forum.arduino.cc/index.php?topic=672896.msg4530014#msg4530014
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
===============================================
*/
const uint8_t flashPin = 8;
const uint8_t shutterPin = 9;
const uint8_t focusPin = 10;
const uint16_t flashTriggerDelay = 1; // ms
const uint16_t focusTriggerDelay = 250; // ms
const uint16_t shutterMinTriggerDelay = 250; // ms
const uint16_t actionBsmallPause = 50; // ms
void flash()
{
digitalWrite(flashPin, HIGH);
delay(flashTriggerDelay);
digitalWrite(flashPin, LOW);
}
void focus(unsigned long t=0)
{
digitalWrite(focusPin, HIGH);
delay(t > focusTriggerDelay ? t : focusTriggerDelay);
digitalWrite(focusPin, LOW);
}
void photo(unsigned long t = 0)
{
digitalWrite(focusPin, HIGH);
digitalWrite(shutterPin, HIGH);
delay(t > shutterMinTriggerDelay ? t : shutterMinTriggerDelay);
digitalWrite(shutterPin, LOW);
digitalWrite(focusPin, LOW);
}
void photoWithFlash(unsigned long t = 0)
{
unsigned long demiT = t / 2;
digitalWrite(focusPin, HIGH);
digitalWrite(shutterPin, HIGH);
if (demiT > shutterMinTriggerDelay) delay(demiT);
else delay(shutterMinTriggerDelay);
digitalWrite(flashPin, HIGH);
delay(flashTriggerDelay);
if (demiT > shutterMinTriggerDelay + flashTriggerDelay) delay(demiT - shutterMinTriggerDelay - flashTriggerDelay);
digitalWrite(flashPin, LOW);
digitalWrite(shutterPin, LOW);
digitalWrite(focusPin, LOW);
}
// -------- GESTION DES COMMANDES SERIES ------------
const byte tailleMax = 10;
char messageRecu[tailleMax + 1] = ""; // +1 pour stocker un '\0' à la fin de la chaîne
byte index = 0;
boolean recevoirMessage(const char finDeChaine)
{
boolean fin = false;
if (Serial.available()) { // si on a au moins un charactère en attente
int recu = Serial.read(); // -1 si erreur, sinon notre charactère
if (recu != -1) {
if (recu == finDeChaine) fin = true;
else {
if (recu != '\r') { // on ignore CR
messageRecu[index++] = (char) recu;
messageRecu[index] = '\0'; // on marque la fin de chaîne
if (index >= tailleMax) index = tailleMax - 1; // évite de déborder
}
}
}
}
return fin;
}
void toutEteint()
{
digitalWrite(flashPin, LOW);
digitalWrite(shutterPin, LOW);
digitalWrite(focusPin, LOW);
}
void menuGeneral()
{
toutEteint();
Serial.println(F("\n(C)\t\tCoup de flash"));
Serial.println(F("(M/Mxxx)\tMise au point (appui xxx ms)"));
Serial.println(F("(P/Pxxx)\tPrendre une photo (appui xxx ms)"));
Serial.println(F("(Fxxx)\t\tPrendre une photo PAUSE B avec coup de Flash au milieu du temps d'attente (appui xxx ms)"));
Serial.print(F("Entrez une commande:"));
}
void setup()
{
pinMode(flashPin, OUTPUT);
pinMode(shutterPin, OUTPUT);
pinMode(focusPin, OUTPUT);
toutEteint();
Serial.begin(115200);
Serial.println(F("--- PRISE DE PHOTO ---"));
menuGeneral();
}
void loop()
{
if (recevoirMessage('\n')) {
Serial.println(messageRecu);
switch (messageRecu[0]) {
case 'C':
flash();
break;
case 'M': // expecting 'M', "Mxxxx" where xxx is the duration of the press in ms
focus(atol(messageRecu + 1));
break;
case 'P': // expecting 'P', "Pxxxx" where xxx is the duration of the press in ms
photo(atol(messageRecu + 1));
break;
case 'F':// expecting F or Fxxxx where xxx is the duration of the press in ms
photoWithFlash(atol(messageRecu + 1));
break;
default: break; // on ne fait rien
}
// On re-initalise notre buffer
index = 0;
messageRecu[0] = '\0';
menuGeneral();
}
}
Voilà. Vous avez la base pour pouvoir bosser. Maintenant au lieu de lire le port Série, il suffit de lire un capteur de détection et appeler la fonction que vous voulez pour que la photo se déclenche
un petit exemple (en Islande) sur mouvement (photos recadrées)
amusez vous bien !