Justin and Ken's Super Turtle Tank
Justin Hartsaw & Kenneth Yesh
Overview
For our project, we made a car that you can program 1 - 10 commands: drive forward, drive backward, turn left, and turn right. You input the sequence by pressing the corresponding push button. Then after you enter your sequence, there is a fifth push button you press to start the series of commands.
The Program
With the program, you can press the four action buttons in any order of your choosing up and multiple times. Then you press the fifth button to start the command sequence. The action buttons work becuse the button pushed corosponds to a number which is stored in an array. When the fifth button is pushed it runs through the array reading what number is there and running its corresponding command.
Pictures
Demo Video
Circuit Diagram
Arduino Program
int motor1a = 1; //declares the first pin for the left motor
int motor1b = 10; //declares the other pin for the left motor
int motor1pmw = 5; // this is the pmw pin for the left motor that will set how much battery power the motor is getting (speed)
int motor2a = 2; //declares the first pin for the right motor
int motor2b = 7; // declares the other pin for the right motor
int motor2pmw = 6; // this is the pmw pin for the left motor that will set how much battery power the motor is getting (speed)
int switchPinF = 11; // button is connected to pin 6
int valF; // variable for reading the pin status
int buttonStateF; // variable to hold the button state
int switchPinB = 4; // switch is connected to pin 4
int valB; // variable for reading the pin status
int buttonStateB; // variable to hold the button state
int switchPinL = 12; // switch is connected to pin 12
int valL; // variable for reading the pin status
int buttonStateL; // variable to hold the button state
int switchPinR = 15; // switch is connected to pin 15
int valR; // variable for reading the pin status
int buttonStateR; // variable to hold the button state
int stepb []={0,0,0,0,0,0,0,0,0,0};//array for storing commands
int list = 0; //variable to change the position in the array the command is stored
int go = 3; //this declares the start push button
int run = 0; // variable that switches between 1 and 0
int goval; //variable reading pin status
int action; // variable to run threw the command code
int d = 1000; //the time that each action runs
int led = 14; // declares led as pin 14
void input()
/*process that can be called up later in the program to assign numbers to one of the four possible commands 1 for forward 2 for backwards 3 for left and 4 for right*/
{
if (valF != buttonStateF) { // the forward button state has changed!
if (valF == LOW) { // check if the forward button is pressed
stepb[list] = 1; // store the number 1 for the forward command
list++; // increase list to store the next command number
digitalWrite(led,HIGH); // turns led on so you know the button was pushed
}
}
if (valB != buttonStateB) { // the button state has changed!
if (valB == LOW) { // check if the button is pressed
stepb[list] = 2; // store the number 2 for the forward command
list++; // increase list to store the next command number
digitalWrite(led,HIGH); // turns led on so you know the button was pushed
}
}
if (valL != buttonStateL) { // the button state has changed!
if (valL == LOW) { // check if the button is pressed
stepb[list] = 3; // store the number 3 for the forward command
list++; // increase list to store the next command number
digitalWrite(led,HIGH); // turns led on so you know the button was pushed
}
}
if (valR != buttonStateR) { // the button state has changed!
if (valR == LOW) { // check if the button is pressed
stepb[list] = 4; // store the number 4 for the forward command
list++; // increase list to store the next command number
digitalWrite(led,HIGH); // turns led on so you know the button was pushed
}
}
delay(500);
digitalWrite(led,LOW); //turns LED off
}
void comand(){ /* process that can be called back later in the program that translates the numbers into actual commands such as forward backwards left and right*/
if (action == 1) //forward
{
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
delay(d);
}
if (action == 2){ //backward
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
delay(d);
}
if (action == 3){ //left
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
delay(100);// turns motor off giving it a chance to pause so it is less jerky
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, HIGH);
digitalWrite(motor2a, HIGH);
digitalWrite(motor2b, LOW);
delay(350);
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
delay(100);// turns motor off giving it a chance to pause so it is less jerky
}
if (action == 4){ //right
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
delay(100);
digitalWrite(motor1a, HIGH);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, HIGH);
delay(350);
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
delay(100);
}
}
void setup() //the setup faze where things are declared(runs through once)
{
pinMode(motor1a, OUTPUT); // these simply are declaring them as outputs
pinMode(motor1b, OUTPUT); // these simply are declaring them as outputs
pinMode(motor1pmw, OUTPUT); // these simply are declaring them as outputs
pinMode(motor2a, OUTPUT); // these simply are declaring them as outputs
pinMode(motor2b, OUTPUT); // these simply are declaring them as outputs
pinMode(motor2pmw, OUTPUT); // these simply are declaring them as outputs
analogWrite(motor1pmw, 255); // this is the analog speed value for the arduino (0-255) which controls motor speed 255 being the fastest
analogWrite(motor2pmw, 255); // this is the analog speed value for the arduino (0-255) which controls motor speed 255 being the fastest
pinMode(led, OUTPUT); // these simply are declaring them as outputs
}
void loop()
{
if (run == 0){
valF = digitalRead(switchPinF); // read input value and store it in valF
valB = digitalRead(switchPinB); // read input value and store it in valB
valL = digitalRead(switchPinL); // read input value and store it in valL
valR = digitalRead(switchPinR); // read input value and store it in valR
goval = digitalRead(go); // read input value and store it in goval
if (goval == HIGH) //if the button was pushed start running the cars program
{
run = 1; // set run = 1
}
input(); // run the input process above
buttonStateF = valF; // save the new state in our variable
buttonStateB = valB; // save the new state in our variable
buttonStateL = valL; // save the new state in our variable
buttonStateR = valR; // save the new state in our variable
}
if (run == 1) // run the car through the commands programed in
{
for(int x=0; x<list; x++) //runs the loop as long as "x" is less than "list" and add 1 to "x" every time you run through the loop
{
action=stepb[x]; // sets action equal to a number command saved in the input process
comand(); //runs the command process
}
run = 0; //resets run = 0 to stop the car from looping
list = 0; //reset list = 0
/* prevents car from continuously carrying out its last step.*/
digitalWrite(motor1a, LOW);
digitalWrite(motor1b, LOW);
digitalWrite(motor2a, LOW);
digitalWrite(motor2b, LOW);
}
}
Encountered Problems
Coding
To make the combination work, we first tired using a complex series of "if" statements, but that didn't work so we used an array instead. It worked perfectly after that.
Right Turn Button
While testing the car, we noticed that the right button wasn't working. We did a few troubleshooting ideas, but it still wasn't working. So then we decided to switch what pin it was attached to on the Arduino. We ended up connecting it to analog A1 pin and setting it to pinMode.
Extending the Project
If we had more time to work on the project, we would install sensors so the car wouldn't be running into anything. Also, we would transfer everything to a circuit board so it would look nicer.