Crazy Cool Color Changing Sensor :) by Parker, Pedro, Jackson
by PHS_Engineering in Circuits > Arduino
54 Views, 1 Favorites, 0 Comments
Crazy Cool Color Changing Sensor :) by Parker, Pedro, Jackson

This project is an amateur level that involves complex code and an interesting circuit
Supplies
- Bread Board
- 24 colored wires
- 4 Red LEDS
- 4 Yellow LEDS
- 4 Green LEDS
- 4 Blue LEDS
- 220 Ohms Resistors
- Ultrasonic Sensor
- Arduino
- USB cable
- Laptop
- Cardboard Box
- Servo Motor
- Clay Stuff
- 15 holes L-Bar
- Stick
- Tape
Collect Materials

We gathered the materials we needed
Planning It Out

We drew out our plan and what we were going to do
Getting the Bread Bored Ready

Putting Everything on the Bread Board

We put all the wires, LEDS, resistors, and ultrasonic sensor on the bread board
Cutting the Box

We drew the line wear we were going to cut it
Finishing Everything

The second to last thing we did was coloring the box, cut out a few holes for the ultrasonic sensor and a hole for the Servo sign, and put the cover on top of our circuit to cover up the wires.
Create Sign, Plus Tape It Onto the Servo

.jpg)
We added a sign that says, "Back Away!" and the sign goes up through the hole when someone gets really close.
Writing and Finishing the Code
The final thing we did was wrote code and made sure it worked
CODE:
#include <Servo.h>
Servo myservo;
const int trig = 5 ;
const int echo = 4 ;
const int BlueOne = 13;
const int BlueTwo = 12;
const int GreenOne = 11;
const int GreenTwo = 10;
const int YellowOne = 9;
const int YellowTwo = 8;
const int RedOne = 7;
const int RedTwo = 6;
long duration;
int distance;
void setup() {
// put your setup code here, to run once:
myservo.attach(3);
pinMode (trig, OUTPUT);
pinMode (echo, INPUT);
pinMode(BlueOne, OUTPUT);
pinMode(BlueTwo, OUTPUT);
pinMode(GreenOne, OUTPUT);
pinMode(GreenTwo, OUTPUT);
pinMode(YellowOne, OUTPUT);
pinMode(YellowTwo, OUTPUT);
pinMode(RedOne, OUTPUT);
pinMode(RedTwo, OUTPUT);
Serial.begin (9600);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig, LOW);
delayMicroseconds(1000);
digitalWrite(trig, HIGH);
delayMicroseconds(1000);
duration = pulseIn(echo, HIGH);
distance = duration * 0.034 / 2;
Serial.println (distance);
if (distance <= 12)
{
myservo.write(90);
digitalWrite (BlueOne, HIGH);
}
else
{
myservo.write(0);
digitalWrite (BlueOne, LOW);
}
if (distance <= 22)
{
digitalWrite (BlueTwo, HIGH);
}
else
{
digitalWrite (BlueTwo, LOW);
}
if (distance <= 33)
{
digitalWrite (GreenOne, HIGH);
}
else
{
digitalWrite (GreenOne, LOW);
}
if (distance <= 44)
{
digitalWrite (GreenTwo, HIGH);
}
else
{
digitalWrite (GreenTwo, LOW);
}
if (distance <= 55)
{
digitalWrite (YellowOne, HIGH);
}
else
{
digitalWrite (YellowOne, LOW);
}
if (distance <= 66)
{
digitalWrite (YellowTwo, HIGH);
}
else
{
digitalWrite (YellowTwo, LOW);
}
if (distance <= 77) // if the distance is less than 77, turn on the blue lights
{
digitalWrite (RedOne, HIGH);
}
else
{
digitalWrite (RedOne, LOW);
}
if (distance <= 88)
{
digitalWrite (RedTwo, HIGH);
}
else
{
digitalWrite (RedTwo, LOW);
}
}