Programming issues

new to the programming world but have been learning.  I have a PING)) Ultrasound sensor on my robot and im just trying to get it to move forward than stop if anything comes in front of it. First i started by using a program to make sure the sensor was working (which it is) and to test the values. This is what i started with:

Main:

 LOW PORTC 0

 HIGH PORTC 0

 PAUSE 1

 LOW PORTC 0

 LET DIRSC = %00000000

 PULSIN 0, 1, W0

 debug

goto main

Than i wanted to add the moving and stoping aspect so this is what i put down:

Main:

 LOW PORTC 0

 HIGH PORTC 0

 PAUSE 1

 LOW PORTC 0

 LET DIRSC = %00000000

 PULSIN 0, 1, W0

 debug
 
 if b0 > 60 then
 
 goto move
 else
 goto stp
 end if
 goto Main

move:
high 4 low 5 high 7 low 6
return

stp:
low 4 low 5 low 6 low 7
return

 

When i use this program its like the robot has a delay. It takes it about 30 seconds to take 10 readings and the values change on a very big delay, it wil be at 150 than i place my hand in front and slowly move forward and it willl run into my hand and keep going before it stops. also ill put it onto the floor and it will stop without anything infront of it. Anyone see any errors or have any ideas??

I’m not sure what you’re doing

I don’t know Picaxe basic so I have limited understanding on what you are doing and why you are doing some things. Adding comments to what is happening is always a good idea (read mandatory) when programming, especially if you want others to understand your code as well. Since you are new to programming you can teach yourself proper structured programming so that bad habbits don’t have a chance, you’ll thank yourself for that when debugging or rewriting programs.

Anyway, I don’t understand why you have “if b0 > 60” I assume that’s a Picaxe basic thingy so it probably is correct.

Then you goto move: high 4 low 5 etc. I would expect a comma between them, but again that can be a Picaxe thingy. After that you have a “return”. “goto” and “return” don’t match, you “return” from a “gosub” command. Again this could be a Picaxe thing, but if that is true then Picaxe basic is odd!

Your first problem is the variable you are checking.

Your original code reads the time measure in to the variable W0. Your second part of code checks b0. With PICAXE W0 is a Word variable taking up two Bytes or 16 bits. b0 is the bottom half of W0. You should change the b0 to W0 and you should get better, faster results.

I would suggest you use the keyword Symbol to help you avoid such things in the future. Symbol will let you give a sane name to variables, pins, or constants. http://www.picaxe.com/BASIC-Commands/Variables/symbol/

TANGENT

W0 (w0 is the same) is made up of b1 and b0 and in many cases bit15 through bit0. I am listing them backwards intentionally. Reading binary is done right to left. Computers work with binary. When you check b0 after having put a number in W0, you will only see part of the number. The value stored in W0 may not even contain a number less than 60 in decimal format (ie W0 does not have to contain DEC 159 to actually get a “hit” on if b0 < 60 then)

Good catch on the goto/return

I would have to guess they will function, but, the manual does not specifically call that out as a possibility. While I LOVE spaghetti, I have been taught that it is no way to program. :smiley: With that bit of teaching, GOTO kinda got a bad reputation. :slight_smile: PICAXE BASIC has do/whilie/until loops. If tutorials taught better(?) programming, main: … goto main would not be as common. :slight_smile:

Spaghetti

Goto can be useful when you need to squeeze that extra bit of speed out of a program. (Declaring variables helps even more, but I was lazy.) The following piece of code is faster with gotos than without them.

(Yes it’s a real Quick basic, still have a copy on my laptop. lol)

CLS
t = TIMER

FOR a = 0 TO 1000
FOR b = 0 TO 1000

10 DO
c = c + 1

IF c = 1 THEN d = c: e = COS(d) / SIN(d) + TAN(d): GOTO 10
IF c = 2 THEN d = c: e = COS(d) / SIN(d) + TAN(d): GOTO 10
IF c = 3 THEN d = c: e = COS(d) / SIN(d) + TAN(d): GOTO 10
IF c = 4 THEN d = c: e = COS(d) / SIN(d) + TAN(d): GOTO 10
IF c = 5 THEN d = c: e = COS(d) / SIN(d) + TAN(d): GOTO 10
REM etc etc etc
IF c = 20 THEN d = c: e = COS(d) / SIN(d) + TAN(d)

LOOP UNTIL c = 20
c = 0

NEXT b
NEXT a

PRINT TIMER - t

Would there be a speed change

if you were to instead change the loop variables to their max values instead of goto?

Not sure what you mean.

It’s just a piece of code that does nothing other than keeping the CPU busy. There are probably ways to do it faster, but I had to write a similar piece of code during my education and to annoy my teacher (who was bragging about the speed of the then used 4GL) I wrote the same piece of code in good old GW Basic which executed the code way much faster.

Anyway, when diving into the world of microcontrollers I noticed when reading about Picaxe that the stigma that basic is just a toy language still exists. Odd really, modern basic dialects are fast in both execution and development speed. Too bad the guys at Picaxe made Picaxe basic an interpreter instead of a compiler effectively feeding the old stigma that basic is slow and not to be taken seriously. In fact there were only two reasons for me to not buy Picaxe: My Arduino mega was less than £10 including shipping and I’m a masochist in that I feel the need to finally master C and the C like Arduino syntax is a start towards that goal.

Since I’m new to the
Since I’m new to the programming world do you guys have any suggestions on languages I should start learning and where (either internet sites or books) I should look into it? I’ve been reading basic electronic and robotics books, but the programming part is where I struggle and the codes they give look almost like gibberish so I don’t do much but copy basic things I understand from other codes

Stick with BASIC

You have a PICAXE. No reason to switch gears. BASIC is a good enough language, and, logic is the thing you need to pick up.

Admittedly, BASIC doesn’t have functions, so, there will be something new to learn with other languages, but, otherwise, really, the other languages are just different syntax. You might do well to pick up some kind of way to plan programs; flowchart or psuedocode come to mind.

In the future you may want to consider something like arduino or Python. If you learn arduino, you shoudl be in good shape to understand C/C++ when/if you decide to change to some other processor you should be in good shape to write software for them. Python is only a suggestion if you decide to switch to something like a computer.

Have I rambled enough? :smiley:

Sice im new to programming

Sice im new to programming world do yu guys know any sites or books i could look into that would teach me programming from the basics  so i can understand thing like w0 and b0 rather than just copying similarities on other programs? I’m open to learning any languages

haha thank you guys very

haha thank you guys very much! i think im going to look into getting into the aurdino systems because as i look around that seems to be a popular and useful system and i’ll look into the C language as well

Stick with basic

Stick with basic as Birdmun already said.

The Arduino has only one real benefit over Picaxe: it’s a bit faster, that’s it. The downside of Arduino is that it’s harder to program and if you already have problems understanding basic, then learning the Arduino code will be a lot harder. By buying an Arduino and by trying to understand C you will only confuse yourself making it even harder to understand the bare basics of programming. You don’t learn nothing by trying everything, you learn by sticking to one thing and try to master it. 

Copy code, try to understand what it does and experiment with it. When you feel confident enough try writing the same code yourself and figure out what you did wrong, because you will make errors, a lot of errors! It takes time, programming isn’t something you learn in an afternoon, because if that were true you didn’t need to have a science degree when you want to become a programmer for a living.

 

very good point! thank you

very good point! thank you very much, i will be taking that advice!

I am curious.

Have you downloaded the 4 manuals that Rev-Ed has for PICAXE? I admit they won’t tell you how to program, but, they will list all the commands/keywords that are available, and, they will tell you how to interface with various bits of hardware.

Worst case, you can always come here and ask after you have tried to get something going.

**

You asked about w0 and b0. Computers and microcontrollers work with bits (0/1). There are 8 bits to a byte and in the case of some microcontrollers there are 2 bytes to a word. Binary allows a computer to count while only using a HIGH or a LOW. 8 bits can count from 0 (00000000) to 255 (11111111), for reference 1 is 00000001. The bits are numbered from right to left starting with 1 and doubling value for each following position (128 64 32 16 8 4 2 1). 16 = 00010000.

With the code you listed above, you were reading the time between trigger and echo return in to a 16 bit variable (W0). Later in your code you use a conditional statement on only the bottom half of that word variable; there are some pieces of information that I would suggest you gleen from the manuals I mentioned. Suffice it to say W0 is actually made up of b1 and b0. Your if statement is only looking at half of the data of W0.

Your other problem, if I recall correctly, is W0 will actually contain the time for the “PING” to travel out and back. You need to do a bit of math to convert time in to a distance. Since PICAXE chips don’t do floating point (numbers with decimal points), you need to first multiply W0 by 10. Then, you may divide by 58 for centimeters or 142 for inches. Next up you need to consider how big your distance could be. I think the sensors can/should be good to 3 meters or 300 centimeters. 300 > 255. You will still need a word variable to hold your distance measurement. Admittedly, you could limit your allowed values to make them fit in a byte, but, that is more coding.

I will gladly post some code that should work for you. Your call.