This shows you the differences between two versions of the page.
| Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
| et:arduino:motors:project4 [2017/01/05 12:03] – kaupo.raid | et:arduino:motors:project4 [2020/07/20 09:00] (current) – external edit 127.0.0.1 | ||
|---|---|---|---|
| Line 2: | Line 2: | ||
| - | === Näide 1 === | + | === Näide 1 auto tahhomeeter |
| + | \\ | ||
| <code c> | <code c> | ||
| + | |||
| + | |||
| /* Description | /* Description | ||
| Line 22: | Line 24: | ||
| AccelStepper stepper1(8, motorPin1, motorPin3, motorPin2, motorPin4); | AccelStepper stepper1(8, motorPin1, motorPin3, motorPin2, motorPin4); | ||
| - | int posArray[] = {0, 800, 0, 800, 0, 800, 1500, 3000, 2000, 1000, 3000, 0, 400, 0, 3000, 0}; | + | int posArray[] = {0, 800, 0, 800, 0, 800, 1500, 3000, 2000, 1000, 3000}; |
| - | int accArray[] = {1000, 3000, 1000, 3000, 1000, 3000, 2000, 2000, 500, 1000, 3000, 1000, 3000, 1000, 10000, 1000}; | + | int accArray[] = {1000, 3000, 1000, 3000, 1000, 3000, 2000, 2000, 1000, 1000, 3000}; |
| void setup() | void setup() | ||
| Line 44: | Line 46: | ||
| } | } | ||
| + | </ | ||
| + | |||
| + | |||
| + | \\ | ||
| + | === Näide 2 Ajavõtusüsteem (0-60s) === | ||
| + | \\ | ||
| + | |||
| + | <code c> | ||
| + | /* Description | ||
| + | |||
| + | |||
| + | */ | ||
| + | |||
| + | #include < | ||
| + | |||
| + | #define motorPin1 | ||
| + | #define motorPin2 | ||
| + | #define motorPin3 | ||
| + | #define motorPin4 | ||
| + | // Red - 28BYJ48 pin 5 (VCC) | ||
| + | |||
| + | // NOTE: The sequence 1-3-2-4 is required for proper sequencing of 28BYJ48 | ||
| + | AccelStepper stepper1(8, motorPin1, motorPin3, motorPin2, motorPin4); | ||
| + | |||
| + | unsigned long startTime, timeSpent = 0; | ||
| + | |||
| + | void setup() | ||
| + | { | ||
| + | Serial.begin(9600); | ||
| + | |||
| + | pinMode(A0, INPUT); | ||
| + | digitalWrite(A0, | ||
| + | pinMode(5, | ||
| + | digitalWrite(5, | ||
| + | | ||
| + | stepper1.setMaxSpeed(1500); | ||
| + | stepper1.setAcceleration(1000.0); | ||
| + | stepper1.setSpeed(68.266666667); | ||
| + | stepper1.stop(); | ||
| + | } | ||
| + | |||
| + | void loop(){ | ||
| + | | ||
| + | if(digitalRead(A0) == LOW){ | ||
| + | beep(); | ||
| + | stepper1.moveTo(4096); | ||
| + | stepper1.setSpeed(68.266666667); | ||
| + | startTime = millis(); | ||
| + | while(stepper1.currentPosition() < 4096){ | ||
| + | if(digitalRead(A0) == LOW && (millis()-startTime) > 1000){ | ||
| + | stepper1.stop(); | ||
| + | timeSpent = millis() - startTime; | ||
| + | beep(); | ||
| + | break; | ||
| + | } | ||
| + | stepper1.runSpeed(); | ||
| + | } | ||
| + | if(stepper1.currentPosition() >= 4096){beep(); | ||
| + | Serial.print(" | ||
| + | Serial.print(timeSpent); | ||
| + | timeSpent = 0; | ||
| + | stepper1.moveTo(0); | ||
| + | stepper1.setSpeed(1500); | ||
| + | delay(500); | ||
| + | while(digitalRead(A0) == HIGH && stepper1.currentPosition() != 0); | ||
| + | beep(); | ||
| + | while(stepper1.currentPosition() > 0)stepper1.run(); | ||
| + | stepper1.stop(); | ||
| + | delay(500); | ||
| + | } | ||
| + | } | ||
| + | |||
| + | void beep(){ | ||
| + | | ||
| + | //tone(5, 500, 100); | ||
| + | digitalWrite(5, | ||
| + | delay(50); | ||
| + | digitalWrite(5, | ||
| + | } | ||
| </ | </ | ||
| + | |||