Connect with us

Possible help programming a Picaxe 08?

Discussion in 'Microcontrollers, Programming and IoT' started by irishluck, Oct 14, 2015.

Scroll to continue with content
  1. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    So I have a project I am trying to build but still learning about the Picaxe software programming.
    I am just getting into programming and someone had pointed me to this Picaxe IC chip for my project.
    Theres alot to learn about it obviously but needing alot of help

    I attempted to start to write some of the programming which sort of works but not really ha

    Ive drawn out a schematic of what I'm building and need some help

    Basically I need C.1 to always be high.
    Between C.1 and C.2 will be a LDR sensor. A laser will always be pointing at this.

    When the light in broken it should set C.2 as a high.
    When C.2 is high I need it to activate C.4 as a high which will turn a relay on for about 15 seconds.
    After 15 seconds, I need it to make C.4 and C.2 as a low again which will turn off the relay and start over again.

    This is how far I somewhat got...

    main:
    high b.1
    if input2 = 1 then high 4 endif
    if input2 = 0 then low 4 endif


    LDR:
    wait 3
    if input4 = 1 then low 4 low 2 endif
    goto main

    [​IMG]
     

    Attached Files:

  2. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
     
  3. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Well I didn't think about that. That I could just tie the LDR to the source voltage. So then basically I just need 1 lead of the LDR connecting to pin 1 so when the beam is broken it can turn on a 2nd pin triggering the relay.

    I haven't been reading any pdf files. Ive been trying to watch the youtube clips on programming the Picaxe and also reading the Picaxe manual.
     
  4. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    The PDFs are the help files. While in Picaxe Editor 6 click File then Help. You'll then see a list of PDFs ..
    Manual 1 "Getting Started"
    Manual 2 "Basic Commands"
    Manual 3 "Interfacing Circuits"
    Manual 4 "Flowcharts"

    Manuals 1 - 3 are the most important. For typical LDR use see Manual 3 page 29 but for what you're trying to do you could use a Phototransistor as a switch. This way you're not dealing with analog input. Also see Manual 3 page 8 for Relay interfacing.

    Cheers,
    Chris
     
  5. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    Here's a tip for you. The pdf help files contain sample code for every command, directive, statement, etc. Usually you can simply copy & paste the sample code into the picaxe editor with no issues. However be aware of code snippets that include ASCII text. This requires the text to be enclosed in quotation ("Hello World") marks. The pdf quotation marks are not recognized by the editor and will throw an error when doing a syntax check, running a simulation or downloading the program. The fix is simple.. Just remove the quotes and type them back in.

    Cheers,
    Chris
     
  6. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Appreciate the tip.

    I just really new at programming and coding so its like learning Chinese to me lol
    Just trying to get this project I have Im working on done as soon as possible

    Is there a command that you might be able to help me with?

    Its a command that would constantly watch pin 1 to see if its set high and if so then would goto the next commands I set.
    Im still trying to read the different commands but theres a lot lol
     
  7. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    OK, we'll start with some basic code.

    When you run this in the 'Simulator' click C.1 (Animated Chip Image) with your mouse. This will bring C.2 High. Click C.1 again and C.2 will go Low. The state of the pins will be simulated by changing pin color. A clicked pin will change from light Grey (PinC.1=0) to Yellow (PinC.1=1). An output pin will change from dark Grey (Low) to Green (High).
    Code:
    ;Picaxe Editor Version:  6.0.8.0 (Beta)
    ;Chipset: PICAXE 08M2
    ; *******************************
    
    Main:
       If PinC.1=1 Then
            High C.2
       Else
            Low C.2
       EndIf
    GoTo Main
    Chris
     
    Last edited: Oct 19, 2015
  8. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Ok okay. I didn't know you could click on the pins on the simulator to turn them off and on. Good to know.

    So the Else command is used to test multiple input pin variables so it can execute what to do next
     
  9. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    I think I also got the LDR backwards. I always thought that these photocells use light to block the current but the light actually allows the current to pass.

    That being said, i think I came up with a code and it seems to work. Let me know what ya think.

    Code:
    main:
        if input1 = 0 then high 2 endif
        if input1 = 1 then goto main
       
    
    LDR:
        high c.2
        wait 5
        if input2 = 1 then low 2 high 1 endif
        reset
    It seems to work.

    Except when blocking the LDR it doesn't do anything. My LDR holds at 5.07v when not blocked. When I block it the value changes to 4.65v i do believe. I think I found a code to use this that will watch for the values so that if it goes below 5v that it will turn on pin 2. Its the INPUTTYPE mask code.

    I also need a little input on an IC chip. Need 12vdc to turn on that relay on and Im only getting 5v from the IC chip. Found this on digikey.com a ST662ABD-TR. http://www.digikey.com/product-detail/en/ST662ABD-TR/497-6541-1-ND/1865349

    Looks like it will convert 4.5v(min) to 5.5v(max) into a 12v source.

    Last thing im looking into on the coding is a safety feature.
    I want to incorporate a code into the Picaxe so if say it trips on like 5 times in 2 minutes I want it to turn off and I will have a red LED that turns on when that happens. I'm still reading into the manuals for that code at the moment.
     
    Last edited: Oct 19, 2015
  10. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    No not quite. In that code the "If" statement tests if C.1 is high (= logic 1) or low (= logic 0). The "Then" statement specifies what to do if it is high (= logic 1). The "Else" statement is an instruction of what to do if C.1 is low (= logic 0).

    EDIT: I should mention that in the world of programming (Basic Languages) the "If" and "Select Case" statements are considered to be the most powerful and most often used code block.

    Learn to use the simulator to familiarize yourself with the Picaxe programming environment. It's beats the hell out of always downloading to the chip to test your code. It's a hell of a lot faster too!;)

    Capice?
    Chris
     
    Last edited: Oct 19, 2015
  11. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Oh I've been testing it with the simulator, which is btw a really cool feature with that software.
    Ill mess around with the "else" statement to familiarize myself with that. I see what your saying now about the difference between the "then" and "else" statements though.

    Thanks for the input!

    About the rest of my post that I put above your comment, is that "INPUTTYPE mask" statement sound like that would work with the voltage differences from the LDR?
    When I read about it, it looked like what I needed. Going to try it out.

    EDIT: I correct myself on the "INPUTTYPE mask" statement. Fro what Im reading its mainly used for logic highs and lows.
     
    Last edited: Oct 19, 2015
  12. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    LDRs are not a good choice for digital inputs. They're generally used with a Picaxe by using an ADC ("A"nalog to "D"igital "C"onverter) pin. Hover your mouse over each pin (Tool Tips) of the 08M2 image and you will see which pins accept ADC input. Typical code and circuitry is found in both manual1 & 2. Read up on the ReadAdc command.

    Cheers
    Chris
     
  13. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Well I have the LDR on the right pins. C.1 and C.2 are ADC.

    I tell you one thing I really dont understand is this general purpose variables.
    I read the manuals and some other stuff online about this but not making 100% sense to me.
    I know what bits and bytes and kinda see how to use them with time? sort of. But yea do not get this code at all.
     
  14. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Would this code not work if when the LDR light was cut from it that it would go to the LDR: code and trip the relay on for 5 seconds?

    Code:
    main:
        if input1 = 0 then high 2 goto LDR endif
        if input1 = 1 then goto main
       
    LDR:
        high c.2
        wait 5
        if input2 = 1 then low 2 high 1 endif
        reset
     
  15. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    I can't imagine how that code can't be puking up errors.

    I told you more than once that LDRs are NOT digital devices. They're ANALOG! Did you lookup the ReadAdc command?

    I will make a deal with you. If you agree to loose the LDR and replace it with a PhotoTransistor I will draw the circuit and write the code for you.

    What say you?
    Chris
     
    Last edited: Oct 21, 2015
  16. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Idk but it works. I even have it built on a solderless breadboard. It works except for the fact that theres still always current flow through the ldr. When light hits it it measures 5.07v. When there is not light it measures around 4.65v.

    I read up on the readadc code yes. But i dont understand the b1 or yhe general purpose variables. I dont 100% understand how the readadc statement works. But i did look up the 08m2 chip and pin 1 and 2 are capable an adc pin.

    I dont want someone to write it for me. I mean it would be nice but i wanna learn this. Ive never done any type of programming before.

    Ive never used a phototransistor before so im going to look it up.
     
  17. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    Read Manual 3 (Interfacing Circuits), page 29 for proper LDR interfacing. A sample ReadAdc code snippet is on the same page.

    The ReadAdc code includes a variable or variables that are needed to store Byte data.

    You can read Manual 1 (Getting Started), page 53 to help understand variables.

    Think of variables as data that's stored in a folders in a file cabinet that you will need to retrieve when needed. For example:
    Code:
    B0 = 4    ;  Store the number "4" in the B0 folder
    B1 = 2    ;  Store the number "2" in the B1 folder
    If If PinC.4 = 0 Then
        B2 = B0 + B1   ; If PinC.4 is low the B2 folder will now contain the value "6".
    Else
        B2 = B0 - B1    ; If PinC.4 is high the B2 folder will now contain the value "2"
    EndIf
    
    Chris
     
    Last edited: Oct 21, 2015
  18. irishluck

    irishluck

    57
    2
    Mar 12, 2014
    Well starting to read manual 3 a little bit and had to learn how transistors worked exactly. Those things are pretty neat and what they can do.
    So much to still learn.
     
  19. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    I ran your code and was shocked that it didn't throw multiple errors. I can tell you this though. Placing multiple commands on a single line is not good programming SOP. It will make debugging longer programs harder to read and harder to fix. It will also inhibit the simulator's ability to pause over each command. In most other languages this practice would be rejected by the programming editor with only few exceptions.

    I consider this OK because the program branches to another code block as soon as it's executed and is one of the few instances that EndIf isn't required.

    Code:
    If PinC.1 = 1 Then GoTo main
    This structure, on the other hand, should be avoided.

    Code:
    If PinC.1 = 0 Then High 2 GoTo LDR EndIf
    Also note that in both examples I changed your "Input" command to "PinC.1". This is because you're using "Input" for something it wasn't intended to do. Why the editor doesn't upchuck on it I don't know. I suggest that you read up on the Input command more thoroughly.

    Regarding LDRs:
    Microcontroller Inputs treat LDRs the same way they treat Potentiometers, Variable Resistors (Rheostats), Thermistors, or just about any device that produces an Analog voltage. These devices are never simply logic 0 or logic 1. The ReadAdc command reads discrete voltage levels broken down into 255 parts and stores each value measured in a Variable. You then tell your program do something when the Variable's value is equal to (=), higher than (>), lower than (<), not equal to (<>) or unchanged from a value you specify or even another variable that changes value somewhere else in your program.

    This block of ReadAdc code was taken from Manual 2 (Basic Commands). In plain English it says "Test the Analog Voltage on PinC.1. Divide the chip's supply voltage (5V) into 255 parts and store the value in the Variable B1. If the value of B1 is greater (>) than 50 Then Go To the flsh block and execute the code there. When finished executing the flsh code go back to main. On the other hand ..If the value in B1 is not greater (<) than 50 Then Go To main instead and keep testing B1 until it's greater than 50.

    Code:
    main:
    readadc C.1,b1 ; read value into b1
    if b1 > 50 then flsh ; jump to flsh if b1 > 50
    goto main ; else loop back to start
    flsh:
    high B.1 ; switch on output B.1
    pause 5000 ; wait 5 seconds
    low B.1 ; switch off output B.1
    goto main ; loop back to start
    I would be remiss if I didn't tell you that the simulator provides for simulating the ReadAdc command by providing you with a Values panel that you can enter the C.1 value. Look in the lower left corner of this screenshot to see this panel. In your case this panel is mimicking an LDR connected to PinC.1 input. I set the value to be 38, which is less than the required 50, so the program never jumps to flsh:. Also look up in the right hand corner to see the current value of B1. You'll see that it's what I set the ADC (C.1) value too.

    Use the Up-Down scroll bar arrows to increase the LDR value to 50 and see what the program does. Then change it to 51 and you will see the program jump to the flsh block.
    This panel is designed to be interactive, so you can change the C.1 value while the program is simulating.

    upload_2015-10-21_11-22-27.png

    Have FUN!
    Chris
     
    Last edited: Oct 21, 2015
  20. Martaine2005

    Martaine2005

    2,910
    796
    May 12, 2015
    Patience of a saint with scabs...
    Are you mended dear Uncle Chris..:p
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-