RGB 7 Segment Display Distance Sensor
by 807435 in Circuits > Arduino
18 Views, 0 Favorites, 0 Comments
RGB 7 Segment Display Distance Sensor
In this instruct able I will teach you to create an rgb 7 segment display distance sensor, this is a distance sensor paired with a seven segment display and rgb led, the way this will work is depending on the distance the 7 segment display will display a different number and the led will display a different color.
Supplies
You will need these materials:
Arduino Circuit (x1)
Solderless Breadboard (x1)
Distance Sensor (x1)
RGB Led (x1)
330 Ohm Resistor (x3)
7 Segment Display (x1)
Writing the Code
For the first step you will need to write all the code on an Arduino program, all the code needed is here:
int c = 9;
int d = 8;
int e = 7;
int b = 10;
int a = 11;
int f = 12;
int g = 13;
int trig = 4;
int echo = 2;
int red = 6;
int green = 5;
int blue = 3;
void setup() {
// put your setup code here, to run once:
pinMode(a, OUTPUT);
pinMode(b, OUTPUT);
pinMode(c, OUTPUT);
pinMode(d, OUTPUT);
pinMode(e, OUTPUT);
pinMode(f, OUTPUT);
pinMode(g, OUTPUT);
pinMode(trig, OUTPUT);
pinMode(echo, INPUT);
pinMode(redpin, OUTPUT);
pinMode(greenpin, OUTPUT);
pinMode(bluepin, OUTPUT);
}
void setColor(int red, int green, int blue)
{
red = 255-red;
green = 255-green;
blue = 255-blue;
analogWrite (redpin, red);
analogWrite (greenpin, green);
analogWrite (bluepin, blue);
}
void eight(){digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);}
void zero(){ digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, HIGH);
}
void one(){
digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);}
void two () {digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, HIGH);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);}
void three () {digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, LOW);}
void four () { digitalWrite(a, HIGH);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);}
void five () {digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);}
void six () {digitalWrite(a, LOW);
digitalWrite(b, HIGH);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, LOW);
digitalWrite(f, LOW);
digitalWrite(g, LOW);}
void seven () {digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, HIGH);
digitalWrite(e, HIGH);
digitalWrite(f, HIGH);
digitalWrite(g, HIGH);}
void nine () {digitalWrite(a, LOW);
digitalWrite(b, LOW);
digitalWrite(c, LOW);
digitalWrite(d, LOW);
digitalWrite(e, HIGH);
digitalWrite(f, LOW);
digitalWrite(g, LOW);}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(trig, LOW);
delayMicroseconds(2);
digitalWrite(trig, HIGH);
delayMicroseconds(10);
digitalWrite(trig, LOW);
long duration, inches;
duration = pulseIn(echo, HIGH);
inches = duration/74/2;
Serial.println(inches);
If (inches < 5) {
setColor(255, 0, 0);
zero();
}
If (inches < 10,inches > 5) {
setColor(0, 255, 0);
one();
}
If (inches < 15,inches > 10) {
setColor(0, 0, 255);
two();
}
If (inches < 20,inches > 15) {
setColor(255, 0, 255);
three();
}
If (inches < 25,inches > 20) {
setColor(0, 255, 255);
four();
}
If (inches < 30,inches > 25) {
setColor(255, 255, 0);
five();
}
If (inches < 35,inches > 30) {
setColor(80, 0, 80);
six();
}
If (inches < 40,inches > 35) {
setColor(188, 0, 255);
seven();
}
If (inches < 45,inches > 40) {
setColor(255, 171, 0);
eight();
}
If (inches > 45) {
setColor(78, 140, 176);
nine();
}
}
Understanding the Wiring
You will not need to understand how all the components to work in order to build this circuit but you do need to understand the wiring, this image will help you understand it, and I highly suggest you look at it while wiring, I will also explain in more detail to understand wiring
7 Segment Display: connect com to a resistor connected to power, connect all the letters to arduino and you can ignore DP
RGB Led: connect the plus sign to a resistor connected to power and connect the colors to the arduino (sidenote: you have to connect the colors to the PWM ~ pins on the arduino or it wont work)
Distance Sensor: trig and echo both need to be connected to the arduino, vcc needs to be connected to power and gnd needs to be connected to ground
The Wiring
you need to wire this up in order for this circuit to work, the wiring should look like this, remember to look at both the previous step and the code for help
Finishing the Circuit
all you need to do after wiring is open up that code from earlier and the connect it to the arduino and you should have a working circuit