Skip to content

Commit

Permalink
Increase the accurancy while rotating
Browse files Browse the repository at this point in the history
  • Loading branch information
jaredliw committed Jul 21, 2021
1 parent 266b0f5 commit 49337d7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions PikaBot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define RIGHT_IR A1

#define DELAY 50
#define DIAMETER 15
#define WIDTH 15

int pitchToFreq(String pitch)
{
Expand Down Expand Up @@ -210,9 +210,11 @@ void PikaBot::play(String musicSheet)
musicSheet += ' ';
int lastSpacePos = -1;
int lastNote = QUARTER;
while (true) {
while (true)
{
int spacePos = musicSheet.indexOf(' ', lastSpacePos + 1);
if (spacePos == -1) {
if (spacePos == -1)
{
break;
}
int colonPos = musicSheet.indexOf(':', lastSpacePos + 1);
Expand All @@ -222,14 +224,16 @@ void PikaBot::play(String musicSheet)
{
pitch = musicSheet.substring(lastSpacePos + 1, colonPos);
note = musicSheet.substring(colonPos + 1, spacePos);
} else
}
else
{
pitch = musicSheet.substring(lastSpacePos + 1, spacePos);
note = String(lastNote);
}
int intNote = note.toInt();
lastSpacePos = spacePos;
if ((!String(intNote).equals(note)) || intNote <= 0) {
if ((!String(intNote).equals(note)) || intNote <= 0)
{
continue;
}
this->playTone(pitch, intNote);
Expand Down Expand Up @@ -257,13 +261,13 @@ void PikaBot::stop()

void PikaBot::turnLeft(uint8_t speed)
{
this->move(speed, -speed);
this->move(speed, 0);
}

void PikaBot::turnLeft(uint8_t speed, uint8_t angle)
{
this->turnLeft(speed);
this->_delayMotors(speed, PI * DIAMETER * angle / 360);
this->_delayMotors(speed, 2 * PI * WIDTH * angle / 360);
}

void PikaBot::turnRight(uint8_t speed)
Expand All @@ -274,7 +278,7 @@ void PikaBot::turnRight(uint8_t speed)
void PikaBot::turnRight(uint8_t speed, uint8_t angle)
{
this->turnRight(speed);
this->_delayMotors(speed, PI * DIAMETER * angle / 360);
this->_delayMotors(speed, 2 * PI * WIDTH * angle / 360);
}

uint16_t PikaBot::ultrasonicDistance()
Expand Down

0 comments on commit 49337d7

Please sign in to comment.