I am good at the building circuits part. But combining the codes?
ARGH.
I’m trying to combine the two I’ll put below. I understand what the code means, I get the fact everything has to be exact. I’ve maintained web pages in the past, coded robots. But I cheat! I find good codes and then cut and paste them. I don’t write code. I do know how to go in and change bits of code to speed things up, or change the way things work. So, I really teach the kids how to build, how the things work, and how to find and verify a code that will work for them and then how to make small changes.
But this time, I couldn’t find a code to do what I wanted. So I was going to combine the codes. And I did. Successfully.
And then I didn’t save it!
And now I can’t replicate it. I need it for this Wednesday. This group is…a lot. 10 boys, mostly 8 and 9 years old. A couple just glance at the Fritzing diagram and build on their own. A couple won’t sit down and won’t listen. It’s interesting. This week we’ll straighten things out, I hope. LOL
If you or ada can offer any help, I’d appreciate it!
Here’s the code://---------------------------------------------------------------------
// 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);
}