Created
January 18, 2017 23:38
-
-
Save jchuahtacc/7ccfeb813e55b17cd0cbab2890bae2ab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // 2_sensors.ino | |
| // | |
| // Example robotics code for CODE @ TACC | |
| // Summer 2016 robotics curriculum, available at: | |
| // | |
| // https://github.com/CODE-at-TACC/summer-2016 | |
| // | |
| // Copyright 2016, Texas Advanced Computing Center | |
| // GNU GPLv3 License | |
| /****************************************** | |
| Included libraries | |
| ******************************************/ | |
| #include "codetacc-robotics/codetacc-robotics.h" | |
| /****************************************** | |
| Variables | |
| ******************************************/ | |
| Adafruit_MotorShield shield = Adafruit_MotorShield(); | |
| Adafruit_DCMotor *leftMotor = shield.getMotor(1); | |
| Adafruit_DCMotor *rightMotor = shield.getMotor(2); | |
| UltrasonicSensor ultrasonic = UltrasonicSensor(2, 4); | |
| double distance = 0; | |
| /****************************************** | |
| Custom functions | |
| ******************************************/ | |
| int forward(String params = "") { | |
| leftMotor->run(FORWARD); | |
| rightMotor->run(FORWARD); | |
| return 1; | |
| } | |
| int left(String params = "") { | |
| leftMotor->run(BACKWARD); | |
| rightMotor->run(FORWARD); | |
| return 1; | |
| } | |
| int right(String params = "") { | |
| leftMotor->run(FORWARD); | |
| rightMotor->run(BACKWARD); | |
| return 1; | |
| } | |
| int stop(String params = "") { | |
| leftMotor->run(RELEASE); | |
| rightMotor->run(RELEASE); | |
| return 1; | |
| } | |
| /****************************************** | |
| void setup() | |
| Runs once upon startup | |
| ******************************************/ | |
| void setup() { | |
| shield.begin(); | |
| leftMotor->setSpeed(150); | |
| rightMotor->setSpeed(150); | |
| Particle.variable("distance", distance); | |
| } | |
| /****************************************** | |
| void loop() | |
| Runs forever | |
| ******************************************/ | |
| void loop() { | |
| forward(); | |
| delay(1000); | |
| stop(); | |
| delay(1000); | |
| left(); | |
| delay(1000); | |
| right(); | |
| delay(1000); | |
| distance = ultrasonic.readCm(); | |
| delay(1000); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment