Let's Make Robots!

No Decimals

So I'm testing my ultrasonic sensor for mmy turret and I have run across a problem. This is that there are only 27 word (w0,w1,w2) varriables. So I have to use byte variables (b0,b1,b2). The ultrasonic read the range into a word variable, so then I have to convert this big number down to the size that can fit into a byte. So I used division. b0 = w0 / 100. Then I ran into another problem. The Picaxe takes the next varriable b1 to fill it up with the deciaml place. So 19.24 would be b0=19 and b1 = 2. Now I don't want this because I need to use b2 and I need all those 50 varriables. Is there a way to get rid of the deciaml places.

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

Oddbot mentioned peek and poke, however there is a ton of places to put data. You can preload data using the table command. You can make other tables using the lookup and lookdown commands. There is put and get to read and write to the scratch pad and of course, there is internal and external eeproms. If that is not enough, you can slave a picaxe to be an external eeprom via i2c as well.

readadc10 0, w0 ; the value now occupies b1:b0 so don't use those anymore
pulsin 0, 1, w0   ; different way of filling w0 with a 16 bit value

b2 = w0 / 256 ; this gives you the most important half of the word in a single byte
b2 = w0 >> 8 ; this is a fancy way of writing w0 / 256
b2 = b1         ; this also gives you the most important half of the word in a single byte

;feel free to use b0 and b1 (or w0) for something else after this

Hey Tofu, while your reading manual2 I suggest you read about variable storage and commands such as peek and poke. You can store bytes and words in memory to free up your variables.

Can you then pull the varriables out?

Guess you didn't RTFM!

If you find out the max distance that the sonar is really going to need, you can use min max as it comes in to establish a ceiling on the word variable. Now you can convert this number to 255 (the upper limit of your byte) to keep the highest resolution you can without the fear of a byte overflow after conversion.

Yes, bytes stop at 200 something. You will need to convert. I personally would avoid converting back into 1/2 of your word variable. I.e. let b0=w0/100. You can do this --you already did and got the result you wanted... b0=19 which is your answer, rounded down. However, and this just might be my personal preference but I don't like reusing variables if I can avoid it and can't imagine when I would reuse only one byte out of a word. I would rather use let b2=w0/100 to keep things tidy.

 

You should try to work without negitive numbers sometime!

 

BTW Tofu--

When I said that w0=b0 and b1, I mean that all of the word and byte variables are the same. You can have b0-b54  --or--  w0-w27 not both. Not both because they are the same thing.

Then how can I make bytes hold big numbers from my ultrasonic sensor. because I know it goes over 300. but it stops somewhere at 200 something.

Yup no floating point. I would not know how that feels muhahah!! (rubs arduino).