newbie help needed. check my line following robot code and circuit

Hi all

recently picked up an arduino uno and sparkfun experimenters kit for my son. we are both pretty much total newbs when it comes to writing code in "c" and also fairly new to electronics too.

We have used the attached circuit line drawing, except we are using off the shelf IR sensors (configured the same as the diagram). The sketch we are using is attached too.

We are using 9v to power motors and the motor circuit via a bread board, IR sensors are using 5v through the Uno.
So far we have control of servo motors using transistors to switch the servos.
we have been able to determine the analogue sensor value with an analogue read/ serial print sketch. this value has been used to trigger the motor control circuit (attempted too).

at this point, we have IR sensors returning a change in value when passed over a black tape line, we have motor circuit working when tested independently of the arduino, but as yet, we cannot get motors to respond to IR sensors

any help with code, or circuit trouble shooting would be great.

Also, keep it simple. We've learnt a stack, and have done a lot of systematic trouble shooting, but at the moment we're stumped.

Line_Follower_Robot_Code_2.0.ino (836 Bytes)

line-follower.png

Start slowly kpking. If you do not yet have a circuit that makes the motor respond to the light sensor, dial it back. The fact that your sensors respond as expected is a great start!

Now make a circuit that changes the motor speed and direction respond to a change in voltage. In fact work out each part of your project independently, then combine them. This way you will always have a baseline that you can go back to when he next step of complexity does not work as expected. When you start with too many variables, it's difficult to isolate what works from what doesn't.

Baby steps lead to big strides slowly, but surely.

Thanks Chris

Baby steps is definitely my approach here.

Trying to learn, and teach my 13yr old at the same time while keeping him keen to persevere with baby steps is a lot to juggle.

Troubleshooting is something I do a lot of with my work.
Electronics, not so much.

I'm fairly confident the physical hardware is correct and working as expected.
IR sensors respond and communicate with arduino.
transistors and motors work as expected too, but not controlled by arduino as yet.

The code I'm less confident with, not convinced we've got the arduino talking to the servo control circuit yet . I think i understand the logic, Learning a new language and translating it to code is a challenge.

How can I test if we're getting an output at pin 11&12?

Can you post your code, please?
Use code tags.

I attempted to attach it to my OP if that helps?

Wont be able to repost the code until I can fire up the laptop tomorrow (using smart phone now )

kpking:
I attempted to attach it to my OP if that helps?

No, I'm afraid it doesn't because I'm using a phone too.

Will post code in the morning

Cheers

kpking:
Will post code in the morning
Cheers

I'll save you the trouble. Here it is:-
(I formatted it, but didn't change anything else.)

int leftInput = A3;
int rightInput = A4;
int leftMotor = 12;
int rightMotor = 11;
int leftValue = 0;
int rightValue = 0;

void setup(){
    pinMode(leftMotor, OUTPUT);
    pinMode(rightMotor, OUTPUT);
}

void loop(){
    leftValue = analogRead(leftInput);
    rightValue = analogRead(rightInput);

    if(leftValue < 790 & rightValue < 790){
        digitalWrite(leftMotor, HIGH);          // GO
        digitalWrite(rightMotor, HIGH);
    }
    else if(leftValue > 790 & rightValue > 790){
        digitalWrite(leftMotor, LOW);           // STOP
        digitalWrite(rightMotor, LOW);
    }
    else if(leftValue > 790 & rightValue < 790){
        digitalWrite(leftMotor, LOW);           // TURN LEFT
        digitalWrite(rightMotor, HIGH);
    }
    else if(leftValue < 790 & rightValue > 790){
        digitalWrite(leftMotor, HIGH);          // TURN RIGHT
        digitalWrite(rightMotor, LOW);
    }
}

The first thing I noticed is that this:-

if(leftValue < 790 & rightValue < 790)

should look like this:-

if(leftValue < 790 && rightValue < 790)

&& not &

Same goes for all other similar lines.

Thanks Steve

hopefully its as simple as that

Cheers

OldSteve:
The first thing I noticed is that this:-

if(leftValue < 790 & rightValue < 790)

should look like this:-

if(leftValue < 790 && rightValue < 790)

&& not &

Same goes for all other similar lines.

A little known fact is that you can now also use the words "and" instead of "&&", and "or" instead of the even more wild looking "||". It's an official part of the C++ language. For example,

if(leftValue < 790 and rightValue < 790)

aarg:
A little known fact is that you can now also use the words "and" instead of "&&", and "or" instead of the even more wild looking "||". It's an official part of the C++ language. For example,

if(leftValue < 790 and rightValue < 790)

Wow, really?
I just tried it and, sure enough, it compiles.

    bool a = true;
    bool b = true;
    bool c = false;    
    if(a == true and b == true)
        c = true;

I'm in shock. I learned C++ about 15 years ago, and haven't kept up with the standard since then. I'd never heard of this before in C++.
(If I'd seen someone do it in their code, I would have corrected them. :blush: )

I used PICBasic Pro for a long time, and it allows "AND" and "OR", or "&&" and "||", but I never thought I'd see it in C++.

Thanks for the tip. That one's gotta be worth a point. :slight_smile:

Happy to help OldSteve :wink:

Wait! What!

Made some progress this morning. Fixed up the && error, but no change.

Made some changes to the code after deciding the sensor value used in the if else statements was too close to what sensors were actually returning. No margin for errors.

Also while tidying the wiring a little I found a mistake in IR sensor wiring.

I used some LEDs to help trouble shooting by providing a visual indicator of High output from pin 11,12.

I now know our IR sensors are working, and our code appears to be working, LED indicators (in place of motor circuit) are behaving as expected.

I also identified a blown transistor in the motor control circuit. Trip to parts store is needed before we can continue.

Thanks for helping

kpking:
Happy to help OldSteve :wink:

Ha ha. We're all learning something.

Made some progress this morning. Fixed up the && error, but no change.

That would bring you one step closer at least. Previously, it was a bitwise "OR", ORing 790 with "rightValue", then if "leftValue" was smaller than the result, which was also smaller than 790, your 'if' statement was 'true'.

Keep us posted when you get your replacement parts.

Cool.
Definitely learning, so is my lad so all good. He'll make me look like a dunce in no time with this stuff.

Will post results.

Cheers

aarg:
A little known fact is that you can now also use the words "and" instead of "&&", and "or" instead of the even more wild looking "||". It's an official part of the C++ language. For example,

if(leftValue < 790 and rightValue < 790)

That is SO COOL!!! Thanks aarg, who knew? (Well, you did for one.)

OldSteve:
The first thing I noticed is that this:-

if(leftValue < 790 & rightValue < 790)

should look like this:-

if(leftValue < 790 && rightValue < 790)

&& not &

Same goes for all other similar lines.

I'm not so sure - the expressions evaluate to true (1) or false (0), so 1 & 1 = 1, and so on.
I feel an experiment coming on . . .

AWOL:
I'm not so sure - the expressions evaluate to true (1) or false (0), so 1 & 1 = 1, and so on.
I feel an experiment coming on . . .

Hmm, might be interesting.
I'd like to hear the result if you don't mind.

Perhaps I should have said "I think you meant to do this '&&'.". :slight_smile:

@kpking, I'm still pretty sure that you really meant "&&", or "and", (thanks aarg), but it looks like you've opened a can of worms. :smiley:

I just did a test. It really does work, but I don't understand why.
In this, the output to the serial monitor is "GO":-

int rightValue=100;
int leftValue=789;
if (leftValue < 790 & rightValue < 790) // Equiv to (789 < 4 < 790) Isn't it?
    Serial.println(F("GO"));
else
    Serial.println(F("NoGo"));

But "100 & 790" equals 4.

So the comparison is the equivalent of:-

if (789 < 4 < 790)

Which should return false. How does this work? I'm really confused now.
I know I MUST be doing something wrong, but can't see what it is....

Operator precedence

AWOL:
Operator precedence

Aha. Thank you. It's that simple. I was scratching my head wondering.
I think I'll stick to the conventional method. :slight_smile: