Reading signal from Digital pins

Hi all,

I am currently working on an obstacle avoidance project. It consist of an Infrared sensor which measures the distance from the Arduino Uno and feedback the ADC signal to the A0 pin which can be monitored through the serial monitor. The following is my code:

int IRValue;

void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
aileron.attach(9);
while(!Serial); // wait for serial port to connect. Needed for native USB
}

void loop() {
// put your main code here, to run repeatedly:
IRValue=analogRead(A0); //Reads the value from analog pin (A0)
Serial.print(“IR Sensor=” ); //Display “IR Sensor=” on computer monitor (No next line)
Serial.println(IRValue); //Display IRValue on computer monitor follow by a next line

delay(500); //Pauses the program for 500 milisecond
}

The issue now is that I intend to send a value (let’s say 1800 micro second) to the servo through digital pin 9 when the ADC signal goes above 110. I also want to read/monitor the output signal generated in digital pin 9 through serial monitor. Any suggestions if it is achievable? The following is my attempt, doesn’t managed to get the digital pin 9 to display 1800 through the serial monitor as it shows 0 all the way.

#include<Servo.h>
Servo aileron;

int IRValue;
int DValue;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
aileron.attach(9);
while(!Serial); // wait for serial port to connect. Needed for native USB
}

void loop() {
// put your main code here, to run repeatedly:
IRValue=analogRead(A0); //Reads the value from analog pin (A0)
Serial.print(“IR Sensor=” ); //Display “IR Sensor=” on computer monitor (No next line)
Serial.println(IRValue); //Display IRValue on computer monitor follow by a next line
if(IRValue>110)
{
aileron.writeMicroseconds(1800);
DValue=digitalRead(9);
Serial.print(“D Value=” ); //Display “D Value=” on computer monitor (No next line)
Serial.println(DValue); //Display DValue on computer monitor follow by a next line
}

delay(500); //Pauses the program for 500 milisecond
}

Hello,

I've actually never used digitalRead() and am still fairly new to arduino, but take a look at it's reference page: https://www.arduino.cc/en/Reference/DigitalRead which says that digitalRead() returns either HIGH or LOW which should be equivalent to the int values 0 and 1, which would loosely explain your 0s across the board. digitalRead merely tells you if the pin is high or low and apparently when you're reading it, it's low.

if you want to read what angle your servo is at check this out: https://www.arduino.cc/en/Reference/ServoRead this returns the angle of your servo in degrees so it wont return 1800, but the angle of the servo nonetheless.

to my knowledge and apparently by the arduino reference page, there's no function that returns the the amount of microseconds that your servo last moved.

hope this helps! :D

Hi mspalluzzi,

nice to hear from you, thanks for the clarification.

I am actually working on an obstacle avoidance feature on my quadcopter. I am trying to make sure that the pulse width of certain value - Micro seconds (in my example codes, im using 1800 micro second) is sent to turn the servo (in my case it is the flight controller that controls the motor) through the digital pin. So lets say if my quadcopter is too near to the wall, the Arduino Uno would send a pulse width to the flight controller to hover the quadcopter backwards. Anyway in RC universe, all controls work between 1000 ms to 2000 ms with 1500 ms being the neutral just like Servo. So for my case, if I want my quadcopter to backoff from an obstacle infront of it, a 1800ms (any value greater than 1500ms would activate a rearward movement) can be send to its elevator to activate the specific maneuver.

Of cos, I'm pretty sure that the 1800 micro second would be send to the flight controller by using the following functions Servo elevator, elevator.attach(9) and elevator.writeMicroseconds(1800) and work but then if I could show it in like the serial monitor (obviously not haha) it would be great for my report.

Many thanks.

Yeah doesnt seem the standard servo library has anything for that, however...

http://playground.arduino.cc/Code/MegaServo

this library has a function to read your servo position in microseconds. Tried playing around with it a bit but couldnt get my code to compile. There's a decent amount of stuff on the web about the library though

if you can get it working, should be able to do what you want..

good luck

mspalluzzi:
couldnt get my code to compile.

Actually just got it.
Download the MegaServo library and add it to your IDE libraries like normal.
Go into your arduino library folder (mine is located in Documents/Arduino/libraries) and open up the MegaServo source file. Line 56 should say “#include <WProgram.h>”… change that to “#include <Arduino.h>” (i guess WProgram.h is for the older IDEs). and after that i used this simple code to write and read in microseconds:

#include <MegaServo.h>
MegaServo myServo;

void setup() {
  Serial.begin(9600);
  myServo.attach(9);
  myServo.writeMicroseconds(1090);
  Serial.print(myServo.readMicroseconds() - 1);
}

void loop() {
}

readMicroseconds kept giving me the value i wrote +1

Hope this works out for you!

Servo also has readMicroseconds()

Why you would use it is mysterious, it just gives back the current setting that you last wrote to it (with the granularity of the driver). You should know what you last wrote!