/* ============================================
code is placed under the MIT license
Copyright (c) 2023 J-M-L
For the Arduino Forum : https://forum.arduino.cc/u/j-m-l
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.
===============================================
*/
#include <Toggle.h> // https://github.com/Dlloydev/Toggle/
const byte brocheBouton = 2;
const byte brocheLed = 3;
const byte brocheLedRouge = 9;
const byte brocheLedVerte = 10;
const byte brocheLedBleue = 11;
const unsigned long periodeAnimation = 20; // on change le RGB toutes les 20ms
Toggle bouton;
enum {REPOS, APPUYE} etat = REPOS;
byte intensiteRouge, intensiteVert, intensiteBleu;
void allumerLed() {
digitalWrite(brocheLed, HIGH);
}
void eteindreLed() {
digitalWrite(brocheLed, LOW);
}
void miseAJourRGB() {
analogWrite(brocheLedRouge, intensiteRouge);
analogWrite(brocheLedVerte, intensiteVert);
analogWrite(brocheLedBleue, intensiteBleu);
}
void gestionBouton() {
static unsigned long chrono;
bouton.poll();
switch (etat) {
case REPOS:
if (bouton.onPress()) { // on était au repos, on appuie sur le bouton
intensiteRouge = 0;
intensiteVert = 0;
intensiteBleu = 0;
miseAJourRGB();
allumerLed();
chrono = millis(); // on note le moment du dernier changement des LEDs
etat = APPUYE;
}
break;
case APPUYE:
if (bouton.onRelease()) { // on vient de relâcher le boutonn
intensiteRouge = 0;
intensiteVert = 0;
intensiteBleu = 0;
miseAJourRGB();
eteindreLed();
etat = REPOS;
} else { // on était appuyé et ça n'a pas changé, donc on tient le bouton appuyé
if (millis() - chrono >= periodeAnimation) {
// on est resté assez longtems allumé, on change les RGB
// on monte d'abord le rouge à fond, puis le vert, puis le bleu et on recommence
if (intensiteRouge == 255) {
if (intensiteVert == 255) {
if (intensiteBleu == 255) {
intensiteRouge = 0;
intensiteVert = 0;
intensiteBleu = 0;
} else intensiteBleu++;
} else intensiteVert++;
} else intensiteRouge++;
miseAJourRGB();
chrono = millis(); // on note le moment du dernier changement des LEDs
}
}
break;
}
}
void setup() {
pinMode(brocheLed, OUTPUT);
pinMode(brocheLedRouge, OUTPUT);
pinMode(brocheLedVerte, OUTPUT);
pinMode(brocheLedBleue, OUTPUT);
bouton.begin(brocheBouton);
}
void loop() {
gestionBouton();
}