Arduino-组合草图
编程和Arduino的新手。。 我目前正在为我儿子的淘金透视画制作一个项目,增加了电子设备。根据我的理解,它不喜欢阅读delay()代码 请,任何建议都将不胜感激。多谢各位 我试图把两幅草图结合起来; 一个具有随机波动的LED(用作矿井中的灯笼) 另一个是上下拉动绳子的伺服装置(起到升降滑轮的作用) 每个草图单独工作正常。我不确定不使用“delay()”将两者结合起来的正确方法 LED示意图如下Arduino-组合草图,arduino,Arduino,编程和Arduino的新手。。 我目前正在为我儿子的淘金透视画制作一个项目,增加了电子设备。根据我的理解,它不喜欢阅读delay()代码 请,任何建议都将不胜感激。多谢各位 我试图把两幅草图结合起来; 一个具有随机波动的LED(用作矿井中的灯笼) 另一个是上下拉动绳子的伺服装置(起到升降滑轮的作用) 每个草图单独工作正常。我不确定不使用“delay()”将两者结合起来的正确方法 LED示意图如下 /*random fluctuating leds*/ void setup() { //
/*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));
}
/* 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
}
}
伺服扫描示意图如下
/*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));
}
/* 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
}
}
/*扫描
巴拉根仓
此示例代码位于公共域中。
2013年11月8日修改
斯科特·菲茨杰拉德
http://arduino.cc/en/Tutorial/Sweep
*/
#包括
伺服myservo;//创建伺服对象以控制伺服
//大多数电路板上可以创建12个伺服对象
int pos=0;//变量来存储伺服位置
无效设置()
{
myservo.attach(9);//将针脚9上的伺服连接到伺服对象
}
void循环()
{
对于(pos=0;pos=0;pos-=1)//从180度变为0度
{
myservo.write(pos);//告诉伺服转到变量“pos”中的位置
延迟(15);//等待15毫秒,等待伺服到达该位置
}
}
通常,您可以用以下代码替换延迟(1000)
:
unsigned long interval=1000;//我们需要等待的时间
无符号long-previousMillis=0;//millis()返回一个无符号长字符串。
无效设置(){
//...
}
void循环(){
if((无符号长)(毫秒()-以前的毫秒)>=间隔){
previousMillis=millis();
// ...
}
}
因此,您需要类似于以下代码的代码,我还没有测试过这些代码,但希望能为您提供一个起点:
#包括
伺服myservo;//创建伺服对象以控制伺服
//大多数电路板上可以创建12个伺服对象
int pos=0;//变量来存储伺服位置
int pos_方向=0;//电机方向
无符号长间隔=1000;//我们需要等待的时间(模拟写)
无符号long-previousMillis=0;//millis()返回一个无符号长字符串。
无符号长间隔1=15;//我们需要等待的时间(伺服)
无符号long-previousMillis1=0;//millis()返回一个无符号长字符串。
无效设置(){
间隔=随机(100);
pinMode(12,低);
pinMode(11,低);
pinMode(10,低);
myservo.attach(9);//将针脚9上的伺服连接到伺服对象
}
void循环(){
if((无符号长)(毫秒()-以前的毫秒)>=间隔){
previousMillis=millis();
模拟写入(12,随机(120)+135);
模拟写入(11,随机(120)+135);
模拟写入(10,随机(120)+135);
间隔=随机(100);
}
if((无符号长)(millis()-previousMillis1)>=interval1){
先前的毫秒1=毫秒();
如果(位置方向==0){
如果(pos==180)pos_方向=1;
else-pos++;
}
否则如果(位置方向==1){
如果(pos==0)pos_方向=0;
否则位置--;
}
myservo.write(pos);
// ...
}
}
非常感谢用户3704293!现在就去试试这个=)棒极了,它能用!好极了!如果我想在伺服以另一种方式旋转之前暂停,我如何应用它?