Hello,
I have this quad copter project that I am working on. I am using Mosfets and the digital pins to provide a PWM for the motors. I am also using the Analog pins to read the values at the motor. But for some reason I only get 0s and 1s (on and off states). Ive tried moving the analog pins to everywhere but still nothing.
here is my code.
int trigger=13; // The trigger sensor pin
int echo=11; // The echo sensor pin
int dangerzone=10; //dangerzone for objects in front of quadcopter
int distance; // Random value for distance
int distance1;
long duration;
long duration1;
int RightFrontMotor=9; //inverted white MOSFET Motor yellow
int LeftFrontMotor=5; // MOSFET white Motor white
int RearRightSideMotor=6; // MOSFET black Motor black
int RearLeftSideMotor=3; // MOSFET black Motor orange
int CalculateDistance();
float pingTime; //sets ping time variable
float FronttargetDistance; // sets target distance variable
float speedOfSound=776.6; // speed of sound in mph
float pingTime1;
float targetDistance1;
const int RightFrontMotorPin=A0; //Analog Pin 0 yellow
const int LeftFrontMotorPin=A1; //Analog Pin 1
const int RearRightSideMotorPin=A2; //Analog Pin 2
const int RearLeftSideMotorPin=A3; //Analog Pin 3
pinMode(RightFrontMotor, INPUT);
pinMode( LeftFrontMotor, INPUT);
pinMode(RearRightSideMotor, INPUT);
//pinMode(RearLeftSideMotor, OUTPUT);
void setup() {
Serial.begin(9600); // baud rate begin
pinMode(trigger, OUTPUT); // sets ultrasonic sensor to output
pinMode(echo, INPUT); // sets ultrasonic sensor to input
pinMode(trigger1, OUTPUT); // sensor 1 trigger pin
pinMode(echo1, OUTPUT); // Ultrasonic Sensor output
}
void loop() {
digitalWrite(13,LOW); // sets trigger pin low
delayMicroseconds(2000); //delay time
digitalWrite(13, HIGH); // sets trigger pin to high
delayMicroseconds(15); // delay time/pause
digitalWrite(13,LOW); //sets trigger back to low
pingTime = pulseIn(11,HIGH); //ping time measurement
pingTime=pingTime/1000000.; // ping time conversion to seconds
pingTime =pingTime/3600.; //ping time conversion with conversion
digitalWrite(trigger1,LOW); // sets trigger pin low
delayMicroseconds(2000); //delay time
digitalWrite(trigger1, HIGH); // sets trigger pin to high
delayMicroseconds(15); // delay time/pause
digitalWrite(trigger1,LOW); //sets trigger back to low
pingTime1 = pulseIn(echo1,HIGH); //ping time measurement
pingTime1=pingTime1/1000000.; // ping time conversion to seconds
pingTime1 =pingTime1/3600.; //ping time conversion with conversion
targetDistance1 =speedOfSoundpingTime1; // target distance is equal to x=vt
targetDistance1 = targetDistance1/2; // since the ping must travel twice as far to get to distance and come back divide by 2
targetDistance1= targetDistance1*63360; // convertes distance to inches
Serial.print( "Back target distance is:");
Serial.print(targetDistance1);
Serial.println("inches away");
if (FronttargetDistance > dangerzone) // Forward Yaw
{
analogWrite(RearRightSideMotor,100); // Full Thrust
analogWrite(RearLeftSideMotor ,0); // Full Thrust
analogWrite(RightFrontMotor ,80); // 50% Thrust
analogWrite(LeftFrontMotor ,85); // 50% Thrust
}
else // Backwards Yaw
{
analogWrite(RearRightSideMotor, 175); // 50% Thrust
analogWrite(RearLeftSideMotor ,100); // 50% Thrust
analogWrite(RightFrontMotor ,15); // Full Thrust
analogWrite(LeftFrontMotor ,200); // Full Thrust
}
float BackwardPitch = (analogRead(RightFrontMotorPin) && analogRead(LeftFrontMotorPin));
float FrontPitch = (analogRead(RearRightSideMotorPin) && analogRead(RearLeftSideMotorPin));
float LeftYaw = (analogRead(RightFrontMotorPin) && analogRead(RearRightSideMotorPin));
float RightYaw = (analogRead(LeftFrontMotorPin) && analogRead(RearLeftSideMotorPin));
String AerialPosition = String(BackwardPitch) + "," + String(FrontPitch) + "," + String(LeftYaw) + "," +String(RightYaw);
Serial.println(AerialPosition);
delay(100);
}