• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Sterowanie silnikiem krokowym określoną częstotliwością
#5
Witam
Dziękuję za podpowiedzi. Znalazłem kilka kodów, jeden chciałem przerobić ale coś mi nie wychodzi. Oryginalny:
Kod:
// testing a stepper motor with a Pololu A4988 driver board or equivalent

// this version uses millis() to manage timing rather than delay()
// and the movement is determined by a pair of momentary push switches
// press one and it turns CW, press the other and it turns CCW

byte directionPin = 9;
byte stepPin = 8;

byte buttonCWpin = 10;
byte buttonCCWpin = 11;

boolean buttonCWpressed = false;
boolean buttonCCWpressed = false;

byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 0;
unsigned long millisBetweenSteps = 25; // milliseconds

void setup() { 

  Serial.begin(9600);
  Serial.println("Starting Stepper Demo with millis()");

  pinMode(directionPin, OUTPUT);
  pinMode(stepPin, OUTPUT);
  pinMode(ledPin, OUTPUT);
  
  pinMode(buttonCWpin, INPUT_PULLUP);
  pinMode(buttonCCWpin, INPUT_PULLUP);
  
}

void loop() { 
  
  curMillis = millis();
  readButtons();
  actOnButtons();
  
}

void readButtons() {
  
  buttonCCWpressed = false;
  buttonCWpressed = false;
  
  if (digitalRead(buttonCWpin) == LOW) {
    buttonCWpressed = true;
  }
  if (digitalRead(buttonCCWpin) == LOW) {
    buttonCCWpressed = true;
  }
}

void actOnButtons() {
  if (buttonCWpressed == true) {
    digitalWrite(directionPin, LOW);
    singleStep();
  }
  if (buttonCCWpressed == true) {
    digitalWrite(directionPin, HIGH);
    singleStep();
  }
}

void singleStep() {
  if (curMillis - prevStepMillis >= millisBetweenSteps) {
    prevStepMillis += millisBetweenSteps;
    digitalWrite(stepPin, HIGH);
    digitalWrite(stepPin, LOW);
  }
}

a mój skrócony :
Kod:
byte stepPin = 4;
byte ledPin = 13;

unsigned long curMillis;
unsigned long prevStepMillis = 10;
unsigned long millisBetweenSteps = 23.66; // milliseconds

void setup() {
 
 Serial.begin(9600);
 Serial.println("Starting Stepper Demo with millis()");

 pinMode(stepPin, OUTPUT);
 pinMode(ledPin, OUTPUT);
   
}

void loop() {
 
 curMillis = millis();

 
}

void singleStep() {
 if (curMillis - prevStepMillis >= millisBetweenSteps) {
   prevStepMillis += millisBetweenSteps;
   digitalWrite(stepPin, HIGH);
   digitalWrite(stepPin, LOW);
 }
}
Z oryginalnego chciałem wyrzucić sterowanie kierunkiem obrotów bo to będzie niezmienne. czy moglibyście sprawdzić co źle zrobiłem?
 
Odpowiedź
  


Wiadomości w tym wątku
RE: Sterowanie silnikiem krokowym określoną częstotliwością - przez slawekw_79 - 08-03-2016, 14:10

Skocz do:


Przeglądający: 1 gości