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