/* This program is for this robot http://www.thingiverse.com/thing:1912377 */ #include <Arduino.h> #include <Servo.h> #include "Oscillator.h" #include <SoftwareSerial.h>
//comment out below line if you don't use HC_SR04 sensor #define __HC_SR04__
void setup() { int i; for (i = 0; i < 8; i++) { servos[i].attach(i + SERVO_START_PIN); } Serial.begin(57600); Serial.println("Shit Type AT commands!"); // The HC-06 defaults to 9600 according to the datasheet. mySerial.begin(9600);
void locomotion(angle_t angles[]) { int i = 0; while (1) { if (angles[i].degree == -1 || angles[i].index == -1 || angles[i].delay == -1) { break; } servos[angles[i].index].SetPosition(angles[i].degree); if (angles[i].delay > 0) delay(angles[i].delay); i++; } //delay(1000); }
#ifdef __HC_SR04__ char detect_obstacle(char action) { char i = action; long cm = distance_measure(); if (cm < MIN_DISTANCE) { i = STAND; } else if (cm >= MIN_DISTANCE && cm <= MAX_DISTANCE) { i = BACKWARD; } return i; }
long distance_measure() { // establish variables for duration of the ping, // and the distance result in inches and centimeters: long duration, cm;
// The PING))) is triggered by a HIGH pulse of 2 or more microseconds. // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
// The same pin is used to read the signal from the PING))): a HIGH // pulse whose duration is the time (in microseconds) from the sending // of the ping to the reception of its echo off of an object. duration = pulseIn(HC_SR04_ECHO_PIN, HIGH);
// The speed of sound is 340 m/s or 29 microseconds per centimeter. // The ping travels out and back, so to find the distance of the // object we take half of the distance travelled. cm = duration / 29 / 2; //Serial.print(cm); //Serial.println("cm"); // delay(100); return cm; } #endif