BACKGROUND:
I am working on a group project to design and create a DIY remote control hovercraft. To do so we are using 4 fans (each has a motor) provided by the school. I am unsure what type of motors they are but they seem to be nothing fancy and pretty standard. Our hovercraft thrust fan is connected to an L298N HBridge so we can control the speed, as well as have reverse thrust. We also have two servo's (sg90) connected to turn the rudder for steering and connected to a dropping mechanism. The other three fans are connected to an RFP30N06LE MOSFET and are to be used to generate a lift force to "lift" the hovercraft. We are using an Arduino Uno attached to a USB host shield as our microcontroller and PS4 controller (Arduino PS4 library imported) connected by Bluetooth to control the Arduino inputs.
PROBLEM:
So basically our problem lies with the three lift fans and MOSFET circuit. We would like to be able to push a single button on the controller to start and stop the lift fan motors. The circuit works when we turn on and off the MOSFET manually (i.e. by connecting the wire to a + or -) as well when we set it up by using a button to turn it on or off, but when we connect it to the Arduino and the PS4 controller we get no outputs.
Below are links to the MOSFET datasheet, the Arduino PS4 library and the code I have written to attempt to control the MOSFET with the PS4 controller. Also, I have attached pictures of our circuit connected to the hovercraft and Arduino (black wire into Arduino goes to ground, red wire goes to pin 9, the black and red wires dangling off the hovercraft connect to our 9.6V battery). I should also note that all other controller inputs produce an output (i.e. We can turn the right analog stick to turn the rudder servo motor or press down on the D-pad to change the thrust fan direction).
QUESTION:
If anybody has any insight on how we can make our PS4 controller control the three lift fans by turning on/off the MOSFET please share. Either some changes to the code or changes to the circuit. We are looking for any and all help we can get. Thanks a bunch, friends!
SOLUTION:
The pin connecting the MOSFET to the Arduino needed to be a normal digital pin not a PWM pin. Therefore, we changed int mos = 9;
to int mos = 4;
.
MOSFET datasheet: https://cdn.sparkfun.com/datasheets/Components/General/RFP30N06LE.pdf
Arduino PS4 Library: GitHub - felis/USB_Host_Shield_2.0: Revision 2.0 of USB Host Library for Arduino.
Written Code uploaded to Arduino:
#include <Servo.h>
#include <PS4BT.h>
#include <usbhub.h>
// Satisfy the IDE, which needs to see the include statment in the ino too.
#ifdef dobogusinclude
#include <spi4teensy3.h>
#endif
#include <SPI.h>
USB Usb;
//USBHub Hub1(&Usb); // Some dongles have a hub inside
BTD Btd(&Usb); // You have to create the Bluetooth Dongle instance like so
/* You can create the instance of the PS4BT class in two ways */
// This will start an inquiry and then pair with the PS4 controller - you only have to do this once
// You will need to hold down the PS and Share button at the same time, the PS4 controller will then start to blink rapidly indicating that it is in pairing mode
//PS4BT PS4(&Btd, PAIR);
// After that you can simply create the instance like so and then press the PS button on the device
PS4BT PS4(&Btd);
uint8_t oldL2Value, oldR2Value;
// pins to enable motor A
int enA = 6;
int in1 = 7;
int in2 = 8;
int dropperAngle = 90; // set initial dropping mechanism servo angle to 90 degress
boolean fanDirection = true; // start fan in forward direction
int mos = 9;
boolean lift = true;
boolean thrust = true;
Servo rudder; // create servo object to control rudder servo
Servo dropper; // create servo object to control dropping mechanism servo
void setup() {
Serial.begin(9600);
#if !defined(__MIPSEL__)
while (!Serial); // Wait for serial port to connect - used on Leonardo, Teensy and other boards with built-in USB CDC serial connection
#endif
if (Usb.Init() == -1) {
Serial.print(F("\r\nOSC did not start"));
while (1); // Halt
}
Serial.print(F("\r\nPS4 Bluetooth Library Started"));
//initialize of L298N Duel H Bridge
pinMode(enA,OUTPUT);
pinMode(in1,OUTPUT);
pinMode(in2,OUTPUT);
// MOSFET initialization
pinMode(mos,OUTPUT);
digitalWrite(mos,LOW); // ?? unsure if neccesary to initialize the input to LOW ??
// set initial rotation direction for thrust fan
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
// set up rudder servo and dropping mechanism servo
rudder.attach(3);
dropper.attach(5);
}
void loop() {
Usb.Task();
if (PS4.connected()) {
// disconnects PS4 controller
if (PS4.getButtonClick(PS)) {
PS4.disconnect();
}
// maps right stick to rotate rudder servo
rudder.write(map(PS4.getAnalogHat(RightHatX), 0, 255, 0, 180));
// writes anolog value (0-255) to thrust fan motor (only writes if the value is different)
if(PS4.getAnalogButton(R2) > 0 && PS4.getAnalogButton(R2) != oldR2Value)
analogWrite(enA, PS4.getAnalogButton(R2));
oldR2Value = PS4.getAnalogButton(R2);
// rotates dropping mechanism servo to the left 45 degrees
if (PS4.getButtonClick(LEFT)) {
dropperAngle += 45;
dropper.write(dropperAngle);
delay(5);
}
// rotate dropping mechanism servo to the right 45 degrees
if (PS4.getButtonClick(RIGHT)) {
dropperAngle -= 45;
dropper.write(dropperAngle);
delay(5);
}
// changes the rotation direction of the thrust fan connected to L298N H Bridge
if(PS4.getButtonClick(DOWN)){
if(fanDirection){
digitalWrite(in1,LOW);
digitalWrite(in2,HIGH);
fanDirection = !fanDirection;
delay(10);
}else if(!fanDirection){
digitalWrite(in1,HIGH);
digitalWrite(in2,LOW);
fanDirection = !fanDirection;
delay(10);
}
}
// turns of the thrust fan motor
if(PS4.getButtonClick(X)){
if(thrust){
digitalWrite(in1,LOW);
digitalWrite(in2,LOW);
digitalWrite(mos,LOW); // ?? would like to connect the MOSFET (lift fans) to this button mapping to all fans turn off simultaneously ??
}
}
// ------------------------------------------ // NOTE: I comment out one or the other algorithms below when I am implementing one of them
// original idea to to turn MOSTFET on or off {no luck to get working}
if(PS4.getButtonClick(UP)){
if(lift){
digitalWrite(mos,LOW);
//Serial.print(F("\r\nUp1")); // button click does register in the serial monitor and so does the one below
Serial.print(mos);
}
else{
digitalWrite(mos,HIGH);
//Serial.print(F("\r\nUp2"));
Serial.print(mos);
}
lift = !lift;
}
// -----------------------------------------
// second idea to turn MOSFET on or off {still no luck}.
if(PS4.getButtonClick(UP)){
digitalWrite(mos,HIGH);
}
// -----------------------------------------
// ----- What I am basing my MOSFET algorithm off of --------- //
// digitalWrite(mos, HIGH);
// delay(3000);
// digitalWrite(mos, LOW);
// delay(3000);
}
}