1 | /*
|
2 | Driving a DVD drive spindle three-phase motor
|
3 |
|
4 | This code was used for the stroboscope project
|
5 |
|
6 | This example code is in the public domain. Based on several Arduino code samples
|
7 |
|
8 | http://elabz.com/
|
9 |
|
10 | */
|
11 |
|
12 | // constants won't change. They're used here to
|
13 | // set pin numbers:
|
14 | const int buttonPin = 8;// the number of the direction pushbutton pin
|
15 | const int ledPin = 7; // the number of the status LED pin (not the flash LED)
|
16 | const int potPin = 0; // pot controls the RPM speed
|
17 | const int potPinFlash = 1; // pot controls the flash speed
|
18 | const int motorPin1 =9;
|
19 | const int motorPin2 =10;
|
20 | const int motorPin3 =11;
|
21 | const int flashPin =12;
|
22 | const int motorDelay=5; // together with pot controls the RPM
|
23 | const int flashDelay=2; // controls duration of flash
|
24 | const int frames=12; // has to be divisible by 3 in this version
|
25 | const int serialDelay = 2000; //debug only
|
26 | long serialLast =0; //debug only
|
27 | // Variables will change:
|
28 | boolean ledState = false; // the current state of the status LED output pin
|
29 | int buttonState; // the current reading from the direction input pin
|
30 | int potState; // the current reading from the RPM speed potentiometer
|
31 | int potStateFlash; // the current reading from the flash rate potentiometer
|
32 | int lastButtonState = LOW;
|
33 | int debounceDelay = 50; // the debounce time; increase if the output flickers
|
34 | boolean direct = true; // direction true=forward, false=backward
|
35 |
|
36 | /*
|
37 | int pwmSin[] = {127,110,94,78,64,50,37,26,17,10,4,1,0,1,4,10,17,26,37,50,64,78,94,110,127,144,160,176,191,204,217,228,237,244,250,253,254,253,250,244,237,228,217,204,191,176,160,144,127
|
38 | }; // array of PWM duty values for 8-bit timer - sine function
|
39 | */
|
40 |
|
41 | int pwmSin[]={511,444,379,315,256,200,150,106,68,39,17,4,0,4,17,39,68,106,150,200,256,315,379,444,511,578,643,707,767,822,872,916,954,983,1005,1018,1022,1018,1005,983,954,916,872,822,767,707,643,578,511
|
42 | }; // array of PWM duty values for 10-bit timer - sine function
|
43 |
|
44 | int increment;
|
45 | int flashIncrement = 0;
|
46 | int currentFlash=0;
|
47 | int currentStepA=0;
|
48 | int currentStepB=16;
|
49 | int currentStepC=32;
|
50 | // the following variables are long's because the time, measured in miliseconds,
|
51 | // will quickly become a bigger number than can be stored in an int.
|
52 | long lastDebounceTime = 0; // the last time the output pin was toggled
|
53 | long motorDelayActual = 0; // the actual delay, based on pot value and motor delay set above
|
54 | long flashDelayActual = 0;
|
55 | long flashDelayPerCycle = 0;
|
56 | long lastMotorDelayTime = 0;
|
57 | long flashTime = 0; // how long has flash been ON
|
58 | long flashTimeOFF = 0; // how long has flash been OFF
|
59 |
|
60 | void setup() {
|
61 |
|
62 | TCCR1B = TCCR1B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 9 and 10
|
63 | TCCR2B = TCCR2B & 0b11111000 | 0x01; // set PWM frequency @ 31250 Hz for Pins 11 and 3 (3 not used)
|
64 | // ICR1 = 255 ; // 8 bit resolution
|
65 | ICR1 = 1023 ; // 10 bit resolution
|
66 |
|
67 |
|
68 | pinMode(buttonPin, INPUT);
|
69 | pinMode(potPin, INPUT);
|
70 | pinMode(potPinFlash, INPUT);
|
71 | pinMode(ledPin, OUTPUT);
|
72 | pinMode(motorPin1, OUTPUT);
|
73 | pinMode(motorPin2, OUTPUT);
|
74 | pinMode(motorPin3, OUTPUT);
|
75 | pinMode(flashPin, OUTPUT);
|
76 | digitalWrite(flashPin, LOW);
|
77 | }
|
78 |
|
79 | void loop() {
|
80 | // read the state of the switch into a local variable:
|
81 | int reading = digitalRead(buttonPin);
|
82 |
|
83 | // check to see if you just pressed the button
|
84 | // (i.e. the input went from LOW to HIGH), and you've waited
|
85 | // long enough since the last press to ignore any noise:
|
86 |
|
87 | // If the switch changed, due to noise or pressing:
|
88 | if (reading != lastButtonState) {
|
89 | // reset the debouncing timer
|
90 | lastDebounceTime = millis();
|
91 | }
|
92 |
|
93 | if ((millis() - lastDebounceTime) > debounceDelay) {
|
94 | // whatever the reading is at, it's been there for longer
|
95 | // than the debounce delay, so take it as the actual current state:
|
96 | buttonState = reading;
|
97 | direct = !direct;
|
98 | ledState = !ledState;
|
99 | lastButtonState = reading;
|
100 | }
|
101 |
|
102 | // set the LED using the state of the button:
|
103 | digitalWrite(ledPin, ledState);
|
104 |
|
105 | // save the reading. Next time through the loop,
|
106 | // it'll be the lastButtonState:
|
107 |
|
108 | potStateFlash = analogRead(potPinFlash);
|
109 | potState = analogRead(potPin);
|
110 | motorDelayActual = potState * motorDelay / 100;
|
111 | // flashDelayActual = flashDelay+potStateFlash/200; // if we were controlling it with a POT
|
112 | flashDelayActual = flashDelay;
|
113 | move();
|
114 |
|
115 |
|
116 | }
|
117 |
|
118 |
|
119 |
|
120 | void move()
|
121 | {
|
122 | if((millis() - flashTime) > flashDelayActual)
|
123 | {
|
124 | digitalWrite(flashPin, HIGH);
|
125 |
|
126 | }
|
127 |
|
128 |
|
129 | if((millis() - lastMotorDelayTime) > motorDelayActual)
|
130 | { // delay time passed, move one step
|
131 |
|
132 |
|
133 | if (direct==true)
|
134 | {
|
135 | increment = 1;
|
136 | } else {
|
137 | increment = -1;
|
138 | }
|
139 | currentStepA = currentStepA + increment;
|
140 | if(currentStepA > 47) currentStepA = 0;
|
141 | if(currentStepA<0) currentStepA =47;
|
142 |
|
143 | currentStepB = currentStepB + increment;
|
144 | if(currentStepB > 47) currentStepB = 0;
|
145 | if(currentStepB<0) currentStepB =47;
|
146 |
|
147 | currentStepC = currentStepC + increment;
|
148 | if(currentStepC > 47) currentStepC = 0;
|
149 | if(currentStepC<0) currentStepC =47;
|
150 |
|
151 | lastMotorDelayTime = millis();
|
152 | //flashDelayPerCycle = flashDelayPerCycle + flashDelayPerCycle;
|
153 | currentFlash = currentFlash + 1;
|
154 | if(currentFlash>24)
|
155 | {
|
156 |
|
157 | digitalWrite(flashPin, LOW);
|
158 | currentFlash=0;
|
159 | flashTime = millis();
|
160 | flashDelayActual = millis();
|
161 |
|
162 |
|
163 | }
|
164 |
|
165 |
|
166 | }
|
167 |
|
168 | analogWrite(motorPin1, pwmSin[currentStepA]);
|
169 | analogWrite(motorPin2, pwmSin[currentStepB]);
|
170 | analogWrite(motorPin3, pwmSin[currentStepC]);
|
171 |
|
172 | }
|