summaryrefslogtreecommitdiff
path: root/tags/electronics/stepper-motor-driver/1.1/exerciser/exerciser.pde
blob: f8a3bd471e927c40705cc64ed92f83c3f48c1186 (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
#define stepPin 4
#define dirPin 5

void setup()
{
  Serial.begin(9600);
  Serial.println("Starting stepper exerciser.");

  pinMode(stepPin, OUTPUT);
  pinMode(dirPin, OUTPUT);

  digitalWrite(dirPin, HIGH);
  digitalWrite(stepPin, LOW);
}

void loop()
{
    int i, j;
    
    for (i=1650; i>=600; i-=150)
    {
      Serial.print("Speed: ");
      Serial.println(i);
      
      for (j=0; j<2000; j++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(2);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(i);
      }

      delay(500);
      Serial.println("Switching directions.");
      digitalWrite(dirPin, !digitalRead(dirPin));

      for (j=0; j<2000; j++)
      {
        digitalWrite(stepPin, HIGH);
        delayMicroseconds(2);
        digitalWrite(stepPin, LOW);
        delayMicroseconds(i);
      }

      delay(1000);
      Serial.println("Switching directions."); 
      digitalWrite(dirPin, !digitalRead(dirPin));
  }
}