Arduino的 - 结合素描 [英] Arduino - combine sketches

查看:226
本文介绍了Arduino的 - 结合素描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的编程和Arduino的..
我目前正在为我儿子的淘金西洋镜添加了电子产品的项目。从我的理解,它不喜欢读延迟()codeS。

请,任何建议都AP preciated。谢谢。

我想两个草图结合起来;
一个具有随机波动的LED(充当灯笼在矿井)
另外,该上下拉动一个字符串(作为电梯滑轮)伺服

每个单独的草图正常工作。我不知道正确的方法不使用两者结合起来延迟()

LED下绘制;

  / *随机波动的LED * /无效设置(){
  //把你设置code在这里,一次运行:
pinMode(12,低);
pinMode(11,低);
pinMode(10,LOW);
}空隙环(){
  //把你的主要code在这里,重复运行:
analogWrite(12,随机(120)+135);
analogWrite(11,随机(120)+135);
analogWrite(10,随机(120)+135);
延迟(随机(100));
}

下面伺服扫描素描;

  / *扫描
 由巴拉甘< HTTP://barraganstudio.com>
 这个例子code是在公共领域。 修改2013年11月8日
 由菲茨杰拉德
 http://arduino.cc/en/Tutorial/Sweep
* /#包括LT&;&Servo.h GT;伺服myservo; //创建伺服对象来控制伺服
                // 12伺服对象可以在大多数主板创建INT POS = 0; //变量来存储伺服位置无效设置()
{
  myservo.attach(9); //附加引脚9伺服伺服对象
}无效循环()
{
  为(位置= 0; POS及下; = 180;位置pos + = 1)//从0度变到180度
  {//在1度的步骤
    myservo.write(POS) //告诉伺服去位置变量POS
    延迟(15); //等待15ms的伺服到达位置
  }
  对于(POS = 180; POS> = 0; POS-= 1)//从180度到0度
  {
    myservo.write(POS) //告诉伺服去位置变量POS
    延迟(15); //等待15ms的伺服到达位置
  }
}


解决方案

在一般可以代替延迟(1000)通过以下code:

无符号长间隔= 1000; //我们需要等待的时间
无符号长previousMillis = 0; //米利斯()返回一个unsigned long。无效设置(){
// ...
}空隙环(){
 如果((无符号长)(米利斯() - previousMillis)GT =间隔){
 previousMillis =米利斯();
 // ...
 }
}

所以,你需要像下面code,我没有测试过,但我希望给你一个起点:

的#include< Servo.h>伺服myservo; //创建伺服对象来控制伺服
                // 12伺服对象可以在大多数主板创建INT POS = 0; //变量来存储伺服位置
INT pos_direction = 0; //电机方向无符号长间隔= 1000; //我们需要等待的时间(analogwrite)
无符号长previousMillis = 0; //米利斯()返回一个unsigned long。无符号长区间1 = 15; //我们需要等待的时间(伺服)
无符号长previousMillis1 = 0; //米利斯()返回一个unsigned long。无效设置(){
   间隔=随机(100);
   pinMode(12,低);
   pinMode(11,低);
   pinMode(10,LOW);
   myservo.attach(9); //附加引脚9伺服伺服对象
}空隙环(){
 如果((无符号长)(米利斯() - previousMillis)GT =间隔){
    previousMillis =米利斯();
    analogWrite(12,随机(120)+135);
    analogWrite(11,随机(120)+135);
    analogWrite(10,随机(120)+135);
    间隔=随机(100);
 } 如果((无符号长)(米利斯() - previousMillis1)GT =区间1){
    previousMillis1 =米利斯();
      如果(pos_direction == 0){
         如果(POS == 180)pos_direction = 1;
         否则POS ++;
      }
 否则如果(pos_direction == 1){
    如果(POS == 0)pos_direction = 0;
    其他pos--;
 }
 myservo.write(POS)
 // ...
 }
}

New to programming and Arduino.. I'm currently making a project for my son's Gold Rush diorama with added electronics. From my understanding, it does not like reading delay() codes.

Please, any suggestions are appreciated. Thank you.

I'm trying to combine two sketches; One that has random fluctuating LEDS (acts as the lanterns in the mine shafts) The other, a servo that pulls a string up and down (acts as an elevator pulley)

Each sketch alone works properly. I'm not sure the proper way to combine the two without using "delay()"

Leds sketch below;

  /*random fluctuating leds*/

void setup() {
  // put your setup code here, to run once:
pinMode(12,LOW);
pinMode(11,LOW);
pinMode(10,LOW);
}

void loop() {
  // put your main code here, to run repeatedly:
analogWrite(12, random(120)+135);
analogWrite(11, random(120)+135);
analogWrite(10, random(120)+135);
delay(random(100));
}

Servo Sweep Sketch below;

/* Sweep
 by BARRAGAN <http://barraganstudio.com> 
 This example code is in the public domain.

 modified 8 Nov 2013
 by Scott Fitzgerald
 http://arduino.cc/en/Tutorial/Sweep
*/ 

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position 

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

void loop() 
{ 
  for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees 
  {                                  // in steps of 1 degree 
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
  for(pos = 180; pos>=0; pos-=1)     // goes from 180 degrees to 0 degrees 
  {                                
    myservo.write(pos);              // tell servo to go to position in variable 'pos' 
    delay(15);                       // waits 15ms for the servo to reach the position 
  } 
} 

解决方案

In general you can replace delay(1000) with the following code:

unsigned long interval=1000;  // the time we need to wait
unsigned long previousMillis=0; // millis() returns an unsigned long.

void setup() {
//...
}

void loop() {
 if ((unsigned long)(millis() - previousMillis) >= interval) {
 previousMillis = millis();
 // ... 
 }
}

So you need something like the following code, that I have not tested, but hopefully gives you a starting point:

#include <Servo.h> 

Servo myservo;  // create servo object to control a servo 
                // twelve servo objects can be created on most boards

int pos = 0;    // variable to store the servo position 
int pos_direction=0; // motor direction

unsigned long interval=1000;  // the time we need to wait (analogwrite)
unsigned long previousMillis=0; // millis() returns an unsigned long.

unsigned long interval1=15;  // the time we need to wait (servo)
unsigned long previousMillis1=0; // millis() returns an unsigned long.

void setup() {
   interval = random(100);
   pinMode(12,LOW);
   pinMode(11,LOW);
   pinMode(10,LOW);
   myservo.attach(9);  // attaches the servo on pin 9 to the servo object 
}

void loop() {
 if ((unsigned long)(millis() - previousMillis) >= interval) {
    previousMillis = millis();
    analogWrite(12, random(120)+135);
    analogWrite(11, random(120)+135);
    analogWrite(10, random(120)+135);
    interval = random(100);
 }

 if ((unsigned long)(millis() - previousMillis1) >= interval1) {
    previousMillis1 = millis();
      if(pos_direction==0){
         if(pos==180) pos_direction=1;
         else pos++;
      }
 else if(pos_direction==1){
    if(pos==0) pos_direction=0;
    else pos--;
 }
 myservo.write(pos); 
 // ... 
 }
}

这篇关于Arduino的 - 结合素描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆