I don't like you
Practice scripting for the project 'I like you'
Source Code
/*
For I don't like you. (practice project for the project 'I like you')
version : 0.1
created 07 May 2017
by Douglas Changsoo Park
*/
#include <Servo.h>
#include <NewPing.h>
#define TRIGGER_PIN 12 // Arduino pin tied to trigger pin on the ultrasonic sensor.
#define ECHO_PIN 11 // Arduino pin tied to echo pin on the ultrasonic sensor.
#define MAX_DISTANCE 200 // Maximum distance we want to ping for (in centimeters). Maximum sensor distance is rated at 400-500cm.
NewPing sonar(TRIGGER_PIN, ECHO_PIN, MAX_DISTANCE); // NewPing setup of pins and maximum distance.
Servo myservo;//create servo object to control a servo
int distance;
void setup() {
myservo.attach(9);//attachs the servo on pin 9 to servo object
myservo.write(0);//back to 0 degrees
delay(1000);//wait for a second
Serial.begin(9600); // Open serial monitor at 115200 baud to see ping results.
}
void loop() {
delay(500); //
unsigned int uS = sonar.ping(); // Send ping, get ping time in microseconds (uS).
Serial.print("Ping: ");
Serial.print(uS / US_ROUNDTRIP_CM); // Convert ping time to distance and print result (0 = outside set distance range, no ping echo)
Serial.println("cm");
distance = uS / US_ROUNDTRIP_CM;
delay(500);
// Servo motor moves when the distance is less than 5cm
if (distance < 5 && distance != 0) {
Serial.println("distance : ");
Serial.println(distance);
myservo.write(90);//goes to 45 degrees
delay(500);
myservo.write(-90);//goes to 45 degrees
delay(500);
}
}
'IoT > Arduino' 카테고리의 다른 글
[Arduino] 3rd project. 4X4X4 LED Light Cube (1) | 2017.06.05 |
---|---|
[Arduino Project 2] LED Light stand for 3D Crystal laser cube Ver. 0.4 (0) | 2017.05.08 |
MIT App Inventor - Android App - Mega Millions Jackpot number generator (0) | 2017.01.30 |
Updated Source Code - Automated Mung Bean Sprouts Growing Machine (0) | 2017.01.27 |
Arduino Project 1 - Automated Mung Bean Sprouts Growing Machine (0) | 2017.01.23 |
[Arduino] Motion Sensor and Water Level Sensor (0) | 2017.01.17 |
[Arduino] Joystick, LED Dot Matrix and ADXL335 Module (0) | 2017.01.17 |
[Arduino] Ultrasonic, Keypad and Temperature and Humidity Sensor (0) | 2017.01.15 |
[Arduino] 7 Segment, Servo, LCD and Thermometer (0) | 2017.01.14 |
[Arduino] Using 74HC595 8-bit serial-in/serial or parallel-out shift register (0) | 2017.01.12 |