Josephine S.
Hi! My name is Josephine and I’m a rising sophomore at the Ramaz Upper School. My starter project is the Simon Says game by Sparkfun. I chose this starter project because I have never soldered before, so this project gave me many opportunities to practice my soldering skills. My main project is the RC Robot Tank. I chose this project because I was excited to work with Arduino in addition to doing mechanical engineering for my first time.
Engineer
Josephine S
Area of Interest
Biology, Electrical Engineering
School
Ramaz Upper School
Grade
Incoming Sophomore
Final Milestone
#include PS2X ps2x;
int error = 0;
byte type = 0;
byte vibrate = 0;
int speedA = 80; /*setting this up to change this value using analog write (values from 0-255)
so I can change it based on user input through the PS2 contoller. 80 is the default speed
once the motors start. There are 2 speed values, one for A and one for B*/
int speedB = 80;
int motornameA;
int motornameB;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println(“Serial monitor initialized”);
//(clock, command, attention, data, pressure sensitivity enabled?, rumble enabled?)
error = ps2x.config_gamepad(4, 5, 6, 7, false, false);
if(error == 0){
Serial.println(“Found Controller, configured successful”);
}
else if(error == 1)
Serial.println(“No controller found, check wiring, see readme.txt to enable debug. visit www.billporter.info for troubleshooting tips”);
else if(error == 2)
Serial.println(“Controller found but not accepting commands. see readme.txt to enable debug. Visit www.billporter.info for troubleshooting tips”);
else if(error == 3)
Serial.println(“Controller refusing to enter Pressures mode, may not support it. “);
type = ps2x.readType();
switch(type) {
case 0:
Serial.println(“Unknown Controller type”);
break;
case 1:
Serial.println(“DualShock Controller Found”);
break;
case 2:
Serial.println(“GuitarHero Controller Found”);
break;
}
//Setup Channel A
pinMode(12, OUTPUT); //Initiates Motor Channel A pin
pinMode(9, OUTPUT); //Initiates Brake Channel A pin
Serial.println(“Channel A setup complete.”);
//Setup Channel B
pinMode(13, OUTPUT); //Initiates Motor Channel A pin
pinMode(8, OUTPUT); //Initiates Brake Channel A pin
Serial.println(“Channel B setup complete.”);
Serial.println(“Setup finished”);
}
void loop() {
// put your main code here, to run repeatedly:
delay(100);
ps2x.read_gamepad();
//right side buttons if statements FOR MOTOR B
if(ps2x.ButtonPressed(PSB_GREEN)) //up
{
digitalWrite(13, HIGH); //Establishes forward (same as A backward) direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, speedB); //Spins the motor on Channel B at speed’s value
motornameB = 11;
digitalWrite(12, LOW); //Establishes backward (same as B forward) direction of Motor A
digitalWrite(9, LOW); //Disengage the Brake for Motor A
analogWrite(3, speedA); //Spins motor A at speed’s value
motornameA = 3;
}
else if(ps2x.ButtonPressed(PSB_RED)) //right
{
Serial.println(“red pressed”);
//move motor A
digitalWrite(12, HIGH); //Establishes forward (same as B backward) direction of Motor A
digitalWrite(9, LOW); //Disengage the Brake for Motor A
analogWrite(3, speedA); //Spins motor A at speed’s value
motornameA = 3;
//brake motor B
digitalWrite(8, HIGH); //Englage the brake for Motor B
}
else if(ps2x.ButtonPressed(PSB_PINK)) //left
{
Serial.println(“pink pressed”);
//move motor B
digitalWrite(13, LOW); //Establishes backward (same as A forward) direction of Motor B
digitalWrite(8, LOW); //Disengage the Brake for Motor B
analogWrite(11, speedB);
motornameB = 11;
//brake motor A
digitalWrite(9, HIGH); //Englage the brake for Motor A
}
else if(ps2x.ButtonPressed(PSB_BLUE)) //down
{
Serial.println(“blue pressed”);
digitalWrite(13, LOW); //Establishes backward (same as A forward) direction of Channel B
digitalWrite(8, LOW); //Disengage the Brake for Channel B
analogWrite(11, speedB); //Spins the motor on Channel B at speed’s value
motornameB = 11;
digitalWrite(12, HIGH); //Establishes forward (same as B backward) direction of Motor A
digitalWrite(9, LOW); //Disengage the Brake for Motor A
analogWrite(3, speedA); //Spins motor A at speed’s value
motornameA = 3;
}
//middle buttons if statements
else if(ps2x.ButtonPressed(PSB_SELECT))
{
Serial.println(“select button pressed”);
//brake motor A
digitalWrite(9, HIGH); //Englage the brake for Motor A
//brake motor B
digitalWrite(8, HIGH); //Englage the brake for Motor B
}
//side buttons right if statements
else if(ps2x.ButtonPressed(PSB_R1))
{
Serial.println(“right side side upper button pressed”);
speedB = speedB + 10;
speedB = constrain(speedB, 0, 255);
Serial.print(“Speed B = “);
Serial.print(speedB);
Serial.println(” “);
speedA = speedA + 10;
speedA = constrain(speedA, 0, 255);
Serial.print(“Speed A = “);
Serial.print(speedA);
Serial.println(” “);
analogWrite(motornameB, speedB);
Serial.println(motornameB);
analogWrite(motornameA, speedA);
Serial.println(motornameA);
}
else if(ps2x.ButtonPressed(PSB_R2))
{
Serial.println(“right side side lower button pressed”);
speedB = speedB – 10;
speedB = constrain(speedB, 0, 255);
Serial.print(“Speed B = “);
Serial.print(speedB);
Serial.println(” “);
speedA = speedA – 10;
speedA = constrain(speedA, 0, 255);
Serial.print(“Speed A = “);
Serial.print(speedA);
Serial.println(” “);
analogWrite(motornameB, speedB);
Serial.println(motornameB);
analogWrite(motornameA, speedA);
Serial.println(motornameA);
}
}