极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 15538|回复: 0

关于用CLK输入控制电机启停的问题

[复制链接]
发表于 2016-11-7 13:39:05 | 显示全部楼层 |阅读模式
在淘宝上买了带驱动的电机,需要用CLK输入控制,进行调速,用PWM时,总是调不了速,用上方波之后,发现控制不了电机运行一段时间,自己停下,不知得用什么语法,求教,附上两种程序1:CLK输入,2:PWM输入


#include <IRremote.h>

int motorPin1 = 9; //电机输出         
int RECV_PIN = 8;   //红外头   
int jdq=7;//通过继电器控制电机启停
int a=0;//标示位

IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(9600);
  pinMode(motorPin1,OUTPUT);
  pinMode(jdq,OUTPUT);   
  irrecv.enableIRIn();  
     
}
void loop()
{
  if (irrecv.decode(&results))
  {
  switch (results.value)
  {
    case 0xFFA25D:a=1;      
                  digitalWrite(jdq,HIGH);                                 
                  break;                  
    case 0xFF629D:a=0;
                  digitalWrite(jdq,LOW);                           
                  break;   
  }
  irrecv.resume();  
  }
  motor();
}

void motor()     //控制电机启停,速度
{
  if (a !=0)
  {
    digitalWrite(motorPin1, HIGH);
    delay(0.1);
    digitalWrite(motorPin1, LOW);
    delay(0.1);
  }
}


#include <IRremote.h>

int motorPin1 = 9;        
int RECV_PIN = 8;        
int pwm=5;
int T=5000;
int jdq=7;
IRrecv irrecv(RECV_PIN);
decode_results results;
void setup()
{
  Serial.begin(9600);
  pinMode(motorPin1,OUTPUT);
  pinMode(jdq,OUTPUT);   
  irrecv.enableIRIn();  
     
}
void loop()
{
  if (irrecv.decode(&results))
  {
  switch (results.value)
  {
   case 0xFFA25D :digitalWrite(jdq,HIGH);               
                  for(int i=255;i>=pwm;i--){analogWrite(motorPin1,i);delay(5);}              
                  analogWrite(motorPin1,pwm);
                  delay(T);
                  for(int j=pwm;j<=235;j++){analogWrite(motorPin1,j);delay(5);}  
                  analogWrite(motorPin1,255);                                 
                  digitalWrite(jdq,LOW);
                  break;
  }
  irrecv.resume();  
  }
}
回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-14 20:58 , Processed in 0.033622 second(s), 17 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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