Help, I am absolutely confused with code!!!

TEZARM

Senior Member
main:

loop: if input2 is on then loop
high 5
pause 800
low 5

loop2: if input2 is off then loop2
high 5
pause 2000
low 5

goto main

OK.The problem I am having is finding where to put this piece of code into the above so it works.

go: if input6 is on then go '(goes to gnd)
high 4
pause 2000
low 4

I just don't know how you guys figure this stuff out. You are all geniuses.
I also only want input6 to work when input 2 is on ONLY and not do anything if input 2 is off (off being held high). Pleeeeeaaase help me with this, it is so damn frustrating.
 

Rickharris

Senior Member
It is difficult to see what you are trying to achieve here.

Generally if an input is on it is considered to be high (+5 volts) and if it is off to be zero volts.

As you want something to happen when input 2 is on and a different thing when it is off much will depend on how long the input is on/off for the way you have written your pseudo code.

If you can give some background to your problem perhaps we may be able to help better.
 

Charliem

Senior Member
Hi TEZARM,

I think what you want is Pretty simple.Put all the "If Then" Statments at the beginning of your code,Right after the Main Lable and Before the Loop Lables.
 

TEZARM

Senior Member
Hi charliem. Thanks for your help. From what Rick has said, I don't think I have explained to well. Rick. When I refer to ON, I mean to goto ground, NOT +5v. Could I maybe have a simple example. Thank you both so much for helping.
 
Hi TEZARM,
it may be better to just explain what you want the program to do at first. I am certainly no expert but it looks to me like output 5 would be on almost constantly even if the code was written correctly. Imagine if input 2 was off then the code would ignore 'loop' and go to loop2 which would put output 5 on for 2secs then the program turns off output 5 momentarily but assuming no change of state of input 2 the program would goto the top of the code again skip 'loop' and do loop2 all over again. As you can imagine the picaxe completes the instructions between output 5 switching off and going back round to switching output 2 back on again (back to start of 'loop2' in the blink of an eye, which is probably not what you wanted ?.
 

Rickharris

Senior Member
With my limited knowledge of what you want to do this may be a starting point. It tests input 2 to see if it is on first then checks pin 6 Also taking into account the state or input 2. It then checks to see of input 2 has gone off.

NOTE much will depend on how fast things happen. Th Picaxe will process perhaps 5 to 10 thousand commands per second! so it may well turn on output 5 several times before your input state changes depending on what is driving this. You may well need to play around to get the results you want.

Main:
If pin2=1 then P800 'checks input 2 to see if it is on (High)
if pin6=1 then gnd 'checks input 6 to see if it is on (High)
IF Pin2=0 then P2000 'If input 2 is off (low) then this will
goto Main

P800:
High 5
pause 500
low 5
goto main

P2000:
high 5
pause 2000
Low 5
goto main

Gnd:
If pin2=0 then Main 'checks if Pin2 is on as well (high) if not it jumps back to Main
high 4
pause 2000
low 4

goto Main
 

BeanieBots

Moderator
Have a look at the BRANCH command.
Read all your inputs and then add them together to produce a variable that represents the states. There are several ways of doing this but the simplest would be to assign each input to a BIT variable that makes up b0.
Bit0 = input1
Bit1 = input6 etc. etc
Now, b0 will 0 if all inputs are low
b0=1 if input1 is high and input6 is low
b0=2 if input1 is low and input6 is high
b0=3 if both are high.
This method can be easily expanded up to 8 inputs.
Then use BRANCH to jump to a line at the start of the code you wish to run depending on the combined state of all your inputs.

If the code you wish to run varies depending on previous inputs, then you need to build what is called a "state engine". You still use the method described, but you store in a variable the "state" that you are currently in and use a second BRANCH to jump to a different routine after the first BRANCH.

Alternatively, map your process out in a flow diagram clearly showing where you wish to test each input and what action needs to be taken. It is more than likely that you will then "see" quite clearly what you need to do.
 

TEZARM

Senior Member
Hi all. Thanks for your feedback. I am going to try and explain again alot more simpler thiS time. OK, here goes.

Input 2 is a switch. When I flick it (ON) it goes to ground. This triggers the Picaxe and sends Output 5 High for 0.8 seconds.
When I turn switch other way (OFF) it takes ground away from picaxe and then sends Output 5 High again but this time for 2 seconds.
THIS PART WORKS FINE. This is what my loop and loop2 circuit is for.

Now for the next step.

If Input 6 goes to ground it needs to trigger output 4 for 2 seconds and go high.
I want Input 6 only to be acknowledeged if the switch on Input2 is ON (going to ground.
If the switch on Input 2 is OFF (not going to ground) then if Input 6 is grounded it will not do anything.
The loop loop2 code is just to switch INPUT 2 ONLY as the switch stays grounded all the time when on and when switch IS off there is nothing at Picaxe at all.
HOW DO I Interface Input 6 into my program so far which is just the loop and loop2 part.
 

TEZARM

Senior Member
Grrrrrrrrrrrrrrrrr. Please, need more help.
BeanieBots. I do not understand your code. Flowcharts don't seem to have LOOP.
Rick. I tried your code but not work the way I need it to. It is the way the switch stays grounded when ON that is the problem. Hippy, Stan etc, where are you? Heeeeelllllllppppp.
Please help me before this Picaxe gets it's first flying lesson!!!!!!!
 

BeanieBots

Moderator
Hey, TEZARM, chill.....
I don't have time right now but I'll try to scribble down what I mean and send you an e-mail tonight. (my time).
One of the problems with a text only forum is the difficulty with explaining things that are very easy with a simple diagram.
Flow diagrams CAN do loops, just draw a line from the end back to the beginning and you have a loop. I'm suggesting you should sketch it on paper, not use the flow diagram programming environment. When you can see your program as a picture it often becomes much clearer.

Using SYMBOL might also help to make the code clearer. In your case, you are using switches that pull to 0v for "On".

Symbol On = 0
Symbol Off = 1

Then you can write code like:-
If Input1=On then ......

It would also be a good idea to use Symbol to give your inputs sensible names as well.


Edited by - beaniebots on 11/7/2005 10:18:09 AM
 

TEZARM

Senior Member
BeanieBots. What would we do without you.
Thank you sooooo much. Yes, I would really appreciate a diagram emailed to me when you get time. I do however understand what you are saying using the SYMBOL command. Thats actually a really good idea but I am not clever enough to add it all together I don't think, but I shall give it a try. I have been playing around with that other idea of yours. b0=1 etc. Huh, thats way beyond my brains capabilities. Thanks anyway though.
 

TEZARM

Senior Member
Actually, does this make any sense?

start:

symbol on=0
symbol off=1

if input2=on then ONE
if input2=off then TWO
if input6=on then THREE

ONE:
high 5
pause 800
low 5
goto start

TWO:
high 5
pause 2000
low 5
goto start

THREE:
high 4
pause 2000
low 4
goto start

The tricky part would be..

if input6=on and input2=on then THREE. (This would make sure input 6 only works if input 2 is to ground only) or would it??
Bugger, I have run out of time today to play with Picaxe anymore. I will try this tomorrow to see if it does make any sense.
 

BeanieBots

Moderator
Yes, it does make sense, but if you follow the "flow", does it do what you want?

For example, for the case case you quote regarding input 6, you could solve it in several ways:-

if input6=on AND input2=on then THREE
This is actually correct syntax and would do exactly what it says.

Alternatively, you could test input 6 as the first line after label ONE which is only jumped to when input 2 is correct.

ONE:
if input6 = on then THREE
high 5
....


Now get some sleep and go back to it in the morning ;-)
 
G

Guest

Guest
Hi Tezarm,
Are you sure that when input 2 goes (off) in the program (switch off) that output 5 only stays on for .8 sec then goes off?. As far as I can see the program will loop back to the start label and switch it back on for
a further .8 secs again and keep cycling like that. If so you could do something like if input 2 = on and b0 = 0 then one. In the label one routine you could then add let b0 = 1 so it will stop the program doing a repeat turn on next time round. Also if you do the opposite e.g. if input 2 = off and b0 = 1 (also add let b0 = 1)after the 'two' label in the program. If you do that you could also get round another problem you may have, If you want output
 
G

Guest

Guest
Sorry Tezarm I accidentally hit thepost repy button before I had finished!
If you use a 'flag' like above you could also bring ouput 4 on only when output 5 was on if thats any better, but you would have to use delay loops rather than pauses to keep the program cycling and looking for a change of state on input 6
 
Hi,
just realised how Imanaged to accidentally post my topic, I can post without logging in a username and password for some reason?
 

BeanieBots

Moderator
TEZARM, I've sent you an e-mail with a very rough sketch of a flow diagram approach and also how to assign your inputs to b0 for use in a BRANCH method. Hope it helps. Come back if it is still not clear. I'm not very good at explaining things but it really is quite straight forword once the penny drops.

Edited by - beaniebots on 11/7/2005 8:01:30 PM
 

TEZARM

Senior Member
Hi ercotronic. Nah, doesn't seem to keep repaeating itself over again at all. Works well except when you want to add more code into the loop. I will give your idea a go and see if I can get it to work. I really appreciate you helping me, thats really great.
BeanieBots. Hi, ok thanks for email and ideas. I will get back to ya when have looked over it.
 

TEZARM

Senior Member
Oops. Sorry about the late reply ercotronic. Yeh, I am using pull up resistors on inputs. They are 10k.
 

TEZARM

Senior Member
Hi ercotronic. Thanks for showing so much interest in my project. Nah, have not had a chance to get back onto that project yet. Was hoping to have a play last night but fell a sleep from working to hard through the day. I am hoping to get back into it on wedneday or thursday. Hopefully, anyway. I shall keep ya posted on the progress anyway. Thanks.
 
Top