aboutsummaryrefslogtreecommitdiff
path: root/pilotage_moteur_v2/pilotage_moteur_v2.ino
blob: 29104224c83290fc2f082474fcdf95605cc71420 (plain)
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
String chaine_nbr = "";    // valeur numérique
String chaine_cmd = "";    // valeur commande

int delaylegnth = 4; //durée du delai.

void setup() 
{
  //establish motor direction toggle pins
  pinMode(12, OUTPUT); //CH A -- HIGH = forwards and LOW = backwards???
  pinMode(13, OUTPUT); //CH B -- HIGH = forwards and LOW = backwards???
  
  //establish motor brake pins //etablir les pin du frein du moteur
  pinMode(9, OUTPUT); //brake (disable) CH A
  pinMode(8, OUTPUT); //brake (disable) CH B
  
  Serial.begin(9600);
}

void loop()
{ 
  while (Serial.available() > 0) // si buffer liaison serie non vide // quand on reçoit data
  {
    int inChar = Serial.read(); //lecture des octets rentrants
    if (isDigit(inChar)) 
    {
      // convert the incoming byte to a char and add it to the string: //convertir les octets rentrant en nombre et les ajoute a la chaine de charactere.
      chaine_nbr += (char)inChar; 
    }
    //if (!isDigit(inChar)&&(inChar != 0x0D)&&(inChar != 0x0A)) // si octet PAS nbr ET octet PAS 0x0D (cr) ET octet PAS 0x0A (nl)
    if (!isDigit(inChar)&&(inChar != 0x0D)&&(inChar != 0x0A))
    {
      chaine_cmd += (char)inChar;    // valeur commande
    }
    // if you get a newline, print the string,
    // then the string's value:
    if (inChar == '\r')
    {
      Serial.print("Nom commande : ");
      Serial.print(chaine_cmd);
      Serial.print(" Valeur nombre : ");
      Serial.println(chaine_nbr.toInt()); // valeur après conversion en entier
      
      
      if (chaine_cmd=="cmdH") //commande pour avancer
      {
       for (int cpt=0; cpt<chaine_nbr.toInt(); cpt++)
       {
        actionA(); //effectuer A
        Serial.println("actionA 4pas");
       }
      }
      if (chaine_cmd=="cmdT") //commande pour reculer
      {
       for (int cpt=0; cpt<chaine_nbr.toInt(); cpt++)
       { 
        actionB(); //effectuer B
        Serial.println("actionB 4pas");
       }
      }
      
      // effacement des chaines nbr et cmd
      chaine_nbr = ""; 
      chaine_cmd = "";
    } // fin si fin de ligne
    
  } // fin while
} // fin loop


/////////////////////////////////

void actionA()
{
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 128);   //Moves CH A /Entre 0 et 255
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 128);   //Moves CH B /Entre 0 et 255
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 128);   //Moves CH A /Entre 0 et 255
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 128);   //Moves CH B /Entre 0 et 255
  
  delay(delaylegnth);
}

void actionB()
{
  
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, LOW);   //Sets direction of CH A
  analogWrite(3, 128);     //Moves CH A /Entre 0 et 255
  
  delay(delaylegnth);
    
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW);   //ENABLE CH B
  digitalWrite(13, LOW);   //Sets direction of CH B
  analogWrite(11, 128);   //Moves CH B /Entre 0 et 255
  
  delay(delaylegnth);
  
  digitalWrite(9, LOW);  //ENABLE CH A
  digitalWrite(8, HIGH); //DISABLE CH B
  digitalWrite(12, HIGH);   //Sets direction of CH A
  analogWrite(3, 128);   //Moves CH A /Entre 0 et 255
  
  delay(delaylegnth);
  
  digitalWrite(9, HIGH);  //DISABLE CH A
  digitalWrite(8, LOW); //ENABLE CH B
  digitalWrite(13, HIGH);   //Sets direction of CH B
  analogWrite(11, 128);   //Moves CH B /coefficient Entre 0 et 255 de le tension envoyée.
  
  delay(delaylegnth);
}