PDA

View Full Version : scale code (need help)



grkblood13
04-18-2007, 02:13 AM
what i attempting to do is take a value from a scale and put it into 1 of three buckets. the bucket that the number will go into is the bucket with the lowest present number.

' {$STAMP BS2}
' {$PBASIC 2.5}
max_seats VAR Word 'test run value
sect_pass VAR Byte
sect_pass1 VAR Byte
sect_pass2 VAR Byte
sect_pass3 VAR Byte
total_weight VAR Word
keycount VAR Nib
man VAR Nib

Aircraft_Ident VAR Word
Aircraft_pass VAR Word
Current_weight VAR Word
Aircraft_weight VAR Word
Yes_No VAR Bit
aircraft_weight = 2000
sect_pass = 2000/3
total1 VAR Word
total2 VAR Word
total3 VAR Word
begin:
HIGH 9
HIGH 10

IF IN9 = 0 AND IN10 = 1 THEN calc 'tells program to weigh'
IF IN9 = 1 AND IN10 = 0 THEN standby 'someone is walking through scale backwards'
IF IN9 = 1 AND IN10 = 1 THEN begin 'loops tripwire'
standby:
DO
IF IN9 = 1 OR IN10 = 1 THEN begin 'this is just to make sure there are no glitches'
LOOP

calc:
location CON 0
i VAR Nib
total VAR Word
'Aircraft_Ident = 0
'Aircraft_Ident = (INS & $01FF)
PAUSE 1000 'delay to compensate for time to get on scale'
total = 0
FOR i = 1 TO 15
total = total + (INS & $01FF)' - Aircraft_Ident 'takes reading from IN0 - IN8'
PAUSE 100 'this delay is because the converter is running at 10 Hz'
NEXT
total = total / 15
WRITE location,total
PAUSE 500

'DEBUG DEC total

second:
HIGH 9
HIGH 10
DO
IF IN9 = 1 AND IN10 = 0 THEN add 'adds weight to bins'
IF IN9 = 0 AND IN10 = 1 THEN begin 'starts over because someone stepped off scale'
IF IN9 = 1 AND IN10 =1 THEN second
IF IN9 = 0 AND IN10 =0 THEN second
LOOP
END
RETURN

add:
pit1 CON 0
pit2 CON 0
pit3 CON 0
total_weight = 0

DEBUG CR, DEC total1
DEBUG CR, DEC total2
DEBUG CR, DEC total3
IF (total1 < total2) AND (total1 < total3) THEN add1 'add weight to pit1'
IF (total2 < total1) AND (total2 < total3) THEN add2 'add weight to pit2'
IF (total3 < total1) AND (total3 < total2) THEN add3 'add weight to pit3'
IF (total1 = total2 = total3) THEN add2 'they equal so add to pit2'
IF (total1 = total2) THEN add2 '(1&2 is less than 3)'
IF (total1 = total3) THEN add1 '(1&3 is less than 2)'
IF (total2 = total3) THEN add3 '(2&3 is less than 1)'

add1:
IF sect_pass1 > sect_pass THEN check1
total1 = total1 + total
total_weight = total_weight + total1 'totals weight
'SEROUT 11,84, [$0C, "section 1 =", DEC total1]
DEBUG CR, "section 1 ="
PAUSE 1500
DEBUG DEC total1
WRITE pit1,total1
DEBUG CR
sect_pass1 = sect_pass1 + 1 'increments seat counter
IF total_weight = aircraft_weight THEN full 'plane has reached max weight
IF total_weight > aircraft_weight THEN full
GOSUB begin

add2:
IF sect_pass2 > sect_pass THEN check2
total2 = total2 + total
total_weight = total_weight + total2 'totals weight
'SEROUT 11,84, [$0C, "section 2 =", DEC total2]
DEBUG CR, "section 2 ="
PAUSE 1500
DEBUG DEC total2
WRITE pit2,total2
DEBUG CR
sect_pass2 = sect_pass2 + 1 'increments seat counter
IF total_weight = aircraft_weight THEN full 'plane has reached max weight
IF total_weight > aircraft_weight THEN full
GOSUB begin

add3:
IF sect_pass3 > sect_pass THEN check3
total3 = total3 + total
total_weight = total_weight + total3 'totals weight
'SEROUT 11,84, [$0C, "section 3 =", DEC total3]
DEBUG CR, "section 3 ="
PAUSE 1500
DEBUG DEC total3
WRITE pit3,total3
DEBUG CR
sect_pass3 = sect_pass3 + 1 'increments seat counter
IF total_weight = aircraft_weight THEN full 'plane has reached max weight
IF total_weight > aircraft_weight THEN full
GOSUB begin

check1:
'SEROUT 11,84, [$0C, "section 1 full"]
DEBUG CR
DEBUG CR, "section 1 full",CR
IF sect_pass2 < sect_pass3 THEN add2
IF sect_pass2 > sect_pass3 THEN add3
IF sect_pass2 = sect_pass3 THEN add2

check2:
'SEROUT 11,84, [$0C, "section 2 full"]
DEBUG CR
DEBUG CR, "section 2 full",CR
IF sect_pass1 < sect_pass3 THEN add1
IF sect_pass1 > sect_pass3 THEN add3
IF sect_pass1 = sect_pass3 THEN add1

check3:
'SEROUT 11,84, [$0C, "section 3 full"]
DEBUG CR
DEBUG CR, "section 3 full",CR
IF sect_pass1 < sect_pass2 THEN add1
IF sect_pass1 > sect_pass2 THEN add2
IF sect_pass1 = sect_pass2 THEN add1

full: 'stops program when plane is full
SEROUT 11,84, [$0C, "plane has reached weight limit"]
DEBUG CR
DEBUG CR, "plane has reached weight limit"
END

all that it does is constantly add to bucket 2. i did assign numbers into the variable as a test run with total1=2 and total2= 4. when i did that it added to total3. does anybody know the problem?

Post Edited (grkblood13) : 4/17/2007 6:19:30 PM GMT

allanlane5
04-18-2007, 02:44 AM
Wow. Well my first response is "welcome to spaghetti-code city". Man you have a lot of GOTO's in there.

Secondly -- you set Pit1, Pit2, and Pit2 all to point to EEPROM location zero, so they'll be over-writing each other.

Third -- you DO use a 'GOSUB begin' -- which should probably be a GOTO, since 'begin' never has a 'RETURN'.

Fourthly -- in a couple of places you check if aircraft_weight is equal to a value, then if it's greater than a value. You can do that in one statement.

So -- it looks like you're loading an airplane. Each passenger gets weighed, and depending on which part of the plane is lightest, the passenger gets sent to that part of the plane.

Why you're using the WRITE keyword (which writes to eeprom, which only allows a million writes to a location before you can't write that location any more) I can't tell, since you never READ the eeprom or send out the results.

Post Edited (allanlane5) : 4/17/2007 6:54:39 PM GMT

allanlane5
04-18-2007, 03:46 AM
Okay, so I think I finally understand your policy.

You're trying to put the next passenger into the lightest section of the plane.
So if total1 < total2, AND total1 < total3, THEN, the first section is lightest, stick them there.
If that's NOT true, then
IF Total2 < Total1 AND total2 < total3, THEN, the middle section is lightest, stick them there.
If THAT is not true, then
Total3 MUST be lightest?

I'll have to do a truth table for that, but what you're doing should work.

grkblood13
04-18-2007, 03:55 AM
allan, i got it to work. thanks though. the problem was this line...
IF (total1 = total2 = total3) THEN add2 'they equal so add to pit2'

it actually had to be...
IF (total1 = total2) AND (total1= total3) THEN add2 'they equal so add to pit2'

grkblood13
04-18-2007, 03:59 AM
ha, i jsut read you first response. this code has been messed with a lot. the write isnt even necessary i dont. that was done a while ago, but yea, i agree with you. there a lot of stuff on here that could more than likely be written better. im just not messing with it.

ocne i turn it in torromow ill post the entire code. this is about half of it. i dont think its too bad though. i had to teach myself and it was the first time using basic so...I guess that explains anything.

allanlane5
04-18-2007, 04:00 AM
Very good. Glad you got it to work.