如何由电脑通过arduino控制步进电机
1个回答
展开全部
int val=0;
int i=0;
int Pin0 = 8;
int Pin1 = 9
;int Pin2 = 10;
int Pin3 = 11;
int step = 0;
int stepperSpeed = 1;
void setup()
{
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
val=Serial.read();
if (val=='a')
for(i=0;i<100;i++)
{
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
}
if (val=='b')
for(i=100;i>0;i--)
{
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
int i=0;
int Pin0 = 8;
int Pin1 = 9
;int Pin2 = 10;
int Pin3 = 11;
int step = 0;
int stepperSpeed = 1;
void setup()
{
pinMode(Pin0, OUTPUT);
pinMode(Pin1, OUTPUT);
pinMode(Pin2, OUTPUT);
pinMode(Pin3, OUTPUT);
Serial.begin(9600);
}
void loop()
{
val=Serial.read();
if (val=='a')
for(i=0;i<100;i++)
{
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
}
if (val=='b')
for(i=100;i>0;i--)
{
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, LOW); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, HIGH); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, LOW); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, HIGH); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, LOW); delay(stepperSpeed);
digitalWrite(Pin0, HIGH); digitalWrite(Pin1, LOW); digitalWrite(Pin2, LOW); digitalWrite(Pin3, HIGH); delay(stepperSpeed);
亿昇科技
2023-08-25 广告
2023-08-25 广告
电机控制原理是基于电磁感应和电路理论,通过控制电机的电压、电流、频率等参数,实现对电机转速、转矩等物理量的调节和控制。在电机控制中,一般采用PWM(脉冲宽度调制)技术,通过对电机三相电源的斩波,实现电机的调速和转矩控制。同时,电机控制还需要...
点击进入详情页
本回答由亿昇科技提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询