/* ============================================
code is placed under the MIT license
Copyright (c) 2024 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>
#include<Servo.h>
#include <EEPROM.h> // to do
const byte servoPins[] = {9, 10};
const byte nbServo = sizeof servoPins / sizeof * servoPins;
Servo servos[nbServo];
const byte potPins[nbServo] = {A0, A1};
static_assert(nbServo == sizeof(potPins) / sizeof(*potPins), "servoPins et potPins doivent avoir le même nombre de pins.");
const byte recordPin = 3;
const byte playPin = 2;
Toggle boutonRecord, boutonPlay;
struct ServoPos {
int16_t angle[nbServo];
};
const byte maxPosition = 100;
int nbPositions = 0;
byte positionEnCours = 0;
ServoPos lesPositions[maxPosition];
const unsigned long tpsEntreDeuxPas = 100;
const unsigned long tpsEntrePositions = 5000;
unsigned long debutMouvement;
unsigned long chronoMouvement;
enum {RECORDING, PLAYING} mode = RECORDING;
void setup() {
for (byte i = 0; i < nbServo; i++) {
servos[i].write(map(analogRead(potPins[i]), 0, 1023, 0, 180));
servos[i].attach(servoPins[i]);
}
boutonRecord.begin(recordPin);
boutonPlay.begin(playPin);
Serial.begin(115200);
}
void loop() {
switch (mode) {
case RECORDING:
boutonPlay.poll();
if (boutonPlay.onPress()) {
if (nbPositions != 0) {
positionEnCours = 0;
for (byte i = 0; i < nbServo; i++) servos[i].write(lesPositions[0].angle[i]);
chronoMouvement = debutMouvement = millis();
mode = PLAYING;
} else {
Serial.println("aucune position enregistrée à jouer");
}
break;
}
boutonRecord.poll();
if (boutonRecord.onPress()) {
for (byte i = 0; i < nbServo; i++) {
lesPositions[nbPositions].angle[i] = map(analogRead(potPins[i]), 0, 1023, 0, 180);
}
if (++nbPositions >= maxPosition) nbPositions = maxPosition - 1; // ne pas déborder, on écrasera la dernière
Serial.println("ici besoin d'écrire le tableau en EEPROM - to do");
break;
}
for (byte i = 0; i < nbServo; i++) servos[i].write(map(analogRead(potPins[i]), 0, 1023, 0, 180));
break;
case PLAYING:
if (positionEnCours < nbPositions - 1) {
if (millis() - debutMouvement <= tpsEntrePositions) {
if (millis() - chronoMouvement >= tpsEntreDeuxPas) {
chronoMouvement += tpsEntreDeuxPas;
for (byte i = 0; i < nbServo; i++) servos[i].write(map(millis() - debutMouvement, 0, tpsEntrePositions, lesPositions[positionEnCours].angle[i], lesPositions[positionEnCours + 1].angle[i]));
}
} else {
debutMouvement = millis();
positionEnCours++;
for (byte i = 0; i < nbServo; i++) servos[i].write(lesPositions[positionEnCours].angle[i]);
}
}
else if (positionEnCours == nbPositions - 1) mode = RECORDING;
break;
}
}