Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

arduino - How to combine two Adruino Sketches into one

I am doing a project using sensors (LDR & Ultrasonic) and LEDs using the Arduino Software. I have managed to get the light bulb to work, however I would like to know on how to combine two different Arduino programs into one. Attached below are the two different programs

Program 1:

int ldr=A0;//Set A0(Analog Input) for LDR.
int value=0;
void setup() {
Serial.begin(9600);
pinMode(3,OUTPUT);
}

void loop() {
value=analogRead(ldr);//Reads the Value of LDR(light).
Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
Serial.println(value);
if(value<300)
  {
    digitalWrite(3,HIGH);//Makes the LED glow in Dark.
  }
  else
  {
    digitalWrite(3,LOW);//Turns the LED OFF in Light.
  }
}

Program 2:

#define trigPin 13
#define echoPin 12 
#define led 11
void setup() 
{ Serial.begin (9600); 
pinMode(trigPin, OUTPUT); 
pinMode(echoPin, INPUT); 
pinMode(led, OUTPUT); 
}
void loop() 
{ long duration, distance; 
digitalWrite(trigPin, LOW); 
delayMicroseconds(2); 
digitalWrite(trigPin, HIGH); 
delayMicroseconds(10); 
digitalWrite(trigPin, LOW); 
duration = pulseIn(echoPin, HIGH); 
distance = (duration/2) / 29.1; 
if (distance < 10) 
{ digitalWrite(led,HIGH); 
} 
else { 
digitalWrite(led,LOW); 
} 
Serial.print(distance); 
Serial.println(" cm"); 
delay(500); 
}

Thank you!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use a timer and each 500 milliseconds run loop2 The first loop run normaly but the second loop run each 500 milliseconds

#define trigPin 13
#define echoPin 12
#define led 11
#define DELAY 500
int ldr = A0; //Set A0(Analog Input) for LDR.
int value = 0;
long timer = millis(); // a timer for timing 500 milliseconds

void setup() {
  Serial.begin(9600);
  pinMode(3, OUTPUT);
  pinMode(trigPin, OUTPUT);
  pinMode(echoPin, INPUT);
  pinMode(led, OUTPUT);
}

void loop() {

  value = analogRead(ldr); //Reads the Value of LDR(light).
  Serial.println("LDR value is :");//Prints the value of LDR to Serial Monitor.
  Serial.println(value);
  if (value < 300)
  {
    digitalWrite(3, HIGH); //Makes the LED glow in Dark.
  }
  else
  {
    digitalWrite(3, LOW); //Turns the LED OFF in Light.
  }

  if (millis() - timer >= DELAY ) {
    timer = millis();//reset timer
    loop2();
  }
}


void loop2() {
  long duration, distance;
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);
  duration = pulseIn(echoPin, HIGH);
  distance = (duration / 2) / 29.1;
  if (distance < 10)
  { digitalWrite(led, HIGH);
  }
  else {
    digitalWrite(led, LOW);
  }
  Serial.print(distance);
  Serial.println(" cm");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to WuJiGu Developer Q&A Community for programmer and developer-Open, Learning and Share
...