极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10309|回复: 0

求助啊!疯掉了,电机转速不稳定何解?

[复制链接]
发表于 2013-5-12 11:23:33 | 显示全部楼层 |阅读模式
电机能控制启动、停止、正反转及转速,但是转动过程中总是隔一小段时间跳动一次(电机停止然后立马又恢复转速),这是什么问题啊?求大神相助!!
程序如下:
#include <AFMotor.h>

byte val[6];//上位机发出的六个电机控制字节的暂存数组
AF_DCMotor motor(1, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           
}

void loop()
{

delay(10);
  if (Serial.available()>0) //如果读缓冲区的字节没被读取完
  {
   for(int i=0; i<6; i++)//for循环次数与上位机下传的字节数一致
   {val[i] = Serial.read();}  
  }
  if(val[0]==0x11)           //电机启动
  {
     if(val[1]==0xAA)        //电机正转
     {
       motor.run(FORWARD);
         motor.setSpeed(int( val[2])); //电机 调速
      // motor.setSpeed(int(val[2])); //电机 调速
       delay(30);
     }
    else if(val[1]==0xBB)     //电机反转
    {
      motor.run(BACKWARD);
       motor.setSpeed(int(val[2])); //电机 调速
       delay(30);
    }
  }
  else
  {
    motor.run(RELEASE);  //电机 停   
    delay(30);
  }
}
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-7 20:11 , Processed in 0.049861 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表