freesh
1/2/2014 - 10:43 PM

Arduino Lib für L298 Schrittmotor-Treiber-IC. Vorwärts, Rückwärts, langsames Anfahren und Abbremsen. Noch fehlende Features: Nothalt, Korr

Arduino Lib für L298 Schrittmotor-Treiber-IC. Vorwärts, Rückwärts, langsames Anfahren und Abbremsen. Noch fehlende Features: Nothalt, Korrektur der Geschwindigkeit, Einzelradkorrektur. Ansteuerung mehrerer L298 mit 4 bis 8 einzeln angetriebenen Rädern.

int motor1Front = 3;// defines motor1Front as connected to pin 5
int motor1Back  = 5;// defines motor1Back as connected to pin 6
int motor2Front = 6;// defines motor2Front as connected to pin 7
int motor2Back  = 9;// defines motor2Back as connected to pin 8
void engineStart(unsigned dir);
void engineStop(unsigned dir);

void setup ()
{
  pinMode (motor1Front, OUTPUT);// initialises motor1Front as output
  pinMode (motor1Back, OUTPUT);// initialises motor1Front as output
  pinMode (motor2Front, OUTPUT);// initialises motor1Front as output
  pinMode (motor2Back, OUTPUT);// initialises motor1Front as output
}



void engineStart(unsigned dir) {
  switch(dir) {
    case 'front':
      for (int i=0; i<=255; i++) //accelerate motor from 0 to 255
      {
         analogWrite (motor1Front, i);
         analogWrite (motor2Front, i);
         delay (10);
      }
      break;
    case 'back':
      for (int i=0; i<=255; i++) //accelerate motor from 0 to 255
      {
         analogWrite (motor1Back, i);
         analogWrite (motor2Back, i);
         delay (10);
      }
      break;
  }
}

void engineStop(unsigned dir) {
    switch(dir) {
    case 'front':
      for (int i=255; i>=0; i--) // Decrease speed from 255 to 0
      {
         analogWrite (motor1Front, i);
         analogWrite (motor2Front, i);
         delay (10);
      }
      break;
    case 'back':
      for (int i=255; i>=0; i--) // Decrease speed from 255 to 0
      {
         analogWrite (motor1Back, i);
         analogWrite (motor2Back, i);
         delay (10);
      }
      break;
  }
}

void engineSpeedCorrect(int speedValue) {

}

void loop ()
{
  // usage:
  engineStart('front');
  delay (3000);
  engineStop('front');
  engineStart('back');
  delay (3000);
  engineStop('back');
}