Have to give props to Aaron since he figure out how to code the Arduino so that the thumbstick was working with the propeller. Here we have the complete code for the airboat.
NOTE: some of the code was not used, but it is inactive.
PS2 wireless airboat arduino code
#include <Wire.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_ PWMServoDriver.h"
#include <PS2X_lib.h> //for v1.6
#include <Servo.h>
#include <Adafruit_MotorShield.h>
#include "utility/Adafruit_
#include <PS2X_lib.h> //for v1.6
#include <Servo.h>
PS2X ps2x; // create PS2 Controller Class
//right now, the library does NOT support hot pluggable controllers, meaning
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor2 = AFMS.getMotor(4);
int pos = 90;
int servoInput = 128;
int motorSpeed = 0;
int motorInput = 128;
Servo myservo;
void setup(){
Serial.begin(9600);
AFMS.begin();
motor1->setSpeed(motorSpeed);
motor2->setSpeed(motorSpeed);
myservo.attach(9);
ps2x.config_gamepad(13,11,10, 12, true, true);
}
//you must always either restart your Arduino after you conect the controller,
//or call config_gamepad(pins) again after connecting the controller.
Adafruit_MotorShield AFMS = Adafruit_MotorShield();
Adafruit_DCMotor *motor1 = AFMS.getMotor(1);
Adafruit_DCMotor *motor2 = AFMS.getMotor(4);
int pos = 90;
int servoInput = 128;
int motorSpeed = 0;
int motorInput = 128;
Servo myservo;
void setup(){
Serial.begin(9600);
AFMS.begin();
motor1->setSpeed(motorSpeed);
motor2->setSpeed(motorSpeed);
myservo.attach(9);
ps2x.config_gamepad(13,11,10,
}
void loop(){
/* You must Read Gamepad to get new values
Read GamePad and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
you should call this at least once a second
*/
ps2x.read_gamepad();
/*if(ps2x.Button(PSB_PAD_UP)) // Props ON -> Forward
{
motor1->run(FORWARD);
motor2->run(FORWARD);
Serial.println("Forward");
}
else if(ps2x.ButtonReleased(PSB_ PAD_UP)) // Props OFF
{
motor1->run(RELEASE);
motor2->run(RELEASE);
}
if(ps2x.Button(PSB_PAD_DOWN)) // Props ON -> Backward
{
motor1->run(BACKWARD);
motor2->run(BACKWARD);
Serial.println("Reverse");
}
else if(ps2x.ButtonReleased(PSB_ PAD_DOWN)) // Props OFF
{
motor1->run(RELEASE);
motor2->run(RELEASE);
}
*/
/*if(ps2x.Button(PSB_L1)) // Turns Fins LEFT
{
pos += 5;
Serial.println(pos);
}
if(ps2x.Button(PSB_R1)) // Turns Fins RIGHT
{
pos -= 5;
Serial.println(pos);
}
else if(ps2x.ButtonPressed(PSB_ BLUE)) // CENTERS The Fins
{
pos = 90;
}
if(pos >= 180) // Servo MAX RIGHT, pos = 180
{
pos = 175;
}
else if(pos <=0) // Servo MAX LEFT, pos = 0
{
pos = 5;
}
*/
motorInput = ps2x.Analog(PSS_LY), DEC;
if(motorInput >= 0 && motorInput < 100)
{
motorSpeed = map(motorInput,0,128,255,0);
motor1->setSpeed(motorSpeed);
motor1->run(FORWARD);
motor2->setSpeed(motorSpeed);
motor2->run(FORWARD);
}
if(motorInput > 150 && motorInput <= 255)
motorSpeed = map(motorInput,128,255,0,255);
motor1->setSpeed(motorSpeed);
motor1->run(BACKWARD);
motor2->setSpeed(motorSpeed);
motor2->run(BACKWARD);
}
if(motorInput >= 100 && motorInput <= 150)
{
motor1->run(RELEASE);
motor2->run(RELEASE);
}
servoInput = ps2x.Analog(PSS_RX), DEC;
pos = map(servoInput,0,255,180,0);
if(pos >= 120 && pos <= 136) //Stops the servo from 'twitching'
{
myservo.write(90);
}
else
{
myservo.write(pos);
}
Serial.println(motorInput);
delay(5);
}
/* You must Read Gamepad to get new values
Read GamePad and set vibration values
ps2x.read_gamepad(small motor on/off, larger motor strenght from 0-255)
if you don't enable the rumble, use ps2x.read_gamepad(); with no values
you should call this at least once a second
*/
ps2x.read_gamepad();
/*if(ps2x.Button(PSB_PAD_UP)) // Props ON -> Forward
{
motor1->run(FORWARD);
motor2->run(FORWARD);
Serial.println("Forward");
}
else if(ps2x.ButtonReleased(PSB_
{
motor1->run(RELEASE);
motor2->run(RELEASE);
}
if(ps2x.Button(PSB_PAD_DOWN)) // Props ON -> Backward
{
motor1->run(BACKWARD);
motor2->run(BACKWARD);
Serial.println("Reverse");
}
else if(ps2x.ButtonReleased(PSB_
{
motor1->run(RELEASE);
motor2->run(RELEASE);
}
*/
/*if(ps2x.Button(PSB_L1)) // Turns Fins LEFT
{
pos += 5;
Serial.println(pos);
}
if(ps2x.Button(PSB_R1)) // Turns Fins RIGHT
{
pos -= 5;
Serial.println(pos);
}
else if(ps2x.ButtonPressed(PSB_
{
pos = 90;
}
if(pos >= 180) // Servo MAX RIGHT, pos = 180
{
pos = 175;
}
else if(pos <=0) // Servo MAX LEFT, pos = 0
{
pos = 5;
}
*/
motorInput = ps2x.Analog(PSS_LY), DEC;
if(motorInput >= 0 && motorInput < 100)
{
motorSpeed = map(motorInput,0,128,255,0);
motor1->setSpeed(motorSpeed);
motor1->run(FORWARD);
motor2->setSpeed(motorSpeed);
motor2->run(FORWARD);
}
if(motorInput > 150 && motorInput <= 255)
motorSpeed = map(motorInput,128,255,0,255);
motor1->setSpeed(motorSpeed);
motor1->run(BACKWARD);
motor2->setSpeed(motorSpeed);
motor2->run(BACKWARD);
}
if(motorInput >= 100 && motorInput <= 150)
{
motor1->run(RELEASE);
motor2->run(RELEASE);
}
servoInput = ps2x.Analog(PSS_RX), DEC;
pos = map(servoInput,0,255,180,0);
if(pos >= 120 && pos <= 136) //Stops the servo from 'twitching'
{
myservo.write(90);
}
else
{
myservo.write(pos);
}
Serial.println(motorInput);
delay(5);
}
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home