Bonjour il y a quelques temps j'ai fait une machine à couper le fil de fer avec un Arduino UNO R3 un affichage LCD un pas à pas et un Servo motor.

Je ne suis pas programmeur mais j'ai trouvé quelques codes sur le net, après quelques modifications au code la machine fonctionne bien, cependant je dois ajouter un deuxième stepper avec une vitesse variable différente du premier Stepper, en utilisant un potentiomètre sur l'Analog A5.

Tout les paramètre actuel fonctionne bien, donc aucune modif nécessaire seulement la vitesse du carrousel serais variable, La sortie I12 est disponible pour le pul+ du nouveau Stepper.

J'ai copié les codes ci-dessous afin d'avoir votre commentaire.

Merci

Nom : IMG_0479.PNG
Affichages : 173
Taille : 924,2 KoNom : IMG_0472.PNG
Affichages : 177
Taille : 900,4 KoNom : IMG_0473.PNG
Affichages : 200
Taille : 922,8 KoNom : IMG_0477.PNG
Affichages : 180
Taille : 929,1 KoNom : IMG_0481.PNG
Affichages : 172
Taille : 899,1 Ko

--------------------------------------------


Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
#include <Servo.h>
#include <Stepper.h>
#include <LiquidCrystal.h>
 
 
//////////////////////////////////////
// LCD buttons
//////////////////////////////////////
int lcd_key = 0;
int adc_key_in = 0;
#define btnRIGHT 0
#define btnUP 1
#define btnDOWN 2
#define btnLEFT 3
#define btnSELECT 4
#define btnNONE 5
int pinSwitch = 20;
int pinServo = 13;
 
const int stepsPerRevolution = 200;  // change this to fit the number of steps per revolution
const int stepsPerFoot = 485; // steps per stepsPerFoot of cable
 
int calSteps;           // steps counted during calibration
unsigned long startTime;
unsigned long endTime;
unsigned long calTime;
int switchLastState;    // switch change state
 
int stepCount = 0; 
int cutPos = 0;         // servo cutter closed position
int openPos = 140;        // servo cutter open position
int servoDelay = 500;   // servo movement time
int baseDelay = 6100;   // time to measure one stepsPerFoot of cable
int speed1 =1500;        // stepper speed in RPMs
int cutDelay;           // how long it takes to measure entire length
int setLen;             // requested length
int setQty;             // requested quantity
int currQty = 0;        // current quantity
int totalSteps;         // how many steps we need to measure the requested length
int measureLen = 0;     // measured length (float for decimals!)
int measureInch;
int measureFeet;
int cutLen;             // Measured length during cutting operation
 
 
unsigned long lastDelayTime = 0;
unsigned long delayTime = 100;
unsigned long lastInteraction = 0;
unsigned long lastUpdate = 0;
//int buttonState = 0;
 
int cursorPos = 0;
int menuState = 0;        // Menu position, starts at Menu 0
 
Stepper myStepper(stepsPerRevolution,03,11);
Servo myservo ;
 
// Set up lcd global variable
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
/////////////////////////////////////////////
// New way to read buttons
////////////////////////////////////////////
int read_LCD_buttons()
{
  adc_key_in = analogRead(0); // read the value from the buttons
  if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
  if (adc_key_in < 50) return btnRIGHT;
  if (adc_key_in < 250) return btnUP;
  if (adc_key_in < 450) return btnDOWN;
  if (adc_key_in < 650) return btnLEFT;
  if (adc_key_in < 850) return btnSELECT;
 
    return btnNONE; // when all others fail, return this...
}
 
void setup() {
  Serial.begin(9600);
  Serial.println("======== Automated Cable Cutter Online =========");
 
  pinMode(10, OUTPUT);    // LCD backlight control
  digitalWrite(10,  255);
  pinMode(pinSwitch, INPUT_PULLUP);
 
  lcd.begin(16, 2);     // Begin 16 x 2 LCD
  myservo.attach(pinServo);
  myStepper.setSpeed(speed1);
 
  myservo.write(openPos); // make sure we start with cutter open
 
  showMenus();
}
 
void loop(){
  lcd_key = read_LCD_buttons(); // read the buttons
  processButtons();             // do the thing
  if (digitalRead(pinSwitch) != switchLastState) {
    showMenus();
    switchLastState = digitalRead(pinSwitch);   // checks to see if the wire sensor switch has changed
   } 
 
 }
 
void processButtons () { // Reads the buttons and exectues the proper actions based on the menu currently open
 
  if ((millis() - lastDelayTime) > delayTime) {                                 // Only read buttons every 0.3 second
 
    switch (menuState) {
      case 0:
        ///////////////////////////////////////////////////
        // Menu 0: Cut mode
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT: // Select
            if (digitalRead(pinSwitch) == HIGH) {  // if we haven't loaded a cable
              releaseStepper();
            }
            menuState = 1;
            cursorPos = 1;
            showMenus();
            break;
          case btnRIGHT: // Right
            if (digitalRead(pinSwitch) == HIGH) {  // if we haven't loaded a cable
              releaseStepper();
            }
            menuState = 1;
            cursorPos = 1;
            showMenus();
            break;
          case btnUP: //Up
            menuState = 7;
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 2;
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;
            showMenus();
            break;
        }
        break;
      case 1:
        ///////////////////////////////////////////////////
        // Menu 0 Sub 1: Set cuts
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:   // Select
            if (setQty > 0) { // only if we've set Qty and if there's a cable loaded
              cutCables();
            }
            break;
          case btnRIGHT: // Right
            if (cursorPos == 1) {
              cursorPos = 2;
            } else if (cursorPos == 2) {
              cursorPos = 1;
            }
            showMenus();
            break;
          case btnUP: //Up
            if (cursorPos == 1) {
              setLen ++;
            } else if (cursorPos == 2) {
              setQty ++;
            }
            showMenus();
            break;
          case btnDOWN: // Down
            if (cursorPos == 1 && setLen > 0) {
              setLen --;
            } else if (cursorPos == 2 && setQty > 0) {
              setQty --;
            }
            showMenus();
            break;
          case btnLEFT: // Left
            menuState = 0;
            showMenus();
            break;
        }
        break;
      case 2:
        ///////////////////////////////////////////////////
        // Menu 1: Measure Mode
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 5;    // measure menu
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 5;    // measure menu
            showMenus();
            break;
          case btnUP: //Up
            menuState = 0;    // Go back to Top menu
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 7;    // Go to calibrate menu
            showMenus();
            break;
          case btnLEFT: // ESC
 
            break;
        }
        break;
      case 4:
        ///////////////////////////////////////////////////
        // Cut summary
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnUP: //Up
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 1;    // Go back to Cut menu
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    // Go back to Top menu
            showMenus();
            break;
        }
        break;
 
      case 5:
        ///////////////////////////////////////////////////
        // Measurement mode
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            measureCable();
            break;
          case btnRIGHT: // Right
            // do nothing
            break;
          case btnUP: //Up
            // do nothing
            break;
          case btnDOWN: // Down
            // do nothing
            break;
          case btnLEFT: // ESC
            menuState = 2;    // Go back to Measure top menu
            showMenus();
            break;
        }
        break;
      case 6:
        ///////////////////////////////////////////////////
        // Cut summary
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnUP: //Up
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 5;    // Go back to Measure menu
            showMenus();
            break;
        }
        break;
      case 7:
        ///////////////////////////////////////////////////
        // Calibrate
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 8;    // calibrate menu
            showMenus();
            releaseStepper();
            break;
          case btnRIGHT: // Right
            menuState = 0;    //
            showMenus();
            break;
          case btnUP: //Up
            menuState = 2;    //
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 0;    //
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    //
            showMenus();
            break;
        }
        break;
      case 8:
        ///////////////////////////////////////////////////
        // Calibrate
        ///////////////////////////////////////////////////
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            //menuState = 8;    //
            calibrate();
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 0;    //
            showMenus();
            break;
          case btnUP: //Up
            menuState = 2;    //
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 0;    //
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    //
            showMenus();
            break;
        }
        break;
 
      default:
        switch (lcd_key) {
          case btnNONE: // No buttons
            // Do nothing
            break;
          case btnSELECT:
            menuState = 0;    //
            showMenus();
            break;
          case btnRIGHT: // Right
            menuState = 0;    //
            showMenus();
            break;
          case btnUP: //Up
            menuState = 0;    //
            showMenus();
            break;
          case btnDOWN: // Down
            menuState = 0;    //
            showMenus();
            break;
          case btnLEFT: // ESC
            menuState = 0;    //
            showMenus();
            break;
        }
        break;
    }
    lastDelayTime = millis();
  }
}
 
 
 
void showMenus() {
  // This function holds all the menu designs. Each case is a different menu/submenu. Cases are set by the Enter button incrementing the menuState variable.
  switch (menuState) {
    case 0:
      ///////////////////////////////////////////////////
      // Menu 0: Cut Mode
      ///////////////////////////////////////////////////
 
      lcd.noCursor(); // Don't show the cursor
      lcd.setCursor(0, 0);
      lcd.write(" COUPE BROCHES    ");
      lcd.setCursor(0, 1);
      lcd.write(" APPUYER SELECT  ");
      break;
 
    case 1:
      ///////////////////////////////////////////////////
      // Menu 0, Submenu 1: Cut Setup
      ///////////////////////////////////////////////////
      lcd.clear();
      lcd.cursor(); // Show the cursor
      lcd.setCursor(0, 0);
      lcd.write("LONG.(POUCES) ");
      lcd.setCursor(13, 0);
      lcd.print(setLen);
      lcd.setCursor(0, 1);
      lcd.write("QTY: ");
      lcd.print(setQty);
      if (digitalRead(pinSwitch) == HIGH) { // if we've not loaded a cable
        lcd.setCursor(10, 1);
        lcd.print("CHARGE");
      } else {
        lcd.setCursor(10, 1);
        lcd.print("PRET");
      }
 
      if (cursorPos == 1) {
        lcd.setCursor(13, 0);
      } else if (cursorPos == 2) {
        lcd.setCursor(5, 1);
      }
      break;
 
    case 2:
      ///////////////////////////////////////////////////
      // Menu 1: Measure Mode
      ///////////////////////////////////////////////////
      lcd.noCursor(); // Don't show the cursor
      lcd.setCursor(0, 0);
      lcd.write("  Measure Mode  ");
      lcd.setCursor(0, 1);
      lcd.write("  Press Select  ");
 
      break;
 
    case 3:
      //////////////////////////////////////////////////
      // Cutting in Progress
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 1);
      lcd.print("QTY.:");
      lcd.setCursor(8, 1);
      lcd.print(currQty);
      break;
 
    case 4:
      //////////////////////////////////////////////////
      // Cut summary
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print(" JOB FINI ");
      lcd.setCursor(0, 1);
      lcd.print("QTY. ");
      lcd.setCursor(4, 1);
      lcd.print(currQty);
      lcd.print(" @ ");
      lcd.print(setLen);
      lcd.print(" POUCES ");
      break;
    case 5:
      //////////////////////////////////////////////////
      // Measure mode
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      if (digitalRead(pinSwitch) == HIGH) {
        lcd.print(" CHARGER FILS ");
      } else {
        lcd.print("      PRET     ");
      }
 
      lcd.setCursor(0, 1);
      lcd.print("  APPUYER SELECT  ");
      break;
    case 6:
      //////////////////////////////////////////////////
      // Measure summary
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("MESURE COMPLETER");
      lcd.setCursor(0, 1);
      lcd.print("Len: ");
      lcd.print(measureFeet);
      lcd.print("' ");
      lcd.print(measureInch);
      lcd.print("\"");
      break;
 
    case 7:
      //////////////////////////////////////////////////
      // Calibrate
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("MODE CALIBRATION");
      lcd.setCursor(0, 1);
      lcd.print(" APPUYER SELECT  ");
      break;
 
    case 8:
      //////////////////////////////////////////////////
      // Calibrate
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("CHARGER EXACT 1'");
      lcd.setCursor(0, 1);
      lcd.print("DE FILS.");
      break;
 
    case 9:
      //////////////////////////////////////////////////
      // Calibrate
      //////////////////////////////////////////////////
      lcd.clear();
      lcd.noCursor();
      lcd.setCursor(0, 0);
      lcd.print("CAL. COMPLETER!");
      lcd.setCursor(0, 1);
      lcd.print(calSteps);
      lcd.print(" STEPS/POUCE");
      break;
  }
}
 
void calibrate() {
  calTime = 0;
  calSteps = 0;           // reset
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("CALIBRATION...");
 
 
  while (digitalRead(pinSwitch) == LOW) {
    myStepper.step(1);
    calSteps ++;
    lcd.setCursor(0, 1);
    lcd.print(calSteps);
  }
 
  menuState = 9;        // Calibrate summary menu
  showMenus();
  releaseStepper();
}
 
void cutCables() {
  cutLen = 0;                     // reset measured length
  currQty = 0;                    // reset current quantity
 
  if (digitalRead(pinSwitch) == HIGH) {    // switch reads HIGH if we haven't loaded a wire
    lcd.setCursor(0, 0);
    lcd.print("CHARGER FILS!");
  } else {
    Serial.println("-_-_-_ Cutting subroutine activated _-_-_-");
    Serial.print("~ Cutting ");
    Serial.print(setQty);
    Serial.print(" pieces with length ");
    Serial.print(setLen);
    Serial.println(" feet.");
    Serial.print("~ Current quantity cut: ");
    Serial.println(currQty);
 
    //totalSteps = (setLen * stepsPerFoot);     // total steps per cut
    menuState = 3;                  // Cutting in progress menu
    showMenus();
 
    while (currQty < setQty) {
      myStepper.step(-20);
      delay(100);
      myStepper.step(20);             // unstick the wire from the cutter
      cutLen = 0;                     // reset measured length
      Serial.println("~~~ MESURAGES... ~~~");
      lcd.setCursor(0, 0);
      lcd.print("MESURAGES...    ");
      while (cutLen < setLen) {
        myStepper.step(stepsPerFoot);
        cutLen ++;
        lcd.setCursor(13, 0);
        lcd.print(cutLen);
      }
      Serial.println("~~~ CUTTER OUVERT ~~~");
      lcd.setCursor(0, 0);
      lcd.print(" CUTTER FERMER ");
      myservo.write(cutPos);
      delay(servoDelay);
      Serial.println("~~~ CUTTER FERMER ~~~");
      lcd.setCursor(0, 0);
      lcd.print(" CUTTER OUVERT ");
      myservo.write(openPos);
      delay(servoDelay);
      Serial.println("~~~ COUPE FINI ~~~");
      currQty ++;
      showMenus();
    }
 
    menuState = 4;     // Cut summary
    showMenus();
  }
}
 
void measureCable() {
  int stepsPerInch = (stepsPerFoot / 12);
  measureLen = 0;
 
  lcd.clear();
  lcd.noCursor();
  lcd.setCursor(0, 0);
  lcd.print("  MESURAGES...  ");
  lcd.setCursor(0, 1);
  lcd.print("Len: ");
  lcd.print(measureLen);
  lcd.print(" in.");
  while (digitalRead(pinSwitch) == LOW) {
    myStepper.step(stepsPerInch);
    measureLen = measureLen + 1;
    lcd.setCursor(4, 1);
    lcd.print(measureLen);
  }
 
  //some math
 
  measureInch = measureLen % 12;
  measureFeet = measureLen / 12;
 
  menuState = 6;  // Summary menu
  showMenus();
 
}
void releaseStepper() {
 
  digitalWrite(03, LOW);
  digitalWrite(11, LOW); 
}