Smile and Sad Face Using Arduino and Ultrasonic Sensor
by rayankiwan63 in Circuits > Arduino
671 Views, 2 Favorites, 0 Comments
Smile and Sad Face Using Arduino and Ultrasonic Sensor
ultrasonic smile using arduino
Ultrasonic Smile Using Arduino
In this project , as the distance between ultrasonic sensor and object decreases smiley face get obtained , and sad face as distance increases
Circuit Diagram
components needed
breadboard
Arduino
MAX7219
ultrasonic sensor
wires
Code
#include ledControl.h
const int trigPin = 7;
const int echoPin = 8;
long duration;
int distance;
int DIN = 12; int CS = 11; int CLK = 10;
byte smile[8]= {0x3C,0x42,0x95,0xA1,0xA1,0x95,0x42,0x3C};
byte neutral[8]= {0x3C,0x42,0xA5,0x81,0xBD,0x81,0x42,0x3C};
byte sad[8]= {0x3C,0x42,0xA5,0x91,0x91,0xA5,0x42,0x3C};
LedControl lc=LedControl(DIN,CLK,CS,0);
void setup() { pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(9600); // Starts the serial communication
lc.shutdown(0,false); //The MAX72XX is in power-saving mode on startup
lc.setIntensity(0,15); // Set the brightness to maximum value
lc.clearDisplay(0); // and clear the display
}
void loop() {
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; // Prints the distance on the Serial Monitor Serial.print("Distance: "); Serial.println(distance);
if(distance<6) { printByte(smile); delay(150);
} else { printByte(sad); delay(150); }
}
void printByte(byte character []) { int i = 0; for(i=0;i<8;i++) { lc.setRow(0,i,character[i]); } }