Arduino - combine sketches

2019-08-13 08:13发布

问题:

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 
  } 
} 

回答1:

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