//Stepper Board Test Code //Kevin Darrah 2017 #include #include LiquidCrystal_I2C lcd(0x20,16,2); const int stepPin = 2;//only works on this pin right now const int dirPin = 6; const int actPin = 4;//not used const float motorAngle = 1.8; const float stepSize = 1;//full=1, half=0.5, quarter=0.25, etc... void stepperRotate(float rotation, float rpm); void setup() { // put your setup code here, to run once: pinMode(stepPin, OUTPUT); pinMode(dirPin, OUTPUT); //pinMode(actPin, OUTPUT); hooked to VCC, so no Arduino control Serial.begin(9600); lcd.init(); lcd.backlight(); delay(5000); lcd.clear(); lcd.home(); lcd.print(" IoT Pet Treat"); lcd.setCursor(0,1); lcd.print(" Dispenser"); } void loop() { if(Serial.available()>0){ String data = Serial.readStringUntil('\n'); if(data=="D"){ lcd.clear(); lcd.home(); lcd.print("Dispensing"); lcd.setCursor(0,1); lcd.print("treats now."); stepperRotate(.5, 30);//rotations, RPM delay(500); stepperRotate(-.2, 30);//rotations, RPM delay(3000); lcd.clear(); lcd.home(); lcd.print(" IoT Pet Treat"); lcd.setCursor(0,1); lcd.print(" Dispenser"); } } delay(1500); } void stepperRotate(float rotation, float rpm) { if (rotation > 0) { digitalWrite(dirPin, HIGH); } else { digitalWrite(dirPin, LOW); rotation = rotation * -1; } float stepsPerRotation = 1050; //now we have the steps per rotation, multiply by the rotations for this command to get total steps float totalSteps = rotation * stepsPerRotation; unsigned long stepPeriodmicroSec = ((60.0000 / (rpm * stepsPerRotation)) * 1E6 / 2.0000) - 5; for (unsigned long i = 0; i < totalSteps; i++) { PORTD |= (1 << 2); delayMicroseconds(stepPeriodmicroSec); PORTD &= ~(1 << 2); delayMicroseconds(stepPeriodmicroSec); } }