Schrittmotor ansteuern mit Arduino + A4988

Schrittmotor ansteuern mit Arduino + A4988

Beschaltung

M1
M2
M3
Schrittweite
0
0
0
1 Vollschritt
1
0
0
1/2 Halbschritt
0
1
0
1/4 Viertelschritt
1
1
0
1/8 Achtelschritt
1
1
1
1/16 Sechzehntelschritt

Funktion

Programmcode
				
					#include <Wire.h>
#include "Adafruit_MPR121.h"

Adafruit_MPR121 cap = Adafruit_MPR121();

uint16_t lasttouched = 0;
uint16_t currtouched = 0;

int Index;
int touch;

void setup()
{
  Serial.begin(9600);
  if (!cap.begin(0x5A)) {
    Serial.println("MPR121 not found, check wiring?");
    while (1);
  }
  Serial.println("MPR121 found!");

  pinMode(6, OUTPUT); //Enable
  pinMode(5, OUTPUT); //Step
  pinMode(4, OUTPUT); //Direction

  digitalWrite(6, LOW);
}

void loop()
{
  currtouched = cap.touched();

  for (uint8_t i = 0; i < 12; i++) {
    // it if *is* touched and *wasnt* touched before, alert!
    if ((currtouched & _BV(i)) && !(lasttouched & _BV(i)) ) {
      Serial.print(i); Serial.println(" touched");
      touch = i;
    }
  }
  if (touch == 1) {
    digitalWrite(4, HIGH);
    for (Index = 0; Index < 10; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
  if (touch == 2) {
    digitalWrite(4, LOW);
    for (Index = 0; Index < 10; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
    if (touch == 3) {
    digitalWrite(4, HIGH);
    for (Index = 0; Index < 100; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
  if (touch == 4) {
    digitalWrite(4, LOW);
    for (Index = 0; Index < 100; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
    if (touch == 5) {
    digitalWrite(4, HIGH);
    for (Index = 0; Index < 1000; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
  if (touch == 6) {
    digitalWrite(4, LOW);
    for (Index = 0; Index < 1000; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
    if (touch == 7) {
    digitalWrite(4, HIGH);
    for (Index = 0; Index < 10000; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
  if (touch == 8) {
    digitalWrite(4, LOW);
    for (Index = 0; Index < 10000; Index++)
    {
      digitalWrite(5, HIGH);
      delayMicroseconds(500);
      digitalWrite(5, LOW);
      delayMicroseconds(500);
      touch = 0;
    }
  }
  delay(100);
}
				
			

Jetzt im Onlineshop