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
|
// ################################################################
// VARIABLES / CONSTANTES
// ################################################################
int position = 0;
int resistance = 0;
long positionMax = 9999.0;
float RMax = 1024.0;
float Ratio = 0.390625;
const int step= 12;
const int dir = 11;
const int servomoteur = 10;
const int depart = 9;
const int ledMesure = 8;
const int ledAttenteAction = 7;
const int finCourseMaxi = 6;
const int finCourseMini = 5;
const int ledPret = 4;
// ################################################################
// INITIALISATION
// ################################################################
void setup() {
Serial.begin(9600);
Ratio = positionMax/RMax;
Serial.println("Départ");
pinMode(step, OUTPUT);
pinMode(dir, OUTPUT);
pinMode(servomoteur, OUTPUT);
pinMode(depart, OUTPUT);
pinMode(finCourseMini, OUTPUT);
pinMode(finCourseMaxi, OUTPUT);
pinMode(ledMesure, OUTPUT);
pinMode(ledAttenteAction, OUTPUT);
pinMode(ledPret, OUTPUT);
position=0;
resistance=0;
int sensorValue = analogRead(A0);
zero(); // <====================
}
// ################################################################
// ################################################################
// BOUCLE PRINCIPALE
// ################################################################
// ################################################################
void loop() {
int start = digitalRead(depart);
if (start == HIGH) {
digitalWrite(ledPret, LOW);
photographies(); // <====================
}
digitalWrite(ledPret, HIGH);
int sensorValue = analogRead(A0);
if (abs(resistance-sensorValue)>2){
positionPlateau(sensorValue); // <====================
}
}
// ################################################################
// CALCUL POSITION DESIREE DU PLATEAU SUITE ACTION POTENTIOMETRE
// ################################################################
void positionPlateau(int R){
int newPosition=(R-resistance)*Ratio;
int p = newPosition-position;
Serial.println("");
tourne(p); // <====================
resistance = R;
position=newPosition;
}
// ################################################################
// DEPLACE CHARIOT DE nb TOURS ET VERIFIS LES FINS DE COURSES
// ################################################################
void tourne(int nb){
boolean plus = HIGH;
int pinFinCourse = finCourseMini;
if (nb>0) {
plus=LOW;
pinFinCourse = finCourseMaxi;
}
nb=abs(nb);
digitalWrite(dir, plus);
int x=0;
while (x < nb){
int fincourse = digitalRead(pinFinCourse);
if (fincourse == HIGH) { x = nb; }
digitalWrite(step, HIGH);
delayMicroseconds(500);
digitalWrite(step, LOW);
delayMicroseconds(500);
fincourse = digitalRead(pinFinCourse);
if (fincourse == HIGH) { x = nb; }
x = x + 1;
}
}
// ################################################################
// CYCLES POUR LES PHOTOS
// ################################################################
void photographies(){
Serial.println("Action Photographier");
digitalWrite(ledMesure, HIGH);
int x = 0;
int nx = position / 100;
while (x<100){
x = x + 1;
tourne(-nx); // <====================
// ici photo, pas de servomoteur à ma disposition ce jour
delay(1000);
digitalWrite(ledMesure, LOW);
Serial.print("Photo ... n°");
Serial.print(x, DEC);
Serial.print(" / ");
Serial.println(100, DEC);
delay(1000);
digitalWrite(ledMesure, HIGH);
}
digitalWrite(ledMesure, LOW);
Serial.println("Action Photographier TERMINE");
zero(); // <====================
}
// ################################################################
// MET AU POINT ZERO LE CHARIOT
// ################################################################
void zero(){
digitalWrite(ledAttenteAction, HIGH);
tourne(-positionMax); // <====================
while (analogRead(A0)>0){
Serial.println("Mettre à la position 0 !");
}
Serial.println("Le plateau est à la position 0");
digitalWrite(ledAttenteAction, LOW);
digitalWrite(ledPret, HIGH);
} |
Partager