2023-10-07 08:28:28 +02:00
|
|
|
#include "Servo.h"
|
2023-10-04 15:27:32 +02:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* servo-test.ino
|
2023-10-07 08:28:28 +02:00
|
|
|
* @title: Test de pilotage d'un servomoteur
|
|
|
|
* @project: Blender-EduTech - Tutoriel 6 : Labyrinthe à bille - Développer le jumeau numérique du labyrinthe
|
2023-10-04 15:27:32 +02:00
|
|
|
* @lang: fr
|
|
|
|
* @authors: Philippe Roy <philippe.roy@ac-grenoble.fr>
|
|
|
|
* @copyright: Copyright (C) 2023 Philippe Roy
|
|
|
|
* @license: GNU GPL
|
|
|
|
******************************************************************************/
|
|
|
|
|
2023-10-07 08:28:28 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Servo-moteurs
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
Servo servo;
|
|
|
|
String servo_txt;
|
|
|
|
String servo_txt2;
|
|
|
|
bool servo_sens;
|
|
|
|
int angle_ms;
|
|
|
|
|
2023-10-04 15:27:32 +02:00
|
|
|
/******************************************************************************
|
|
|
|
* Initialisation
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
void setup() {
|
|
|
|
|
2023-10-07 08:28:28 +02:00
|
|
|
// Liaison série
|
|
|
|
Serial.begin(115200);
|
2023-10-04 15:27:32 +02:00
|
|
|
|
2023-10-07 08:28:28 +02:00
|
|
|
// 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);
|
2023-10-04 15:27:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* Boucle principale
|
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
void loop() {
|
2023-10-07 08:28:28 +02:00
|
|
|
|
|
|
|
// 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); */
|
|
|
|
|
2023-10-04 15:27:32 +02:00
|
|
|
}
|