/*
============================================
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.
===============================================
Câblage
PIN
0 NC (Rx)
1 NC (Tx)
2 pinPalettePlus pin 2 --- bouton --- GND
3 pinPaletteMoins pin 3 --- bouton --- GND
4 NC
5 pinServo pin 5 --- pin contrôle angle servo
6 7 Segment 'a' Prévoir résistance de limitation de courant (cf https://eskimon.fr/tuto-arduino-205-afficheurs-7-segments)
7 7 Segment 'b' Prévoir résistance de limitation de courant
8 7 Segment 'c' Prévoir résistance de limitation de courant
9 7 Segment 'd' Prévoir résistance de limitation de courant
10 7 Segment 'e' Prévoir résistance de limitation de courant
11 7 Segment 'f' Prévoir résistance de limitation de courant
12 7 Segment 'g' Prévoir résistance de limitation de courant
13 NC Led interne
A0 pinReglagePlus pin A0 --- bouton --- GND
A1 pinReglageMoins pin A1 --- bouton --- GND
A2
A3
A4
A5
*/
#include <Servo.h>
Servo levierVitesse;
const byte pinServo = 5;
#include "simpleBouton.h" // cf @bricoleau @ http://forum.arduino.cc/index.php?topic=375232.0
// le changement de vitesse
const byte pinPalettePlus = 2; //Cablage : pin 2 --- bouton --- GND
const byte pinPaletteMoins = 3; //Cablage : pin 3 --- bouton --- GND
simpleBouton boutonPlus(pinPalettePlus);
simpleBouton boutonMoins(pinPaletteMoins);
// le réglage de l'angle du servo
const byte pinReglagePlus = A0;
const byte pinReglageMoins = A1;
simpleBouton boutonReglagePlus(pinReglagePlus);
simpleBouton boutonReglageMoins(pinReglageMoins);
const byte pin7Seg[] = {12, 11, 10, 9, 8, 7, 6};
// de 0 à 9 sous la forme 0babcdefg et un 11ème vide, sous la forme 0babcdefg
const byte chiffres7Seg[] = {
/* 0 */ 0b1111110, /* 1 */ 0b0110000, /* 2 */ 0b1101101, /* 3 */ 0b1111001, /* 4 */ 0b0110011,
/* 5 */ 0b1011011, /* 6 */ 0b0011111, /* 7 */ 0b1110000, /* 8 */ 0b1111111, /* 9 */ 0b1110011,
/* x */ 0b0000000
};
void affichageChiffre(byte chiffre) { // de 0 à 9
if (chiffre >= sizeof chiffres7Seg) chiffre = (sizeof chiffres7Seg) - 1; // le dernier n'a aucun affichage
for (byte b = 0; b < sizeof pin7Seg; b++) digitalWrite(pin7Seg[b], bitRead(chiffres7Seg[chiffre], b) ? HIGH : LOW);
}
const int minAngle = 700;
const int maxAngle = 2300;
int boite[] = {700, 930, 1160, 1390, 1620, 1850, 2080}; // les valeurs d'angles en µs
const byte nombreDeVitesses = sizeof boite / sizeof boite[0];
byte vitesse = 0; // vitesse par défaut
inline void reglerVitesse() {
levierVitesse.writeMicroseconds(boite[vitesse]);
Serial.print(F("Vitesse ")); Serial.print(vitesse);
Serial.print(F("\tangle ")); Serial.println(boite[vitesse]);
affichageChiffre(vitesse);
}
void vitessePlus() {
if (vitesse < nombreDeVitesses - 1) {
vitesse++;
reglerVitesse();
}
}
void vitesseMoins() {
if (vitesse > 0) {
vitesse--;
reglerVitesse();
}
}
void reglagePlus() {
if (++boite[vitesse] > maxAngle) boite[vitesse] = maxAngle;
reglerVitesse();
}
void reglageMoins() {
if (--boite[vitesse] < minAngle) boite[vitesse] = minAngle;
reglerVitesse();
}
void gestionPalettes() {
boutonPlus.actualiser();
if (boutonPlus.vientDEtreEnfonce()) vitessePlus();
else {
boutonMoins.actualiser();
if (boutonMoins.vientDEtreEnfonce()) vitesseMoins();
}
}
void gestionAngle() {
boutonReglagePlus.actualiser();
if (boutonReglagePlus.vientDEtreEnfonce()) reglagePlus();
else {
boutonReglageMoins.actualiser();
if (boutonReglageMoins.vientDEtreEnfonce()) reglageMoins();
}
}
void setup() {
for (byte b = 0; b < sizeof pin7Seg; b++) pinMode(pin7Seg[b], OUTPUT);
Serial.begin(115200);
reglerVitesse();
levierVitesse.attach(pinServo);
}
void loop() {
gestionPalettes();
gestionAngle();
}