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
|
|
|
/******************************************************************************
|
2023-10-12 00:09:05 +02:00
|
|
|
* Servomoteur
|
2023-10-07 08:28:28 +02:00
|
|
|
******************************************************************************/
|
|
|
|
|
|
|
|
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-12 00:09:05 +02:00
|
|
|
// Servomoteur
|
|
|
|
servo.attach(6); // Servomoteur sur broche D9
|
2023-10-07 08:28:28 +02:00
|
|
|
servo_sens = true;
|
2023-10-12 00:09:05 +02:00
|
|
|
angle_ms = 600; // 500 -> 0°, 1500 -> 90° et 2500-> 180°
|
|
|
|
servo.writeMicroseconds(angle_ms); // 600 -> 9°
|
|
|
|
/* servo.write(10); */
|
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
|
2023-10-12 00:09:05 +02:00
|
|
|
if (servo.read()==90) {
|
2023-10-07 08:28:28 +02:00
|
|
|
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°
|
2023-10-12 00:09:05 +02:00
|
|
|
/* servo.write(servo.read()+1); // Résolution à 1° */
|
|
|
|
/* delay(100); */
|
2023-10-07 08:28:28 +02:00
|
|
|
} else {
|
|
|
|
angle_ms=angle_ms-1; // Résolution à 180/2000 soit 0,09°
|
2023-10-12 00:09:05 +02:00
|
|
|
/* servo.write(servo.read()-1); // Résolution à 1° */
|
|
|
|
/* delay(100); */
|
2023-10-07 08:28:28 +02:00
|
|
|
}
|
|
|
|
servo.writeMicroseconds(angle_ms);
|
2023-10-12 00:09:05 +02:00
|
|
|
delay(5);
|
2023-10-07 08:28:28 +02:00
|
|
|
|
|
|
|
// 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
|
|
|
}
|