|
int ledPin = 13;
int pul_1 = 2; // pulse of moto 1
int dir_1 = 3; //direction of mote 2
int pul_2 =4; //pulse of mote 2
// String inputString = "";
void setup()
{
Serial.begin(9600); //串口波特率为9600
pinMode(pul_1, OUTPUT);
pinMode(dir_1, OUTPUT);
pinMode(pul_2, OUTPUT);
digitalWrite(pul_1,LOW);
digitalWrite(pul_2,LOW);
digitalWrite(dir_1, HIGH);
digitalWrite(ledPin, HIGH);
}
int incomingByte; // for incoming serial data
void step(boolean dir,int steps)
{
digitalWrite(dir_1,dir);
for(int i=0; i < steps; i ++)
{
digitalWrite(pul_1, HIGH);
digitalWrite(pul_2, HIGH);
delayMicroseconds(50);
digitalWrite(pul_1, LOW);
digitalWrite(pul_2, LOW);
delayMicroseconds(50);//50US脉冲
// speed control
delay(100);
}
}
void loop()
{
//int StartSign = inputString.indexOf('*');
if (Serial.available() > 0)
{
// read the incoming byte:
incomingByte = Serial.read();
if(incomingByte=='l') // indicate the arrival of the data.
{
digitalWrite(ledPin, HIGH);
}
else
digitalWrite(ledPin, LOW);
switch (incomingByte) // control the channel according to
{
case '0':
step(true,1000);
break;
case '1':
//step(true,100);
//delay(10000);
step(false,1000);
break;
}
}
}
|
|