Analog as digital I/O

Hi folks,

I am using an Adafruite motor shield V1 clone. As the shield uses all but one of the digital pins I’m using the analogue pins but I’m not seeing any responces from these devices. This is a part of the ino file:

#include <SoftwareSerial.h>
#include <NewPing.h>
#include <AFMotor.h>
#include <Servo.h>

int rxPin = 14;
int txPin = 15;
SoftwareSerial Bloot = SoftwareSerial(rxPin, txPin);

int trigPin = 16;
int echoPin = 17;

void setup(){
pinMode(rxPin, OUTPUT);
pinMode(txPin, INPUT);
Bloot.begin(9600);
Bloot.print(“Bluetooth active”);

pinMode(trigPin, OUTPUT);
pinMode(echoMode, INPUT);
}

void scanAhead(){
Bloot.println(“Scan Ahead”);

digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration/2)/29.1;

Bloot.println(cm);
}

I’m sorry if this is a bit verbose. If anybody can spot what I’m doing wrong I would be grateful to hear about it.

Thanks guys. Regards John

Your code doesn’t even compile. This can’t be the actual code that you tested:-

int echoPin = 17;
.
.
.
pinMode(echoMode, INPUT);

echoMode was not declared in this scope

When posting code, it should always be placed within

[code]code tags[/code]

.
Its not too late to edit and correct this. The </> button also generates code tags.

And analogue input pins should be fine as digital outputs. Which board are you using?

Edit: You also didn’t declare ‘duration’ or ‘cm’ before using them:

duration was not declared in this scope

cm was not declared in this scope

And you didn’t show your ‘loop()’ function. Post all of your code, so it can be copied and pasted into an IDE for checking.

I didn’t want to create a tomb that no one wanted to read so it doesn’t show the whole code. “echoMode” is a typo, it should read “echoPin”. duration and cm are int. Sorry.

jd2117:
I didn’t want to create a tomb that no one wanted to read so it doesn’t show the whole code. “echoMode” is a typo, it should read “echoPin”. duration and cm are int. Sorry.

But you still didn’t post all of your code. (Between code tags.)

Just hit >Tools >Auto Format in the IDE, then copy and paste it into a reply between code tags. (Use “Reply”, not “Quick Reply”, or the </> button might not be visible.)

Also say which Arduino board you have. Name analog pins A0, A1, not 14, 15, etc, as that is portable between different Arduinos. The Mega pin 14 is not A0.

Hi,

I have re-written the important part of the code and compiled it. I am using the UNO and I thought that if you are using A0, A1, A2 and A3, as digital pins, you had to rename them as D14, D15, D16 and D17. Is this not correct?

I am a noobie at using Forums and the Arduino UNO, so I have to thank you for your patients. I hope I’ve got it right this time. Regards John :slight_smile:

/*
Name: Testing_analog.ino
Created: 6/25/2016 4:16:36 PM
Author: John
*/

// the setup function runs once when you press reset or power the board
#include <AFMotor.h>
#include <Servo.h>
#include <NewPing.h>
#include <SoftwareSerial.h>

long duration, cm;
long maxLimit = 75;
long minLimit = 50;
int speed = 200;
int halfSpeed = 100;

int rxPin = 14;
int txPin = 15;
SoftwareSerial Bloot = SoftwareSerial(rxPin, txPin);

int trigPin = 16;
int echoPin = 17;

Servo scanServo;

void setup() {
pinMode(rxPin, OUTPUT);
pinMode(txPin, INPUT);
Bloot.begin(9600);
Bloot.print(“Bluetooth active”);

pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
}

// the loop function runs over and over again until power down or reset
void loop() {
scanAhead;
}

void scanAhead() {
Bloot.println(“Scan ahead”);

scanServo.write(90);

digitalWrite(trigPin, LOW);
delayMicroseconds(5);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);

pinMode(echoPin, INPUT);
duration = pulseIn(echoPin, HIGH);
cm = (duration / 2) / 29.1;

Bloot.println(cm);

if (cm > minLimit && cm <= maxLimit) {
Bloot.println(“Half speed”);
}

else if (cm > maxLimit) {
Bloot.println(“Full speed”);
}

else if (cm <= minLimit) {
Bloot.println(“Halt”);
}
}

You have been asked a couple of times to use code tags. These put the code in a separate window as seen in other posts.

Please go back and edit previous post and add the tags. This can be done by placing     [code]  and [/code]  around the code or use the </> icon. This makes it easier for others to read.

Weedpharma

jd2117:
I am using the UNO and I thought that if you are using A0, A1, A2 and A3, as digital pins, you had to rename them as D14, D15, D16 and D17. Is this not correct?

Not strictly necessary. You can use D14, D15, D16 and D17, but you don’t have to. A0, A1, A2 and A3 works too.

Now how about those code tags?
Please read this, and take note of item #7:-
How to use this forum
(There’s a link to it at the top of each forum section. Reading it is mandatory.)

jd2117: Why don't we just delete this post and put it in the too hard basket.

Is it really too hard for you to add code tags?

Are you so special that you don't need to use code tags when everyone else does?

If you're not prepared to put in any effort, don't expect anyone else to. After all, it's your problem and we're just trying to help.

We ask as a simple courtesy to other forum members to : Post your code in code tags so that we can be certain that the forum software hasn't mangled your code, and so that the code can be easily copied. Post all your code, so that others can replicate your problem(s) and help you fix them.

If you don't comply, I can more or less guarantee that your post will become a "tomb".

You're not calling the function "scanAhead" from your "loop" function; look at all your other function calls.