In: Computer Science
How do I change my if statements into while and for loops, so
that I can make my bot go straight and turn exactly 12 times.
/***********************************************************************
* Exp7_1_RotaryEncoder -- RedBot Experiment 7_1
*
* Knowing where your robot is can be very important. The RedBot
supports
* the use of an encoder to track the number of revolutions each
wheels has
* made, so you can tell not only how far each wheel has traveled
but how
* fast the wheels are turning.
*
* This sketch was written by SparkFun Electronics, with lots of
help from
* the Arduino community. This code is completely free for any
use.
*
* 8 Oct 2013 M. Hord
* Revised, 31 Oct 2014 B. Huang
***********************************************************************/
#include <RedBot.h>
RedBotMotors motors;
RedBotEncoder encoder = RedBotEncoder(3,9); // initializes
encoder on pins A2 and 10
int buttonPin = 12;
int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192
ticks per wheel rev
// variables used to store the left and right encoder
counts.
int lCount;
int rCount;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("left right");
Serial.println("================");
}
void turn()
{
long lCount = 0;
long rCount = 0;
long targetCount;
float numRev;
motors.leftMotor(-80);
motors.rightMotor(0);
// variables for tracking the left and right encoder counts
long prevlCount, prevrCount;
long lDiff, rDiff; // diff between current encoder count and previous count
encoder.clearEnc(BOTH); // clear the encoder count
delay(100); // short delay before starting the motors.
while (lCount < 0)
{
// while the right encoder is less than the target count – debug print
// the encoder values and wait – this is a holding loop.
lCount = encoder.getTicks(LEFT);
rCount = encoder.getTicks(RIGHT);
Serial.print(lCount);
Serial.print("\t");
Serial.print(rCount);
Serial.print("\t");
Serial.println(targetCount);
// calculate the rotation “speed” as a difference in the count from previous cycle.
lDiff = (lCount - prevlCount);
rDiff = (rCount - prevrCount);
// store the current count as the “previous” count for the next cycle.
prevlCount = lCount;
prevrCount = rCount;
}
delay(500); // short delay to give motors a chance to
respond.
motors.brake();
}
void loop(void)
{
// wait for a button press to start driving.
if(digitalRead(buttonPin) == LOW)
{
encoder.clearEnc(BOTH); // Reset the counters.
delay(500);
motors.drive(180); // Start driving forward.
}
// store the encoder counts to a variable.
lCount = encoder.getTicks(LEFT); // read the left motor
encoder
rCount = encoder.getTicks(RIGHT); // read the right motor
encoder
// print out to Serial Monitor the left and right encoder
counts.
Serial.print(lCount);
Serial.print("\t");
Serial.println(rCount);
// if either left or right motor are more than 5 revolutions,
stop
if ((lCount >= 5*countsPerRev) || (rCount >= 5*countsPerRev
))
{
turn();
encoder.clearEnc(BOTH);
//motors.brake();
delay(500);
motors.drive(180); // Start driving forward.
}
}
Below is a sample code doing the same:
CODE
#include <RedBot.h>
RedBotMotors motors;
RedBotEncoder encoder = RedBotEncoder(3,9); // initializes
encoder on pins A2 and 10
int buttonPin = 12;
int countsPerRev = 192; // 4 pairs of N-S x 48:1 gearbox = 192
ticks per wheel rev
// variables used to store the left and right encoder
counts.
int lCount;
int rCount;
void setup()
{
pinMode(buttonPin, INPUT_PULLUP);
Serial.begin(9600);
Serial.println("left right");
Serial.println("================");
}
void turn()
{
long lCount = 0;
long rCount = 0;
long targetCount;
float numRev;
motors.leftMotor(-80);
motors.rightMotor(0);
// variables for tracking the left and right encoder counts
long prevlCount, prevrCount;
long lDiff, rDiff; // diff between current encoder count and previous count
encoder.clearEnc(BOTH); // clear the encoder count
delay(100); // short delay before starting the motors.
while (lCount < 0)
{
// while the right encoder is less than the target count – debug print
// the encoder values and wait – this is a holding loop.
lCount = encoder.getTicks(LEFT);
rCount = encoder.getTicks(RIGHT);
Serial.print(lCount);
Serial.print("\t");
Serial.print(rCount);
Serial.print("\t");
Serial.println(targetCount);
// calculate the rotation “speed” as a difference in the count from previous cycle.
lDiff = (lCount - prevlCount);
rDiff = (rCount - prevrCount);
// store the current count as the “previous” count for the next cycle.
prevlCount = lCount;
prevrCount = rCount;
}
delay(500); // short delay to give motors a chance to
respond.
motors.brake();
}
void loop(void)
{
// wait for a button press to start driving.
if(digitalRead(buttonPin) == LOW)
{
encoder.clearEnc(BOTH); // Reset the counters.
delay(500);
motors.drive(180); // Start driving forward.
}
// store the encoder counts to a variable.
lCount = encoder.getTicks(LEFT); // read the left motor
encoder
rCount = encoder.getTicks(RIGHT); // read the right motor
encoder
// print out to Serial Monitor the left and right encoder
counts.
Serial.print(lCount);
Serial.print("\t");
Serial.println(rCount);
// if either left or right motor are more than 5 revolutions, stop
int count = 0;
while (count < 12)
{
if (lCount >= 5*countsPerRev) || (rCount >= 5*countsPerRev
)
{
turn();
encoder.clearEnc(BOTH);
//motors.brake();
delay(500);
motors.drive(180); // Start driving forward.
count ++;
}
}
}