blender-edutech-tutoriels/labyrinthe/6-jumeaux/test/servo-test/servo-test.ino

104 lines
3.0 KiB
C++

#include "Servo.h"
/******************************************************************************
* servo-test.ino
* @title: Test de pilotage d'un servomoteur
* @project: Blender-EduTech - Tutoriel 6 : Labyrinthe à bille - Développer le jumeau numérique du labyrinthe
* @lang: fr
* @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
* @copyright: Copyright (C) 2023 Philippe Roy
* @license: GNU GPL
******************************************************************************/
/******************************************************************************
* Servo-moteurs
******************************************************************************/
Servo servo;
String servo_txt;
String servo_txt2;
bool servo_sens;
int angle_ms;
/******************************************************************************
* Initialisation
******************************************************************************/
void setup() {
// Liaison série
Serial.begin(115200);
// Servo-moteurs
servo.attach(9); // Servo sur broche D9
servo_sens = true;
angle_ms = 1500; // Position centrale (90°, 500 -> 0° et 2500-> 180°)
servo.writeMicroseconds(angle_ms);
}
/******************************************************************************
* Boucle principale
******************************************************************************/
void loop() {
// Affichage
servo_txt = String(servo.read());
servo_txt2 = String(angle_ms);
Serial.print(servo_txt2);
Serial.print(" -> ");
Serial.print(servo_txt);
Serial.println("°");
// Sens
if (servo.read()==170) {
servo_sens=false;
}
if (servo.read()==10) {
servo_sens=true;
}
// Mouvement
if (servo_sens) {
angle_ms=angle_ms+1; // Résolution à 180/2000 soit 0,09°
//servo1.write(servo1.read()+1); // Résolution à 1°
} else {
angle_ms=angle_ms-1; // Résolution à 180/2000 soit 0,09°
//servo1.write(servo1.read()-1); // Résolution à 1°
}
servo.writeMicroseconds(angle_ms);
delay(10);
// Test des positions
/* angle_ms =1500; */
/* servo.writeMicroseconds(angle_ms); */
/* servo_txt = String(servo.read()); */
/* servo_txt2 = String(angle_ms); */
/* Serial.print(servo_txt2); */
/* Serial.print(" -> "); */
/* Serial.print(servo_txt); */
/* Serial.println("°"); */
/* delay(1000); */
/* angle_ms =500; */
/* servo.writeMicroseconds(angle_ms); */
/* servo_txt = String(servo.read()); */
/* servo_txt2 = String(angle_ms); */
/* Serial.print(servo_txt2); */
/* Serial.print(" -> "); */
/* Serial.print(servo_txt); */
/* Serial.println("°"); */
/* delay(1000); */
/* angle_ms =2500; */
/* servo.writeMicroseconds(angle_ms); */
/* servo_txt = String(servo.read()); */
/* servo_txt2 = String(angle_ms); */
/* Serial.print(servo_txt2); */
/* Serial.print(" -> "); */
/* Serial.print(servo_txt); */
/* Serial.println("°"); */
/* delay(1000); */
}