Help combining two sets of code

Mercy, please.

I have spent the past two weeks trying to put together two separate codes for a group of kids I'm working with. The kids are younger than I usually work with, and I've gotten older! I cannot get it to work.

The idea is for a cool little 'home security system' with fast flashing blue and red LEDs, using the HC SR04 and a buzzer for added excitement. I couldn't find a code that did exactly what I wanted, and I thought, "Oh, come on! You can DO this!"

I was wrong. If I put the two codes here, is there some kind person who would be willing to do it for me? (I know! The sense of entitlement!) I feel like if I can see it done, I'll understand what I'm doing wrong.

I'll be forever grateful!

//---------------------------------------------------------------------
//  Program:      moving_light_display
//
//  Description:  Flashes four LEDs connected to Arduino pins 2 to 5 
//                in various patterns
//
//  Date:         4 April 2016      Author: W.A. Smith
//                http://startingelectronics.org
//---------------------------------------------------------------------
// change speed of pattern change in milliseconds here
#define SPEED_MS  75

// change LED patterns here
unsigned char led_pattern[] = {
  0x01, 0x02, 0x04, 0x08, 0x04, 0x02, 0x01, 0x00,
  0x06, 0x09, 0x06, 0x09, 0x06, 0x09, 0x06, 0x09,
  0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a, 0x05, 0x0a
};

void setup() {
  // set pin 2 to 5 as outputs
  for (int i = 2; i <= 5; i++) {
    pinMode(i, OUTPUT);
  }
}

void loop() {
  DisplayPattern(led_pattern, sizeof(led_pattern));
  delay(SPEED_MS);
}

void DisplayPattern(unsigned char *pattern, int num_patterns)
{
  static int pattern_num = 0; // keeps count of patterns
  unsigned char mask = 1;     // for testing each bit in pattern

  // do for LEDs on pin 2 to pin 5
  for (int i = 2; i <= 5; i++) {
    // check if bit in pattern is set or not and switch LED accordingly
    if (pattern[pattern_num] & mask) {
      digitalWrite(i, HIGH);
    }
    else {
      digitalWrite(i, LOW);
    }
    mask <<= 1;   // adjust mask for checking next bit in pattern
  }
  pattern_num++;  // move to next pattern for next function call
  // keep pattern within limits of pattern array
  if (pattern_num >= num_patterns) {
    pattern_num = 0;
  }
}

AND


// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
}

Show us your attempt at combining the two sketches.

This is better than I got when I asked 2 days ago. Worked like a charm.

int led = 2;
int led2 = 3;
int led3 = 4;
int led4 = 5;


// defines pins numbers
const int trigPin = 9;
const int echoPin = 10;
const int buzzer = 11;
const int ledPin = 13;

// defines variables
long duration;
int distance;
int safetyDistance;


void setup() {
                 
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT); 
pinMode(led4, OUTPUT);

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
pinMode(buzzer, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600); // Starts the serial communication
}


void loop() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(2);

// Sets the trigPin on HIGH state for 10 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

// Calculating the distance
distance= duration*0.034/2;

safetyDistance = distance;
if (safetyDistance <= 5){
  digitalWrite(buzzer, HIGH);
  digitalWrite(ledPin, HIGH);
}
else{
  digitalWrite(buzzer, LOW);
  digitalWrite(ledPin, LOW);
}

// Prints the distance on the Serial Monitor
Serial.print("Distance: ");
Serial.println(distance);
digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(100);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(100); 
  {digitalWrite(led2, HIGH);
delay(100);
digitalWrite(led2, LOW);
delay(100);}
{digitalWrite(led3, HIGH);
delay(100);
digitalWrite(led3, LOW);
delay(100);}// wait for a second
{digitalWrite(led4, HIGH);}
delay(100);
digitalWrite(led4, LOW);
}


Does it compile ?

What does not work ?

What buzzer are you using ?

IMO, I would use the new ping library.

  {digitalWrite(led2, HIGH); // remove {
{digitalWrite(led3, HIGH); // remove {


delay(100);}// wait for a second // remove }

{digitalWrite(led4, HIGH);} // remove { and }

I switched the LED code to something simpler. Everything complies and works. Unfortunately, it doesn't stop for anything! So the lights and buzzer go off, no matter how far away things are from the HC-SR04.

I'll install that library, thanks. I've only done this a few times. I find it fascinating, but not something I get to try very often. I feel like I'm starting from scratch every time.

Try this:

#include <NewPing.h>

const byte led     = 2;
const byte led2    = 3;
const byte led3    = 4;
const byte led4    = 5;
const byte trigPin = 9;
const byte echoPin = 10;
const byte buzzer  = 11;
const byte ledPin  = 13;

long duration;
int distance;
int safetyDistance;


NewPing sonar(trigPin, echoPin, 200);


//                                       s e t u p ( )
//********************************************^************************************************
void setup()
{
  Serial.begin(9600);

  pinMode(led, OUTPUT);
  pinMode(led2, OUTPUT);
  pinMode(led3, OUTPUT);
  pinMode(led4, OUTPUT);
  pinMode(buzzer, OUTPUT);
  pinMode(ledPin, OUTPUT);

} //END of   setup()


//                                       l o o p ( )
//********************************************^************************************************
void loop()
{
  distance = sonar.ping_cm();;
  Serial.print("cm =  ");
  Serial.println(distance);

  safetyDistance = distance;

  if (safetyDistance <= 5)
  {
    digitalWrite(buzzer, HIGH);
    digitalWrite(ledPin, HIGH);
  }

  else
  {
    digitalWrite(buzzer, LOW);
    digitalWrite(ledPin, LOW);
  }

  digitalWrite(led, HIGH);   
  delay(100);              
  digitalWrite(led, LOW);    
  delay(100);
  digitalWrite(led2, HIGH);
  delay(100);
  digitalWrite(led2, LOW);
  delay(100);
  digitalWrite(led3, HIGH);
  delay(100);
  digitalWrite(led3, LOW);
  delay(100);
  digitalWrite(led4, HIGH);
  delay(100);
  digitalWrite(led4, LOW);

} //END of   loop()

It's still just flashing and buzzing and the serial monitor is reading fine. Maybe it's a wiring issue? I'll have to look at it in the morning light.

I find this equal parts fascinating and frustrating! Thank you for your help

Seems to work fine here.

i.e. 4 LEDs flash a sequence, if distance is <= 5cm buzzer and LED come ON > 5cm buzzer and LED go OFF.

Show us a good image of your wiring.

Your combined code from Post 1 simulation

Your [code from Post 4 simulation oops!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.