Created
February 16, 2018 16:43
-
-
Save liz-miller/72b231eab2357170c982c91c73f1fa48 to your computer and use it in GitHub Desktop.
Four Steps to writing an Arduino Program Step 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Servo.h> | |
/* | |
*Title: My Awesome Arduino Program | |
*Author: Liz Miller | |
*Date: 02/15/2018 | |
*Version: v1.0 | |
*Purpose: This code shows you how to write an Arduino Program! | |
*/ | |
//STEP 2 - define all of your IO | |
const int led = 6; | |
const int ping = 5; | |
Servo myServo; | |
void setup() { | |
// put your setup code here, to run once: | |
myServo.attach(3); //servo on pin 3 | |
pinMode(led, OUTPUT); | |
pinMode(ping, INPUT); | |
Serial.begin(9600); //to initialize the serial monitor | |
myServo.write(0); //initialize servo to position 0 | |
delay(15); | |
} | |
//turns the given LED on | |
void ledOn(const int led){ | |
digitalWrite(led, HIGH); | |
} | |
//moves the servo 180 degrees | |
void moveServo(){ | |
myServo.write(180); //move servo to opposite side (180-deg) | |
delay(50); //delay to give servo enough time to reach the position | |
} | |
/* | |
* detect distance from ping sensor | |
* Set PING to OUTPUT and pulse for 2 microseconds | |
* Send LOW, HIGH, LOW signal | |
* Set PING as INPUT and read the signal | |
*/ | |
int detectDistance(){ | |
int dur, inches; | |
// send out a signal | |
pinMode(ping, OUTPUT); | |
digitalWrite(ping, LOW); | |
delayMicroseconds(2); | |
digitalWrite(ping, HIGH); | |
delayMicroseconds(2); | |
digitalWrite(ping, LOW); | |
delayMicroseconds(2); | |
//read the signal as a pulse | |
pinMode(ping, INPUT); | |
dur = pulseIn(ping, HIGH); | |
//translate duration as a distance in inches | |
inches = dur/74/2; //74 microseconds per inch (account only for the return, divide by 2); | |
return inches; //return the distance as an integer | |
} | |
void loop() { | |
// put your main code here, to run repeatedly: | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment