极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 10500|回复: 0

做一个两个舵机的小云台,只能减不能增加

[复制链接]
发表于 2015-1-9 19:44:10 | 显示全部楼层 |阅读模式
先上程序……
#include <Servo.h>
Servo servo1;
Servo servo2;
int a,b;
int m,n;
int pos1 = 90;
int pos2 = 90;
void setup() {
  // put your setup code here, to run once:
  servo1.attach(3);
  servo2.attach(4);
  Serial.begin(9600); //波特率为9600bps
  servo1.write(pos1);
  servo2.write(pos2);
}

void loop() {
  // put your main code here, to run repeatedly:
a = analogRead(2);  //521
b = analogRead(5);   //503
digitalWrite(13,LOW);
m = compute(a);
n = compute(b);

pos1 = pos1 + m;
pos2 = pos2 + n;

if(pos1 >= 180)
{
  pos1 = 180;
}
if(pos1 < 0)
{
  pos1 = 0;
}
if(pos2 >= 180)
{
  pos2 = 180;
}
if(pos2 < 0)
{
  pos2 = 0;
}

  servo1.write(pos1);
  servo2.write(pos2);
  
  Serial.print(a);
  Serial.print(" , ");
  Serial.println(b);
  delay(1000);
}
int compute( int input)
{
  int k;
if(input > 600)
{
   k = ( input - 500 )/100;
}
if(input < 400)
{
   k =( input - 500 )/100;
}
else
{
   k = 0;
}
return k;
}


下面的子函数输出值只有负值是怎么回事呀,数据类型错了吗?
回复

使用道具 举报

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

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 11:52 , Processed in 0.048973 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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