噢噢噢 发表于 2018-7-12 12:45:48

请模友们帮我看看

玩四轴有好几年了,就想自己diy个悟,只为装逼,买了3d打印机,打印了全部零件,关键卡在了变形部分,用了arduino   nano和行程开关控制舵机,程序是我网上找的,我感觉程序里定义的引脚和连接图不一样(实物连接图在下面)自己搞了好久还不行,请模友们帮我看看。不胜感激   最下面是程序。




// Designed for Skull Quadcopter
// Rev 1.0


#include <Servo.h>
#include <PinChangeInt.h>




// Define RX variables
#define PIN1 2
#define PIN1dataReg PIND
#define pin1state (PIN1dataReg & (1 << PIN1))
volatile long pulseStart1; // pulse start time measured from millies in ISR for pin 1
volatile long pulseTime1; // pulse width time measured from millies in ISR for pin 1 <-- this is the output we require


Servo myservo;// create servo object to control a servo
int pos = 0;    // variable to store the servo position


// Define Microswitch pins
const int msdown = 5;
const int msup = 4;
int msdownstate = 0;
int msupstate = 0;


// Define Variables for LEDs
int led1 = 6;                  // output pin for LED 1
int led2 = 7;                  // output pin for LED 2
int led3 = 8;                  // output pin for LED 3
int led4 = 9;                  // output pin for LED 4
int led13 = 13;                   // onboard LED indictor incase no others are attached
unsigned long time;            // holds current time for change of state
int flashtime = 400;            // time to flash LEDs in milliseconds
boolean ledon = false;         // remembers start of LED
boolean movedown = true;      // safety feature
boolean moveup = true;      // safety feature


// The below procedure handles the pulse from the RX input
void handlePin1()
{
if(pin1state)
    pulseStart1=micros(); // we got a positive edge
else
    pulseTime1=micros()-pulseStart1; // Negative edge: get pulsewidth
}


// LED Control - very basic, can be expanded.
void turnledoff() {
    digitalWrite(led1, LOW);      
    digitalWrite(led2, LOW);      
    digitalWrite(led3, LOW);   
    digitalWrite(led4, LOW);
    digitalWrite(led13, LOW);   
}
void turnledon() {
    digitalWrite(led1, HIGH);      
    digitalWrite(led2, HIGH);      
    digitalWrite(led3, HIGH);   
    digitalWrite(led4, HIGH);      
    digitalWrite(led13, HIGH);
}
void flashled() {
      // LED Flash
if (millis() - time > flashtime){
      time = millis();
      
      if (ledon){
      turnledoff();
      ledon = false;
      } else {
      turnledon();
      ledon = true;
      }
      
}


}


// Lets set everything up
void setup() {
// initialize serial communication at 115200 bits per second so we can see whats going on:
Serial.begin(115200);

pinMode(PIN1, INPUT);          // Initialize rx input pin
digitalWrite(PIN1, HIGH);      // Set the rx pin to High to start the counter
PCintPort::attachInterrupt(PIN1, &handlePin1, CHANGE); // Attach the interrupt

pinMode(led1, OUTPUT);         // Assign output mode to pin for LED 1
pinMode(led2, OUTPUT);         // Assign output mode to pin for LED 2
pinMode(led3, OUTPUT);         // Assign output mode to pin for LED 3
pinMode(led4, OUTPUT);         // Assign output mode to pin for LED 4
pinMode(led13, OUTPUT);         // Assign output mode to pin for LED 4


pinMode(msup, INPUT);
pinMode(msdown, INPUT);
msupstate = digitalRead(msup);
msdownstate = digitalRead(msdown);
time = millis();               // Save current millis reading (this is a counter that always runs in milliseconds)
turnledon();
}


// Main Program
void loop() {

msupstate = digitalRead(msup);
msdownstate = digitalRead(msdown);
// read the input channels


   if (pulseTime1 > 900 && pulseTime1 < 1300){ //legs down
   myservo.attach(3);
    pos = 0;
    moveup = true;
    if (msupstate == LOW){
      if (movedown){myservo.write(pos);}
    flashtime = 200;
    flashled();
    } else {
      movedown = false;
      myservo.detach();
      turnledon();
    }
   }
    if (pulseTime1 > 1299 && pulseTime1 < 1701){ //no movement
    myservo.detach();
   
    pos = 100;
   }
   
if (pulseTime1 > 1700) { //legs up
myservo.attach(3);
    pos = 180;
    movedown = true;
    if (msdownstate == LOW){
       if (moveup) {myservo.write(pos);}
    flashtime = 200;
    flashled();
    } else {
      moveup = false;
      turnledon();
      myservo.detach();
    }
}
   
}





leigongdianmu 发表于 2018-7-12 14:06:57

顶楼主

txy123456txy 发表于 2018-7-12 14:46:27

不懂帮顶

飞飞机使我快乐 发表于 2018-7-12 16:12:42

顶http://www.moz8.com//mobcent//app/data/phiz/default/04.png

tengvv 发表于 2018-7-12 18:24:52

帮顶

woshixinshouwop 发表于 2018-7-12 19:10:25

像我这种专业白学的,连看都看不懂

注册DIY 发表于 2018-7-12 20:37:46

都是高手赞

lyp315 发表于 2018-7-12 22:49:53

顶一下,高手高手高高手

小丑人生 发表于 2018-7-12 22:59:00

只做上下限位的话,用二极管和常闭开关改舵机就行,没必要这么复杂。

小丑人生 发表于 2018-7-12 23:00:29

表示已经飞起来了。。

噢噢噢 发表于 2018-7-12 23:18:29

小丑人生 发表于 2018-7-12 22:59 static/image/common/back.gif
只做上下限位的话,用二极管和常闭开关改舵机就行,没必要这么复杂。

哥们,具体的连接图有吗?还想控制四个灯

madanzi 发表于 2018-7-13 11:06:58

你找的这个写程序的人自己有没有成功呀,arduino 的舵机只能接9,10口,你的在3口

https://zhidao.baidu.com/question/354346468.html

噢噢噢 发表于 2018-7-13 12:10:29

madanzi 发表于 2018-7-13 11:06 static/image/common/back.gif
你找的这个写程序的人自己有没有成功呀,arduino 的舵机只能接9,10口,你的在3口

https://zhidao.baidu.co ...

别人不知道有没有成功,我也发现程序里定义3脚,回去改下看看

fpvfpv 发表于 2018-7-13 13:30:54

路过。   

我是大肚啊 发表于 2018-7-15 09:09:31

不懂帮顶

咕噜咕噜 发表于 2018-7-17 13:25:14

路过帮顶

莫失 发表于 2018-9-3 14:59:44

虽然看不懂,但是赞一个

我是大肚啊 发表于 2018-9-3 17:55:16

小丑人生 发表于 2018-7-12 23:00 static/image/common/back.gif
表示已经飞起来了。。

这个厉害

光湿恩惠 发表于 2018-9-3 22:33:43

小丑人生 发表于 2018-7-12 23:00 static/image/common/back.gif
表示已经飞起来了。。

厉害了
页: [1]
查看完整版本: 请模友们帮我看看