极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 14397|回复: 2

连续舵机两种编程调用方式效果不同呀

[复制链接]
发表于 2012-7-18 15:36:52 | 显示全部楼层 |阅读模式
连续舵机两种编程调用方式效果不同
1.采用舵机用库函数
    myservo.write(val);                  // sets the servo position according to the scaled value
2.采用脉冲高电平
        digitalWrite(9, HIGH);   // sets the LED on
        delayMicroseconds(val);                  // waits for a second
        digitalWrite(9, LOW);    // sets the LED off

效果不同 A.用库函数会连续转动,即代码执行完了还在转,估计有定时时钟调用,而使用第二种方式执行过后,舵机停止
         B。采用第二种方式,修正微调舵机,让输入为16进制的80、81、82(即~1051ms~)舵机停止,转而采用方式1时,舵机参数要在93度时才停止  ,这个不同是个怎么回事呢,两种调用有偏差?

代码附后
1.采用舵机用库函数
#include <Servo.h>

Servo myservo;  // create servo object to control a servo
int val;    // variable to read the value from the analog pin

void setup()
{
  myservo.attach(9);  // attaches the servo on pin 8 to the servo object
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) {
    // get incoming byte:
    val = Serial.read();
    Serial.println(val);
    myservo.write(val);                  // sets the servo position according to the scaled value
    delay(300);                           // waits for the servo to get there
  }
  delay(100);                           // waits for the servo to get there
}
2.采用脉冲高电平
int potpin = 0;  // analog pin used to connect the potentiometer
int val;    // variable to read the value from the analog pin
int inByte = 0;         // incoming serial byte
void setup()
{
  pinMode(9, OUTPUT);
  ;  // attaches the servo on pin 9 to the servo object
  Serial.begin(9600);
}

void loop()
{
  if (Serial.available() > 0) {
    // get incoming byte:
    inByte = Serial.read();
    Serial.println(inByte);
    val = map(inByte, 0, 255, 1000, 2000);     // scale it to use it with the servo (value between 0 and 180)
    Serial.println(val);

    for(int i=0;i<10;i++)
    {
      {
        digitalWrite(9, HIGH);   //
        delayMicroseconds(val);                  // waits for a second
        digitalWrite(9, LOW);    //
        delay(20);              //
      }
    }
  }
  delay(100);                           // waits for the servo to get there
}

回复

使用道具 举报

发表于 2012-7-18 15:56:39 | 显示全部楼层
表示同情中....
回复 支持 反对

使用道具 举报

发表于 2016-5-31 13:36:25 | 显示全部楼层
这个可以怎么用蓝牙控制啊
回复 支持 反对

使用道具 举报

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

本版积分规则 需要先绑定手机号

Archiver|联系我们|极客工坊

GMT+8, 2024-5-20 16:12 , Processed in 0.041298 second(s), 19 queries .

Powered by Discuz! X3.4 Licensed

Copyright © 2001-2021, Tencent Cloud.

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