Competition Code
Copy this code into Arduino.
#include <Motor.h>
#include <Pushbutton.h> #define echoPin A0 #define pingPin 3 #define leftMotorSpeed 9 #define rightMotorSpeed 10 #define leftDirection 4 #define rightDirection 8 #define LED 7 #define buzzerPin 5 #define buttonPin 2 #define leftSensor A1 #define rightSensor A2 #define IREmitter 6 int MOVE_SPEED = 200; //255 is maximum //int TURN_DURATION = 700; //milliseconds //Instantiation of the libraries (this is like making a personal copy with whatever name you want for it. Motor motor; Pushbutton button(buttonPin); //I've named them motor and button because that makes it easy to read and understand the code. //Setting up for the Music player const int songLength = 8; // Length must equal the total number of notes and spaces in notes[] and the total number of digits in beats[] char notes[] = "edcedc C"; // a space represents a rest int beats[] = {4, 4, 4, 4, 4, 4, 3, 1}; // A "1" represents a quarter-note, 2 a half-note, etc. int tempo = 40; // To make the song play faster, decrease this value. //because the song is being used as the delay for your turn, decreasing the tempo will decrease the amoutn of turn void setup() { Serial.begin(9600); pinMode(echoPin, INPUT); pinMode(pingPin, OUTPUT); pinMode(LED, OUTPUT); pinMode(buzzerPin, OUTPUT); pinMode(leftSensor, INPUT); pinMode(rightSensor, INPUT); pinMode(IREmitter, OUTPUT); motor.setupRight(rightMotorSpeed, rightDirection); //the library executes the necessary pinMode commands for these pins as part of this .setupRight function motor.setupLeft(leftMotorSpeed, leftDirection); Serial.println("Waiting for button"); waitForButtonAndCountDown(); } void loop() { digitalWrite(IREmitter, HIGH); int rightInput = analogRead(rightSensor); int leftInput = analogRead(leftSensor); Serial.print("Left/"); Serial.print(leftInput); Serial.print(" - Right/"); Serial.println(rightInput); delay(5); if ( rightInput > 995 || leftInput > 995) { motor.left(-MOVE_SPEED); motor.right(-MOVE_SPEED); delay(500); motor.left(MOVE_SPEED); delay(500); motor.right(MOVE_SPEED); //Backuo and turn aroudn } else { long duration = ping(); delay(500); // we don't want this to run too fast Serial.println(duration); if ( duration > 600 ) { motor.left(-MOVE_SPEED); //delay(TURN_DURATION); playSong(tempo); // I've replaced the delay } else { motor.left(MOVE_SPEED); motor.right(MOVE_SPEED); } } //EXTRA STUFF It the button is pressed we want to stop the motors and wait until it is pressed again if (button.isPressed()) { // if button is pressed, stop and wait for another press to go again motor.left(0); //stop the left motor motor.right(0); //stop the right motor digitalWrite(LED, HIGH); button.waitForRelease(); waitForButtonAndCountDown(); Serial.println("Fight!"); } } long ping() { long duration2; //the text actually uses duration digitalWrite(pingPin, LOW); delayMicroseconds(2); digitalWrite(pingPin, HIGH); delayMicroseconds(10); digitalWrite(pingPin, LOW); duration2 = pulseIn(echoPin, HIGH); return duration2; } //////Extras///// //a function called on startup and after pushing the button to pause the robot void waitForButtonAndCountDown() { digitalWrite(LED, HIGH); button.waitForButton(); digitalWrite(LED, LOW); for (int i = 0; i < 3; i++ ) { Serial.println(i); tone(buzzerPin, i * 200 + 200, 700); delay(1000); } noTone(buzzerPin); } ////////The rest of this sketch is are functions used to play the music void playSong(int tempo) { int i, duration; for (i = 0; i < songLength; i++) { // step through the song arrays duration = beats[i] * tempo; // length of note/rest in ms if (notes[i] == ' ') { // is this a rest? delay(duration); // then pause for a moment } else { // otherwise, play the note tone(buzzerPin, frequency(notes[i]), duration); delay(duration); // wait for tone to finish } delay(tempo / 10); // brief pause between notes } } int frequency(char note) { // This function takes a note character (a-g), and returns the // corresponding frequency in Hz for the tone() function. int i; const int numNotes = 8; // number of notes we're storing // The following arrays hold the note characters and their // corresponding frequencies. The last "C" note is uppercase // to separate it from the first lowercase "c". If you want to // add more notes, you'll need to use unique characters. // For the "char" (character) type, we put single characters // in single quotes. char names[] = { 'c', 'd', 'e', 'f', 'g', 'a', 'b', 'C' }; int frequencies[] = {262, 294, 330, 349, 392, 440, 494, 523}; // Now we'll search through the letters in the array, and if // we find it, we'll return the frequency for that note. for (i = 0; i < numNotes; i++) // Step through the notes { if (names[i] == note) // Is this the one? { return (frequencies[i]); // Yes! Return the frequency } } return (0); // We looked through everything and didn't find it, // but we still need to return a value, so return 0. } |
|