• tumblr
  • youtube
  • mail

Pirate Servo


#include <Servo.h>
String myName;
int treasureX, treasureY;
int inputX, inputY;
int nearness;
int nearnessX, nearnessY;
int servoAngle = 1;
const int servoMin = 130;
const int servoMax = 170;
Servo myservo;
int readNum (char *prompt) {
int inputNum = 0;
while (inputNum < 1 || inputNum > 9) {
Serial.println ("All numbers must be between 1 and 9 inclusive!");
Serial.println(prompt);
while (Serial.available() == 0) {}
inputNum = Serial.parseInt();
Serial.print("You entered: ");
Serial.print(inputNum);
Serial.print("\n\n");
}
return (inputNum);
}
int findNearness () {
if (treasureX >= inputX)
nearnessX = treasureX – inputX;
else
nearnessX = inputX – treasureX;
if (treasureY >= inputY)
nearnessY = treasureY – inputY;
else
nearnessY = inputY – treasureY;
return (nearnessX + nearnessY);
}
void servoWiggle () {
for (int i = 0; i < 3; i++) {
myservo.write(120);
delay(200);
myservo.write(170);
delay(200);
}
myservo.write(150);
}
// clear serial terminal
void clearTerminal () {
for (int i = 3; i > 0; i–) { // counting backwards from 3 to 0
Serial.print("Clearing screen in ");
Serial.print(i);
Serial.println(" seconds.");
delay (1000);
}
for (int i = 0; i < 100; i++) { // counting backwards from 3 to 0
Serial.println("");
}
}
void setup() {
Serial.begin(9600); // turn on Serial Port MAKE SURE YOU HAVE NO LINE ENDING IN THE TERMINAL!!
myservo.attach(9);
myservo.write(servoMin);
tone(11, 200, 500);
}
void loop() {
int toneFreq;
int toneFreq2;
inputX = 0;
inputY = 0;
servoWiggle();
Serial.println("Hello Hider.");
treasureX = readNum("Please enter the X location to hide the treasure:");
treasureY = readNum("Please enter the Y location to hide the treasure:");
clearTerminal ();
Serial.println("The treasure is hidden!\n");
Serial.println("Seeker. Enter your name:");
while (Serial.available() == 0) {} //Wait for user input
myName = Serial.readString(); //Read user input into myName
Serial.print("Hello ");
Serial.print(myName);
Serial.println(". Good to meet you!\n");
while (!(treasureX == inputX && treasureY == inputY)) {
inputX = readNum("Guess the X location of the treasure:");
inputY = readNum("Guess the Y location of the treasure:");
Serial.print("Your nearness quotient is: ");
Serial.print(findNearness()); // NOTE: a function returning something used as argument to ANOTHER function!!
Serial.println(",\n");
if (inputX == treasureX) {
Serial.print('\n');
Serial.println("Matey X is weeare the treasure lay!");
Serial.print('\n');
tone(11, 2000, 300);
delay(500);
tone(11, 2500, 300);
delay(500);
tone (11, 2000, 300);
delay(500);
}
if (inputY == treasureY) {
Serial.print('\n');
Serial.println("Shiver me Y coordinate! Correctee!");
Serial.print('\n');
tone(11, 4500, 300);
delay(500);
tone(11, 4000, 300);
delay (500);
tone(11, 4500, 300);
delay(500);
}
toneFreq = map (findNearness(), 0, 16, 1000, 40); // map our nearness range to a reverse tone range
toneFreq2 = map (findNearness(), 5, 100, 300, 10);
tone(21, toneFreq, 100); // note the new argument… what does it do?
delay (300);
tone(31, toneFreq, 250);
tone(11, toneFreq2, 100);
servoAngle = map (findNearness(), 0, 16, servoMax, servoMin);
myservo.write(servoAngle);
}
// "you won" jingle
tone(11, 1000, 100);
delay (100);
tone(11, 1500, 205);
delay (10);
tone(11, 3000, 250);
delay (1000);
tone(11, 200, 2000);
delay(200);
tone(11, 1000, 200);
delay (100);
tone(11, 1500, 2000);
delay (10);
tone(11, 3000, 200);
delay (100);
tone(11, 4000, 700);
delay (800);
tone(11, 4000, 500);
delay(780);
tone(11, 1000, 200);
delay (100);
tone(11, 1500, 200);
delay (10);
tone(11, 3000, 200);
delay (700);
tone(11, 14000, 200);
delay(780);
tone(11, 3000, 700);
delay (100);
tone(11, 2000, 205);
delay (500);
tone(11, 1500, 250);
delay (1000);
tone(11, 1000, 390);
delay (100);
tone(11, 1500, 205);
delay (100);
tone(11, 3000, 250);
delay (1000);
Serial.println("You Won Tiddeleee!\n\n\nRestarting:");
servoWiggle();
}

view raw

pirateservo.ino

hosted with ❤ by GitHub


#include <Servo.h>
String myName;
int treasureX, treasureY;
int inputX, inputY;
int nearness;
int nearnessX, nearnessY;
int servoAngle = 1;
const int servoMin = 130;
const int servoMax = 170;
Servo myservo;
int readNum (char *prompt) {
int inputNum = 0;
while (inputNum < 1 || inputNum > 9) {
Serial.println ("All numbers must be between 1 and 9 inclusive!");
Serial.println(prompt);
while (Serial.available() == 0) {}
inputNum = Serial.parseInt();
Serial.print("You entered: ");
Serial.print(inputNum);
Serial.print("\n\n");
}
return (inputNum);
}
int findNearness () {
if (treasureX >= inputX)
nearnessX = treasureX – inputX;
else
nearnessX = inputX – treasureX;
if (treasureY >= inputY)
nearnessY = treasureY – inputY;
else
nearnessY = inputY – treasureY;
return (nearnessX + nearnessY);
}
void servoWiggle () {
for (int i = 0; i < 3; i++) {
myservo.write(120);
delay(200);
myservo.write(170);
delay(200);
}
myservo.write(150);
}
// clear serial terminal
void clearTerminal () {
for (int i = 3; i > 0; i–) { // counting backwards from 3 to 0
Serial.print("Clearing screen in ");
Serial.print(i);
Serial.println(" seconds.");
delay (1000);
}
for (int i = 0; i < 100; i++) { // counting backwards from 3 to 0
Serial.println("");
}
}
void setup() {
Serial.begin(9600); // turn on Serial Port MAKE SURE YOU HAVE NO LINE ENDING IN THE TERMINAL!!
myservo.attach(9);
myservo.write(servoMin);
tone(11, 200, 500);
}
void loop() {
int toneFreq;
int toneFreq2;
inputX = 0;
inputY = 0;
servoWiggle();
Serial.println("Hello Hider.");
treasureX = readNum("Please enter the X location to hide the treasure:");
treasureY = readNum("Please enter the Y location to hide the treasure:");
clearTerminal ();
Serial.println("The treasure is hidden!\n");
Serial.println("Seeker. Enter your name:");
while (Serial.available() == 0) {} //Wait for user input
myName = Serial.readString(); //Read user input into myName
Serial.print("Hello ");
Serial.print(myName);
Serial.println(". Good to meet you!\n");
while (!(treasureX == inputX && treasureY == inputY)) {
inputX = readNum("Guess the X location of the treasure:");
inputY = readNum("Guess the Y location of the treasure:");
Serial.print("Your nearness quotient is: ");
Serial.print(findNearness()); // NOTE: a function returning something used as argument to ANOTHER function!!
Serial.println(",\n");
if (inputX == treasureX) {
Serial.print('\n');
Serial.println("Matey X is weeare the treasure lay!");
Serial.print('\n');
tone(11, 2000, 300);
delay(500);
tone(11, 2500, 300);
delay(500);
tone (11, 2000, 300);
delay(500);
}
if (inputY == treasureY) {
Serial.print('\n');
Serial.println("Shiver me Y coordinate! Correctee!");
Serial.print('\n');
tone(11, 4500, 300);
delay(500);
tone(11, 4000, 300);
delay (500);
tone(11, 4500, 300);
delay(500);
}
toneFreq = map (findNearness(), 0, 16, 1000, 40); // map our nearness range to a reverse tone range
toneFreq2 = map (findNearness(), 5, 100, 300, 10);
tone(21, toneFreq, 100); // note the new argument… what does it do?
delay (300);
tone(31, toneFreq, 250);
tone(11, toneFreq2, 100);
servoAngle = map (findNearness(), 0, 16, servoMax, servoMin);
myservo.write(servoAngle);
}
// "you won" jingle
tone(11, 1000, 100);
delay (100);
tone(11, 1500, 205);
delay (10);
tone(11, 3000, 250);
delay (1000);
tone(11, 200, 2000);
delay(200);
tone(11, 1000, 200);
delay (100);
tone(11, 1500, 2000);
delay (10);
tone(11, 3000, 200);
delay (100);
tone(11, 4000, 700);
delay (800);
tone(11, 4000, 500);
delay(780);
tone(11, 1000, 200);
delay (100);
tone(11, 1500, 200);
delay (10);
tone(11, 3000, 200);
delay (700);
tone(11, 14000, 200);
delay(780);
tone(11, 3000, 700);
delay (100);
tone(11, 2000, 205);
delay (500);
tone(11, 1500, 250);
delay (1000);
tone(11, 1000, 390);
delay (100);
tone(11, 1500, 205);
delay (100);
tone(11, 3000, 250);
delay (1000);
Serial.println("You Won Tiddeleee!\n\n\nRestarting:");
servoWiggle();
}

view raw

pirateservo.ino

hosted with ❤ by GitHub

Leave a reply

Skip to toolbar