Arduino Serial Monitor only reading 0s and 1s PLEASE HELP

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);

}

float BackwardPitch = (analogRead(RightFrontMotorPin) && analogRead(LeftFrontMotorPin));

What do you imagine that line does?

Please use code tags when posting code.

PWM is zeros and ones, that is all you will read if you try and read them.

Note pay attention to what AWOL is saying.

Hi,

Can you please post a copy of your sketch, using code tags?
They are made with the </> icon in the reply Menu.
See section 7 http://forum.arduino.cc/index.php/topic,148850.0.html

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.....Tom.... :slight_smile:

Checking PWM values with analogRead() or with digitalRead() - which would make more sense - is utterly pointless.

If you really need to check the variations at the motor use an oscilloscope - but the strong likelihood is that analogWrite() works properly.

...R

For more on what PWM actually is read this:-
http://www.thebox.myzen.co.uk/Tutorial/PWM.html

But if it's a PWM, shouldnt analog be reading values between 0-5V? I don't understand why it is onlu measuring 0 and 1s

Did you answer the question in reply #1?

But if it's a PWM, shouldnt analog be reading values between 0-5V? I don't understand why it is onlu measuring 0 and 1s

PWM is DIGITAL. But as you know, it CAN be used to control the speed of a motor. It can also dim an incandescent lamp or it can make an LED appear dim to the human eye.

And, PWM can be converted to analog with a low-pass filter.

P.S.
It rarely makes sense to for the processor to read it's own output... The software is in total control of outputs, so the software already "knows" the output state. :wink:

mandre12:
But if it's a PWM, shouldnt analog be reading values between 0-5V? I don't understand why it is onlu measuring 0 and 1s

Did you actually read that link I posted?

Can you read?

That link addressed exactly that question.

shouldnt analog be reading values between 0-5V?

No read that link and stop being lazy.

mandre12:
I don't understand why it is onlu measuring 0 and 1s

Maybe it would make more sense if you imagine the function is called PWMwrite() rather than analogWrite()

There is nothing analog about PWM.

...R

Even if you had an analog voltage that you could read, you would get 0's and 1's:

float BackwardPitch = (analogRead(RightFrontMotorPin) && analogRead(LeftFrontMotorPin));
float FrontPitch = (analogRead(RearRightSideMotorPin) && analogRead(RearLeftSideMotorPin));
float LeftYaw = (analogRead(RightFrontMotorPin) && analogRead(RearRightSideMotorPin));
float RightYaw = (analogRead(LeftFrontMotorPin) && analogRead(RearLeftSideMotorPin));

That code does not do what you think it does. && is a boolean and operation. (A && B) is 1 (true) if both A and B evaluate to true (for integers, anything other than 0 is true), otherwise it's 0 (false). Then you're converting that to a float which - unsurprisingly - takes on the value of 0 or 1.

I have NO CLUE what sort of math you were expecting to happen there.

In any event - it won't work, you're driving that pin with PWM (it looks like - but you haven't set it as output, which you should do), so analogRead() will read ~0v (near 0) or ~5v (near 1023), not anything inbetween.

Why are you reading a pin which you're also driving with analogWrite()? You know what the pin is doing, you're actively controlling it with analogWrite() - you know the duty cycle.

Your code should be posted in code tags.

You have comments that don't match the code, or don't appear to - and much of the code doesn't look like it does what you think it does.