Code

 

I'm working now on the code for my new robot and it's not doing what it has to do.

It's supposed to drive around and detect walls, turn and drive furthur.

Do you guys know what's wrong?

This is the code:

 

Symbol danger = 10
Symbol servo_turn = 700
Symbol turn = 500
main:
readadc 0, b1
readadc 1, b2
if b1 <danger then
gosub nodanger
else gosub whichway
end if
goto main
nodanger:
high 5 : high 7 : low 4 : low 6 'vooruit
return
whichway:
gosub totalhalt
gosub servoturn
pause servo_turn
readadc 0, b1
readadc 1, b2
if b1<b2 then
gosub lturn
else
gosub rturn
end if
return
totalhalt:
low 4 : low 5 : low 6 : low 7
servo 0, 150
servo 1, 150
wait 1
return
servoturn:
servo 0, 250
servo 1, 50
return
lturn:
high 5 : low 4 : low 6 : low 7
pause turn : gosub totalhalt
return
rturn:
high 7 : low 4 : low 5 : low 6
pause turn : gosub totalhalt
return

 

 

https://www.youtube.com/watch?v=TEb1iyNma7E

I did not read the code, but

I did not read the code, but can you tell us what it is supposed to do and what it is actually doing?

Video

I have included a video now

What it is doing wrong

there’s nothing in front the robot so it has no reason to look to the side’s and only one wheel is turning.

but thanks for the tips.

Check your danger value?

I haven’t used those sensors but on the sharps the lower the number the further the distance. 10 seems pretty low to me. Are you sure you not measuring the longest distance instead of the shortest?

Chuck gave some good ideas too. And you really should use whitespace in your code to make it more readable. And comments helps not only others reading it but yourself if you don’t look at it for a while.

Symbol danger = 10
Symbol servo_turn = 700
Symbol turn = 500

main:
    readadc 0, b1                               ; read left sensor
    readadc 1, b2                               ; read right sensor
    if b1 <danger then                       ; if nothing in front of left sensor
    gosub nodanger                          ; go forward
    else gosub whichway                 ; otherwise check which way is clear
    end if
    goto main
nodanger:
    high 5 : high 7 : low 4 : low 6     ; set motors forward
    return
whichway:
    gosub totalhalt                             ; stop
    gosub servoturn                          ; scan   
    pause servo_turn                        ; wait for servo to reach it’s position
    readadc 0, b1                               ; check if clear or obstacle on left
    readadc 1, b2                               ; check if clear or obstacle on right
    if b1<b2 then                                ; if way is clearer to left
    gosub lturn                                   ; then turn left
    else
    gosub rturn                                   ; otherwise turn right
    end if
    return
totalhalt:
    low 4 : low 5 : low 6 : low 7          ; stop
    servo 0, 150                                   ; centre servo’s 0 and 1
    servo 1, 150
    wait 1                                               ; wait a second till they’re centred
    return
servoturn:
    servo 0, 250                                    ; scan to the left   
    servo 1, 50                                       ; scan to the right   
    return
lturn:
    high 5 : low 4 : low 6 : low 7        ; turn on motors to turn left
    pause turn : gosub totalhalt        ; turn for half a second then stop
    return
rturn:
    high 7 : low 4 : low 5 : low 6        ; turn on motors to turn left
    pause turn : gosub totalhalt        ; turn for half a second then stop
    return

I think I wrote (some part

I think I wrote (some part of?) that code - it looks like my old standard labels :slight_smile:

But I do not think I can recognize the mechanics of the robot?!

It looks like a strange mashup, and I think this is what you need to do:

Do not use my code, but delete everything.

Make 1 thing work! Very, very simple thing, make it work 100%, and have no code but the code to make that one thing work.

When done, add one line of code, to make the next thing work. Only 1 thing!!

Move on from there - and if stuck again, you are only stuck with 1 thing, and you can explain to us how you are stuck with that single thing, in every detail. And we need that in order to help.

But I think that if you take the approach of making one thing working at a time, you will not need help the same way :wink:

Note; Wen I wrote the original code, and when ever I, and or any body are making something that works, this is excactly how we do it. It’s the only way to write code for a robot.

good catch

Good on ya chuckie. That will make a difference. I didn’t see how you adjust the distance with it though. Is there a pot on the back? Those rings around it?

Rings

The rings around the sensor are mounting rings.

And in the back ther’s a little screw which you can adjust the distance.

10

Yeahh 10 seems to be low.

But if there’s nothing infront the sensor the value is around 180 and if there’s somthing infront of the robot it is around 2.

Like Chuck said below, these

Like Chuck said below, these sensors are digital. So there is no reason to have them hooked up to analog inputs. I would just connect them to digital inputs and you will get a 0 or 1 reading. Right now you are treating them like they are analog.

Like the man said

there is only 0 and 1. You have to test the b1 and b2 if it is 0 then avoid. If it is 1 then ok all is clear. No < or > just :-

Symbol danger = 0

Main:
    servo 0 , 150 : servo 1 , 150
    if b1 = danger and b2 = danger then gosub whichway
    else gosub nodanger
    endif
    goto Main
whichway:
    gosub totalhalt
    gosub servoturn
    pause servo_turn
    if b1 = danger and b2 = danger then gosub reverse
    else if b1 = danger gosub rturn
    else gosub lturn
    endif

That’s wrong. This sensor is

That’s the wrong code for this sensor or the wrong sensor for this code. This sensor is more like digital. You can set the range with this little screw to your danger distance. The output of this sensor is just  “0”  or  “1”  …yes kind of digital. The range you set with this screw just triggers the output to  “1” . There is no analog output related to the distance.

You just have to check if b1 is “1” or “0” and b2 is “1” or “0”

I would say …

you might want to consider a couple of tests, if the sensors will be treated as digital.

if b1 = danger AND b2 = danger then gosub whichway

will run into something if b1 OR b2 see something too close. You should therefore check and respond to individual sensors. Along the lines of:

if b1 = danger OR b2 = danger then gosub whichway 

my mistake

good point