1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
|
// Dans EEPROM
// - le nombre de photo max
// - calcul pour tige filetée (Ratio) (2 valeurs entières)
// - mode avance ou recule
// En mode controle des fonctions possibilité de modifier ces valeurs
// Le plateau va d'une buté à une autre pour mesurer la "distance"
// ##########################################################################################
// Biblothèques
// ##########################################################################################
#include <TimeLib.h>
#include <LiquidCrystal_I2C.h>
#include <EEPROM.h>
// ##########################################################################################
// Variables
// ##########################################################################################
LiquidCrystal_I2C lcd(0x27, 16, 2);// définit le type d'écran lcd pour ma part j'utilise un 16 x 2
// +--------------------------------------------+
// | l'attribution des pins |
// +--------------------------------------------+
// moteurs
const int dirMvt = 5;
const int stepMvt= 6;
const int servomoteur = 4;
// led
const int ledPret = 8; // vert
const int ledMesure = 9; // rouge
// boutons
const int finCourseMini = 10;
const int finCourseMaxi = 11;
const int depart = 12;
const int testServomoteur = 7;
// +--------------------------------------------+
long position = 0;
long resistance = 0;
float RMax = 1024.0;
float Ratio = 0.390625;
long positionMax = 1.0;
int sens = 1;
byte mode = 1;
int nbPhotosMax = 200;
int nbPhotos = 0;
int angle = 90;
int periode=20000;// période entre chaque début d'impulsion en microsecondes
byte renseigne = 0;
// ##########################################################################################
// INITIALISATION
// ##########################################################################################
void setup() {
Serial.begin(9600);
setTime(00,00,00,04,11,2019); // On fixe le temps de départ à 12h00 le 10 janvier 2014
// Préparation écran ----------------------
lcd.init();
lcd.backlight();
lcd.setCursor(0,0);
lcd.print(centre("(c) H. Deconchat"));
lcd.setCursor(0,1);
lcd.print(centre("Presente :"));
litEeprom();
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("Photos Macro"));
lcd.setCursor(0,1);
lcd.print(centre("par Increments"));
// Initialisation pins----------------------
pinMode(servomoteur, OUTPUT); // on prépare le pin en mode OUTPUT
digitalWrite(servomoteur,LOW); // on l'initialise à l'état bas
pinMode(depart, INPUT);
pinMode(testServomoteur, INPUT);
pinMode(finCourseMini, INPUT);
pinMode(finCourseMaxi, INPUT);
pinMode(ledMesure, OUTPUT);
pinMode(ledPret, OUTPUT);
// ------------------------------------------
delay(3000);
lcd.clear();
if (digitalRead(testServomoteur) == HIGH | renseigne != 52)
{ testFonctions(); } // control toutes les fonctionnalités avant de démarer
Ratio = positionMax/RMax;
Serial.println("Départ");
position=0;
resistance=0;
//positionMax
int sensorValue = analogRead(A2);
zero(); // <====================================
Serial.println("");
Serial.println("");
Serial.println("----------------------------------");
Serial.println(" Les réglages sont terminés");
Serial.println("----------------------------------");
Serial.println(" PRET");
Serial.println("----------------------------------");
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("P.M.I."));
lcd.setCursor(0,1);
lcd.print(centre("Enfin pret"));
}
// ##########################################################################################
// #----------------------------------------------------------------------------------------#
// # BOUCLE PRINCIPALE #
// #----------------------------------------------------------------------------------------#
// ##########################################################################################
void loop() {
if (digitalRead(testServomoteur) == HIGH){
setAngle(angle);// on appelle la fonction setAngle définie plus bas
setAngle(-angle);// on appelle la fonction setAngle définie plus bas
}
if (digitalRead(depart) == HIGH) {
digitalWrite(ledPret, LOW);
photographies(); // <====================================
digitalWrite(ledPret, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("P.M.I."));
lcd.setCursor(0,1);
lcd.print(centre("Enfin pret"));
}
else {
int sensorValue = analogRead(A2); // on déplace le plateau
if (abs(resistance-sensorValue)>2){
positionPlateau(sensorValue); // <====================================
}
nbPhotos = analogRead(A1)/5; // on change le nombrede photos
lcd.setCursor(0,0);
lcd.print("nb Photos : " + String(nbPhotos) + " ");
lcd.setCursor(0,1);
lcd.print("position : " + String(sensorValue) + " ");
}
}
// ##########################################################################################
// CALCUL POSITION DESIREE DU PLATEAU SUITE ACTION POTENTIOMETRE
// ##########################################################################################
void positionPlateau(int R){
int ecart = R - resistance; // ecarte entre avant et maintenant
int newPosition = R * Ratio; // calcul la position sur la tige filetée
int p = newPosition-position; // calcul l'écart avec la position actuelle
/*
Serial.println(Ratio, DEC);
ecrire("ancien R = ",resistance); // <====================================
ecrire(", R = ", R); // <====================================
ecrire(", ecart (R-r) = ",ecart); // <====================================
ecrire(", position = ",position); // <====================================
ecrire(", nouvelle position = ", newPosition); // <====================================
ecrire(", Ecart position = ", p); // <====================================
Serial.println("");
*/
tourne(p, finCourseMaxi); // <====================================
position=newPosition;
resistance = R;
}
// ##########################################################################################
// DEPLACE CHARIOT DE nb TOURS ET TEST LES FINS DE COURSES
// ##########################################################################################
int tourne(int nb, int pinFinCourse){
boolean plus = HIGH;
/*int pinFinCourse = finCourseMini;
if (nb>0) {
plus=LOW;
pinFinCourse = finCourseMaxi;
}*/
nb=abs(nb);
digitalWrite(dirMvt, plus);
int x=0;
while (x < nb){
int fincourse = digitalRead(pinFinCourse);
if (fincourse == HIGH)
{
x = nb;
Serial.println("Fin de course atteint");
return x;
}
digitalWrite(stepMvt, HIGH);
delayMicroseconds(1000);
digitalWrite(stepMvt, LOW);
delayMicroseconds(1000);
fincourse = digitalRead(pinFinCourse);
if (fincourse == HIGH) { x = nb; }
x = x + 1;
// Serial.println(x, DEC);
}
}
// ##########################################################################################
// CYCLE POUR PRENDRE LES PHOTO
// ##########################################################################################
void photographies(){
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("Photos en cours"));
Serial.println("Action Photographier");
digitalWrite(ledMesure, HIGH);
int x = 0;
int nx = position / nbPhotos;
Serial.println("");
while (x < nbPhotos){
x = x + 1;
tourne(-nx, finCourseMini); // <====================================
digitalWrite(ledMesure, LOW);
setAngle(angle);// on appelle la fonction setAngle définie plus bas
setAngle(-angle);// on appelle la fonction setAngle définie plus bas
//delay(1000);
digitalWrite(ledMesure, HIGH);
Serial.print("Photo ... n°");
Serial.print(x, DEC);
Serial.print(" / ");
Serial.println(nbPhotos, DEC);
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(centre(String(x) + "/" + String(nbPhotos)));
//delay(1000);
//digitalWrite(ledMesure, HIGH);
}
digitalWrite(ledMesure, LOW);
Serial.println("Action Photographier TERMINE");
zero(); // <====================================
}
// ##########################################################################################
// MET AU POINT ZERO LE CHARIOT
// ##########################################################################################
void zero(){
AtteintFinCourseMini(); // <====================================
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Mettre a 5 maxi");
int sec = second();
boolean rouge = true;
while (analogRead(A2)>5){
if (sec != second()) {
if (rouge) {
digitalWrite(ledPret, HIGH);
digitalWrite(ledMesure, LOW);
rouge=false;
}else {
digitalWrite(ledPret, LOW);
digitalWrite(ledMesure, HIGH);
rouge=true;
}
sec=second();
}
Serial.print("Position actuelle : ");
Serial.print(analogRead(A2),DEC);
Serial.print(" ===> ");
Serial.println("Mettre à la position 0, en tournant le bouton !");
lcd.setCursor(0,1);
lcd.print(" ");
lcd.setCursor(0,1);
lcd.print(centre(String(analogRead(A2))));
}
lcd.clear();
digitalWrite(ledPret, LOW);
digitalWrite(ledMesure, LOW);
Serial.println("Le plateau est à la position 0");
digitalWrite(ledPret, HIGH);
resistance=0;
position=0;
}
// ##########################################################################################
// DEPLACE CHARIOT JUSQU'A ATTEINDRE FINS DE COURSES MINI
// ##########################################################################################
void AtteintFinCourseMini(){
Serial.println("");
Serial.println("Le plateau doit reculer jusqu'au contact fin de course");
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Le plateau doit");
lcd.setCursor(0,1);
lcd.print("arriver en bute");
int fincourse = digitalRead(finCourseMini);
while (fincourse != HIGH){
tourne(-20, finCourseMini);
fincourse = digitalRead(finCourseMini);
}
Serial.println("La fin de course atteint");
Serial.println("");
}
// ##########################################################################################
// met une zéro devant et ne récupère que les n caractaires de la fin
// ##########################################################################################
String metZero(int nb, int longueur){
String valeur = " " + String(nb);
return valeur.substring(valeur.length()-longueur, valeur.length());
}
// ##########################################################################################
// fonction setAngle pour envoyer les impulsions
// ##########################################################################################
void setAngle(int a){
int duree=map(a,0,179,1000,2000);// on transforme l'angle en microsecondes et on stocke dans la variable duree
digitalWrite(servomoteur,LOW);//on met le pin à l'état bas
// la boucle qui suit est nécessaire
// pour laisser le temps au servo d'aller à sa position
for (int t=0;t<300;t++){
digitalWrite(servomoteur,HIGH);// on envoie l'impulsion
delayMicroseconds(duree); // pendant la bonne durée
digitalWrite(servomoteur,LOW); // on stoppe l'impulsion
delayMicroseconds(periode-duree); // on attend le temps restant pour atteindre la période
}
}
// ##########################################################################################
// centre texte sur écran
// ##########################################################################################
String centre(String txt){
String espaces = " ";
int n = (16 - txt.length()) / 2;
return espaces.substring(0, n) + txt;
}
/* #########################################################################################
* E C R I T D A N S E E P R O M
* ######################################################################################### */
void ecritDansEeprom(){
// EEPROM.begin(10);
Serial.println();
Serial.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
Serial.println("% Ecriture dans l'EEPROM %");
Serial.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
byte a = sqrt(nbPhotosMax);
byte b = nbPhotosMax - (a * a);
byte RatioA = sqrt(positionMax);
byte RatioB = positionMax - (RatioA * RatioA);
EEPROM.write(0,52); Serial.println("% - Addresse 0 = 52"); // si l'eeprem est renseignée ou pas (52 oui si non non)
EEPROM.write(1,a); Serial.println("% - Addresse 1 = " + String(a)); // nombre maxi de photos (1 x 1 + 2)
EEPROM.write(2,b); Serial.println("% - Addresse 2 = " + String(b)); // nombre maxi de photos (1 x 1 + 2)
EEPROM.write(3,RatioA); Serial.println("% - Addresse 3 = " + String(RatioA)); // Ratio déplacement (3 x 3 + 4)
EEPROM.write(4,RatioB); Serial.println("% - Addresse 4 = " + String(RatioB)); // Ratio déplacement (3 x 3 + 4)
EEPROM.write(5,mode); Serial.println("% - Addresse 5 = 1"); // mode 0 : recule, 1 : avance;
//pour copier le cache mémoire en eeprom ( l'écriture se fait à ce nomment la )
// EEPROM.commit();
EEPROM.end(); // pour liberer la memoire
Serial.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
Serial.println();
}
/* #########################################################################################
* L I T D A N S E E P R O M
* ######################################################################################### */
void litEeprom(){
Serial.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
Serial.println("% Lecture dans l'EEPROM %");
Serial.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
// EEPROM.begin(10);
renseigne = EEPROM.read(0);
Serial.print("% - Adresse 1 = "); Serial.println(renseigne, DEC);
if (renseigne==52){ // 52 c'est mon age
byte nbPhotosMax1 = EEPROM.read(1); Serial.print("% - Adresse 1 = "); Serial.println(nbPhotosMax1, DEC);
byte nbPhotosMax2 = EEPROM.read(1); Serial.print("% - Adresse 2 = "); Serial.println(nbPhotosMax2, DEC);
nbPhotosMax = nbPhotosMax1 * nbPhotosMax2;
byte RatioA = EEPROM.read(3); Serial.print("% - Adresse 3 = "); Serial.println(RatioA, DEC);
byte RatioB = EEPROM.read(4); Serial.print("% - Adresse 4 = "); Serial.println(RatioB, DEC);
positionMax = RatioA * RatioA + RatioB;
Ratio = positionMax / RMax;
byte mode = EEPROM.read(5); Serial.print("% - Adresse 5 = "); Serial.println(mode, DEC);
if (mode == 0) {
sens = 1;
} else {
sens = -1;
}
} else { Serial.println("% L'EEPROM n'a pas été renseignée"); }
EEPROM.end(); // pour liberer la memoire
Serial.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
}
// ##########################################################################################
// Tests fonctionnels + réglages
// ##########################################################################################
void testFonctions() {
int n = 0;
/* lcd.setCursor(0,0);
lcd.print(centre("Test servomoteur"));
setAngle(angle);// on appelle la fonction setAngle définie plus bas
setAngle(-angle);// on appelle la fonction setAngle définie plus bas
lcd.clear();
digitalWrite(ledPret, HIGH);
lcd.setCursor(0,0);
lcd.print(centre("Led Verte Alumee"));
lcd.setCursor(0,1);
lcd.print(centre("Tester depart"));
delay(2000);
while (digitalRead(depart) != HIGH){
lcd.setCursor(0,0);
lcd.print("P : " + metZero(analogRead(A1), 4) + " / " + metZero(analogRead(A2), 4) + " ");
}
digitalWrite(ledPret, LOW);
digitalWrite(ledMesure, HIGH);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("Appuyez sur fin"));
lcd.setCursor(0,1);
lcd.print(centre("de course mini"));
delay(2000);
while (digitalRead(finCourseMini) != HIGH){ }
lcd.clear();
digitalWrite(ledMesure, LOW);
lcd.setCursor(0,0);
lcd.print(centre("Appuyez sur fin"));
lcd.setCursor(0,1);
lcd.print(centre("de course maxi"));
delay(2000);
while (digitalRead(finCourseMaxi) != HIGH){ }
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("Fonctions"));
lcd.setCursor(0,1);
lcd.print(centre("OK"));
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("Choix du mode"));
while (digitalRead(depart) != HIGH){
if (digitalRead(testServomoteur) == HIGH) { return; }
lcd.setCursor(0,1);
Serial.println(analogRead(A2));
String nn=String(analogRead(A2));
Serial.println(nn);
int n = (nn.substring(nn.length()-2,1)).toInt();
Serial.println(n);
if (n%2==0) {
mode=1; lcd.print(centre("en avancant"));
sens = 1;
} else {
mode=0; lcd.print(centre("en reculant"));
sens = -1;
}
}
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("Nb de photo maxi"));
delay(1000);
while (digitalRead(depart) != HIGH){
if (digitalRead(testServomoteur) == HIGH) { return; }
lcd.setCursor(0,1);
n = analogRead(A2);
Serial.println(n);
lcd.print(centre(String(n)) + " ");
}
nbPhotosMax = n;
*/
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Calcul MVT chariot");
n = 0;
Serial.println("Zéro");
while (digitalRead(finCourseMini) != HIGH){
if (digitalRead(testServomoteur) == HIGH) { return; }
int nn = tourne(20, finCourseMaxi);
n = n + abs(nn);
lcd.setCursor(0,1);
lcd.print(centre("zero " + String(n)));
}
n=0;
Serial.println("Mesure");
while (digitalRead(finCourseMaxi) != HIGH){
if (digitalRead(testServomoteur) == HIGH) { return; }
int nn = tourne(-20, finCourseMini);
n = n + abs(nn);
Serial.println(n);
lcd.setCursor(0,1);
lcd.print(centre(" x= " + String(n)));
}
positionMax = n;
lcd.clear();
lcd.setCursor(0,0);
lcd.print(centre("MVT chariot"));
lcd.setCursor(0,1);
lcd.print(centre("Termine"));
delay(10000000);
// il faut écrire ici dans l'eeprom les données
} |
Partager