DREIDEL WITH SUMO ROBOT
You will already need to own an assembled sumo robot but can use the code below to play the game and hear the Dredyl Song. Be sure to print out the dreidel paper for competition.
What will you learn about?
- Speed of Sound
Rotational velocity and Speed
Timing and intervals
Circumference
Steps:
This activity assumes you have an already assembled Sumo Robot League kit. If you do not, please assemble following the instructions included with your kit.
Print out the Dreidel Challenge Sheet.
https://theclubhouse-augusta.slack.com/files/U0C4Z6ASW/F89QC5K4H/dreidel_with_sumo_robot.mp4
See below: This is your code to copy and paste into your IDE (select the appropriate pin assignments for you board)
//Written by William J. Ashby, PhD of the Sumo Robot League
//special thanks to Programmer: Caleb A. Jones for the Dredyl Song
// use these if your circuitboard is red
#define pingPin 3
#define echoPin A0
#define leftSensor A1
#define rightSensor A2
#define buttonPin 2
#define leftDirection 4
#define buzzerPin 5
#define IREmitter 6
#define LED 7
#define rightDirection 8
#define leftMotorSpeed 9
#define rightMotorSpeed 10
//*/
/*// use these if your circuitboard is black
#define pingPin 10
#define echoPin A0
#define leftSensor A1
#define rightSensor A2
#define rearSensor A3
#define buttonPin 2
#define buzzerPin 3
#define IREmitter 4
#define leftMotorSpeed 5
#define rightMotorSpeed 6
#define leftDirection 7
#define rightDirection 8
#define LED 13
//*/
bool leftIsFlipped = false; //If the left motor goes backwards when it is supposed to go forwards set to true
bool rightIsFlipped = true;
int lBlack = 0; int rBlack = 0;
int lWhite = 0; int rWhite = 0;
/*****Setting Up for the Dreidel Song*********/
const float halfBeat = 250;
const int cN = 262;
const int dN = 294;
const int eN = 339;
const int fN = 349;
const int gN = 392;
char melody[] = {'g', 'c', 'c', 'd', 'd', 'e', 'c', 'e', 'g', 'g', 'f', 'e', 'd', 'd', 'd', 'd', 'e', 'e', 'f', 'd', 'd', 'g', 'f', 'e', 'd', 'c', 'e', 'g', 'e', 'g', 'e', 'g', 'e', 'e', 'g', 'g', 'f', 'e', 'd', 'd', 'f', 'd', 'f', 'd', 'f', 'd', 'd', 'g', 'f', 'e', 'd', 'c'};
int hfBeat[] = {1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 3, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 3, 3, 3};
int totalNotes = 52;
int tone_;
int beat;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
pinMode(pingPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(leftDirection, OUTPUT);
pinMode(rightDirection, OUTPUT);
pinMode(leftMotorSpeed, OUTPUT);
pinMode(rightMotorSpeed, OUTPUT);
pinMode(leftSensor, INPUT);
pinMode(rightSensor, INPUT);
pinMode(IREmitter, OUTPUT);
pinMode(buttonPin, INPUT_PULLUP);
pinMode(buzzerPin, OUTPUT);
//calibrateBlackWhite();
}
void loop() {
//wait for the button to be pressed
delay(100);
Serial.println("beginning of loop");
if (digitalRead(buttonPin) == 0) {
Serial.println("in the if");
//Start rotating at a random speed
int randomSpeed = random(70, 255);
int plusOrMinus = random(-1, 1);
Serial.print(plusOrMinus);
if (plusOrMinus == 0) {
randomSpeed = -randomSpeed;
}
setSpeeed(randomSpeed, 0);
setSpeeed(-randomSpeed, 1);
int randomStart = random(15, 40);
for (int ii = randomStart; ii < 53; ii++)
{
delay(halfBeat * hfBeat[ii-1]);
tone_ = melody[ii];
if ( melody[ii] == 'c' ) tone(buzzerPin, cN, halfBeat * hfBeat[ii]);
if ( melody[ii] == 'd' ) tone(buzzerPin, dN, halfBeat * hfBeat[ii]);
if ( melody[ii] == 'e' ) tone(buzzerPin, eN, halfBeat * hfBeat[ii]);
if ( melody[ii] == 'f' ) tone(buzzerPin, fN, halfBeat * hfBeat[ii]);
if ( melody[ii] == 'g' ) tone(buzzerPin, gN, halfBeat * hfBeat[ii]);
//Serial.println(ii);
}
drive(0);
Serial.println("turned off motors");
}
}
// below here are the functions to help you get up and running fast.Feel free to look around but making changes could break your code.
void drive(int x) {
// map converts [-100,100] to [-255,255]
int spd = map(x, -100, 100, -255, 255);
setSpeeed(spd, 0);
setSpeeed(spd, 1);
}
void setSpeeed(int sped, int motor) {
int speedPin, directionPin;
bool flippedPin;
if ( motor == 0 ) {
speedPin = leftMotorSpeed;
directionPin = leftDirection;
flippedPin = leftIsFlipped;
} else if ( motor == 1 ) {
speedPin = rightMotorSpeed;
directionPin = rightDirection;
flippedPin = rightIsFlipped;
}
if ( sped == 0 ) {
analogWrite(speedPin, 0);
digitalWrite(directionPin, LOW);
} else if ( (sped > 0 && !flippedPin) || (sped < 0 && flippedPin) ) {
digitalWrite(directionPin, HIGH);
analogWrite(speedPin, abs(sped));
} else if ( (sped < 0 && !flippedPin) || (sped > 0 && flippedPin) ) {
digitalWrite(directionPin, LOW);
analogWrite(speedPin, abs(sped));
}
}
void turnDegrees(double y) {
bool lWheel = 0;
bool rWheel = 0;
if (y > 0) {
lWheel = 0;
rWheel = 1;
if (leftIsFlipped) {
lWheel = 1;
}
if (rightIsFlipped) {
rWheel = 0;
}
digitalWrite(leftDirection, lWheel); analogWrite(leftMotorSpeed, 255);
digitalWrite(rightDirection, rWheel); analogWrite(rightMotorSpeed, 255);
delay(y * 6.21111);
analogWrite(leftMotorSpeed, 0);
analogWrite(rightMotorSpeed, 0);
} else if (y < 0) {
lWheel = 1;
rWheel = 0;
if (leftIsFlipped) {
lWheel = 0;
}
if (rightIsFlipped) {
rWheel = 1;
}
digitalWrite(leftDirection, lWheel); analogWrite(leftMotorSpeed, 255);
digitalWrite(rightDirection, rWheel); analogWrite(rightMotorSpeed, 255);
delay(abs(y * 6.21111));
analogWrite(leftMotorSpeed, 0);
analogWrite(rightMotorSpeed, 0);
} else Serial.println("0 degree turn");
}
boolean objectWithin(int cm) {
return getDistance() < cm;
}
int getDistance() {
long duration;
int distance;
digitalWrite(pingPin, LOW);// Added this line
delayMicroseconds(2); // Added this line
digitalWrite(pingPin, HIGH);
delayMicroseconds(10); // Added this line
digitalWrite(pingPin, LOW);
duration = pulseIn(echoPin, HIGH);
distance = (int) ( (duration / 2) / 29.1 );
return distance;
}
//returns true if either front IR sensor detects white
//uses the thresholds for the left and right front IR sensors that
//are calculated via the calibrateBlackWhite function
boolean isWhite() {
digitalWrite(IREmitter, HIGH);
int left = analogRead(leftSensor);
Serial.print("left: "); Serial.println(left);
int right = analogRead(rightSensor);
Serial.print("right: "); Serial.println(right);
digitalWrite(IREmitter, LOW);
int lThresh = (lWhite + ((lBlack - lWhite) / 2));
Serial.print("lThresh: "); Serial.println(lThresh);
int rThresh = (rWhite + ((rBlack - rWhite) / 2));
Serial.print("rThresh: "); Serial.println(rThresh);
if (left < lThresh || right < rThresh) {
return true;
} else return false;
}
//averages 5 black and 5 white IR readings for each front IR sensor
//then calculates the threshold between the balck and white readings
void calibrateBlackWhite() {
int n = 5;
int lBlacks[n]; int rBlacks[n];
int lWhites[n]; int rWhites[n];
Serial.println("place front sensors on black and press button");
while (digitalRead(buttonPin) == 1) {
delay(100);
}
digitalWrite(IREmitter, HIGH);
for (int i = 0; i < n; i++) {
lBlacks[i] = analogRead(leftSensor);
rBlacks[i] = analogRead(rightSensor);
Serial.print("lBlacks: ");
Serial.print(lBlacks[i]);
Serial.print(" rBlacks: ");
Serial.println(rBlacks[i]);
}
digitalWrite(IREmitter, LOW);
Serial.println("place front sensors on white and press button");
delay(1000);
while (digitalRead(buttonPin) == 1) {
delay(100);
}
digitalWrite(IREmitter, HIGH);
for (int i = 0; i < n; i++) {
lWhites[i] = analogRead(leftSensor);
rWhites[i] = analogRead(rightSensor);
Serial.print("lWhites: ");
Serial.print(lWhites[i]);
Serial.print(" rWhites: ");
Serial.println(rWhites[i]);
}
digitalWrite(IREmitter, LOW);
for (int i = 0; i < n; i++) {
lBlack += lBlacks[i];
rBlack += rBlacks[i];
lWhite += lWhites[i];
rWhite += rWhites[i];
}
lBlack = lBlack / n; rBlack = rBlack / n;
lWhite = lWhite / n; rWhite = rWhite / n;
Serial.print("average of left blacks: "); Serial.println(lBlack);
Serial.print("average of right blacks: "); Serial.println(rBlack);
Serial.print("average of left whites: "); Serial.println(lWhite);
Serial.print("average of right whites: "); Serial.println(rWhite);
delay(1000);
}