#include <Wire.h>
byte dirPin = 8;
byte stepPin = 9;
unsigned long Timer=1;
unsigned long Spide1=4000;
int x;String start=" ";
void setup() {
Serial.begin(9600);
Serial.println("Starting StepperTest");
//digitalWrite(ledPin, LOW);
pinMode(dirPin, OUTPUT);
pinMode(stepPin, OUTPUT);
Wire.begin(4); // join i2c bus with address #4
Wire.onReceive(receiveEvent); // register event
Serial.begin(9600); // start serial for outpu
delay(500);
x=2;
delay(500);
}
void loop() {
if(x==0){x=0;start=" ";}
if(x==1){start="A";}
if(x==2){start="B";}
if(x==3){x=0;start="C";}
if(x==4){x=0;start="D";}
if(x==5){x=0;start="E";}
if(start=="A"){Steppr1();}
if(start=="B"){Steppr2();}
if(start=="C"){start=" ";Spide1=Spide1-20;}
if(start=="D"){start=" ";Spide1=Spide1+20;}
if(start=="E"){Spide1=1800;}
}
void receiveEvent(int howMany)
{
while(1 < Wire.available()) // loop through all but the last
{
char c = Wire.read(); // receive byte as a character
Serial.print(c); // print the character
}
x = Wire.read(); // receive byte as an integer
Serial.println(x); // print the integer
}
void Steppr1(){
for(int i = 0; i < Timer; i++) {
Serial.println("1");
digitalWrite(dirPin,HIGH);
digitalWrite(stepPin,HIGH);
delayMicroseconds(Spide1);
digitalWrite(stepPin,LOW);
delayMicroseconds(Spide1);
}
}
void Steppr2(){
for(int i = 0; i < Timer; i++) {
Serial.println("2");
digitalWrite(dirPin,LOW);
digitalWrite(stepPin,HIGH);
delayMicroseconds(Spide1);
digitalWrite(stepPin,LOW);
delayMicroseconds(Spide1);
}
}