need help with Picaxe

Discussion in 'Embedded Systems and Microcontrollers' started by appleLady, Nov 15, 2010.

  1. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    can someone help me fix the code?
    i dont know what's wrong with if..else command

    the code :
    '===================
    main:
    readadc 0,b1

    if b1<0 then short
    else if b1>=3 then normal
    else alarm
    end if
    goto normal


    short:
    high 1
    low 2
    low 3
    goto main

    normal:
    low 1
    high 2
    low 3
    goto main


    alarm:
    low 2
    low 1
    high 3
    pause 2000
    low 3
    goto main

    '=====end====


    condition :
    b0<0 short
    b0>=3 normal
    b0 = infinity alarm
     
  2. Rbeckett

    Member

    Sep 3, 2010
    205
    32
    I might suggest that you join the Rev-Ed forum and present that code to them. They are 100% Picaxe and will get you going pretty darn quick over there. Its here http://www.picaxeforum.co.uk/. Free to join and the biggest forum for those chips and programming language. I am a member too, same user name.
    Bob
     
  3. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    already registered but i cant post any thread.

    "appleLady, you do not have permission to access this page. This could be due to one of several reasons:

    Your user account may not have sufficient privileges to access this page. Are you trying to edit someone else's post, access administrative features or some other privileged system?
    If you are trying to post, the administrator may have disabled your account, or it may be awaiting activation."
     
  4. Rbeckett

    Member

    Sep 3, 2010
    205
    32
    PM Me the info and the question and I will post it for you if you would like. It will be a little slower, but at least get it into the Picaxe pro's arena. Hope this helps.
    Bob
     
  5. JoeFromOzarks

    Active Member

    Apr 14, 2010
    95
    27
    Which PicAxe chip are you using? The changes below
    will work with an 18X

    ' === code===
    main:
    readadc 0,b1
    IF b1=0 THEN
    ' minimum value of b1 is 0, maximum value is 255
    ' b1 < 0 can never happen
    GOSUB short
    ELSEIF b1 >= 3 THEN
    ' 3 to 255 is a VERY wide range!!
    GOSUB normal
    ELSE
    ' this will happen ONLY if b1 = 1 or b1 = 2
    GOSUB alarm
    ENDIF
    PAUSE 2000 ' so you can see the output conditions
    GOSUB normal ' return outputs to "normal" (??)
    GOTO main ' do it all again

    short:
    high 1
    low 2
    low 3
    RETURN

    normal:
    low 1
    high 2
    low 3
    RETURN

    alarm:
    low 2
    low 1
    high 3
    pause 2000
    low 3
    RETURN

    ' === end code===
     
  6. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    i used picaxe14

    sorry didn't mention it earlier.
    i think i used the wrong value.
    The want to detect if the value of voltage is within the range of 2v to 3V, then the circuit is normal. other than that, trigger the alarm.
    IF the voltage is zero, then the wire is short circuit.
     
  7. JoeFromOzarks

    Active Member

    Apr 14, 2010
    95
    27
    #rem

    In a "perfect" world, the ADC for each voltage measurement:

    0.0vdc b1=0
    0.5vdc b1=26
    1.0vdc b1=51
    1.5vdc b1=77
    2.0vdc b1=102
    2.5vdc b1=128
    3.0vdc b1=153
    3.5vdc b1=179
    4.0vdc b1=204
    4.5vdc b1=230
    5.0vdc b1=255

    But, without having a circuit diagram I can only guess the actual values.

    Remember, you should have a current limiting resistor on the ADC input (and a prevention circuit to keep the input between 0vdc and 5vdc) which will affect the ADC values!

    #endrem


    IF b1 > 154 THEN ' 154=3.1vdc
    GOSUB alarm ' input is greater than 3.1vdc volts
    ELSEIF b1 >= 102 then ' 102=2.0vdc
    GOSUB normal ' input is between 2.0v and 3.0v
    ELSE ' everything not covered above
    GOSUB short ' below 102 - less than 2.0vdc
    ENDIF



     
  8. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    thanks Joe.
    i'll attach you my circuit.
    [​IMG]
     
  9. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    You need bypass caps on the power & gnd pins. 0.1uF is popular.
    Also put 330ohm resistors inline with the LEDs.
    What's the point of the shorted diode?
     
  10. JoeFromOzarks

    Active Member

    Apr 14, 2010
    95
    27
    I quickly sketched up an interpretation of your schematic.

    Since you didn’t include a “download” circuit, I added the “advanced” circuit. You may stick with the “basic” circuit if you wish.

    I popped in a typical “7805” power supply, feed it with 8-12VDC. Keep the caps as close as possible to the 7805. The 1n4007 across the 7805 protects the 7805 and circuit when power is removed.

    The input 1n4007 protects the circuit from input reversal. (I have a bucket full of 1n4007’s, that’s why I use them verses a 1n4002, or 1n4003…)

    Unless you’re using LED’s with built in resistors, you’ll want to add a 330R (typical, for red LED’s) for each LED, as shown.

    You must have a decoupling capacitor (104 / .1uF ceramic) located as physically close to the power pins of the PicAxe as possible.

    Any unused inputs (IN4/IN3/IN2/IN1) must be tied either to ground V0 (recommended) or to V+ through a 10K0. “Floating” inputs will only cause you grief. Unused outputs may be left floating.

    Between the POT (RV1) and the ADC pin of the PicAxe you’ll want a low value resistor (100R) or you can use two low value resistors between the POT and V+ and another at V0 to limit current to the input pin. Although it’ll change your ADC value, you can easily make up the difference in the code. (I have another bucket full of 1KR’s so I use them, again – simple to adjust for in code.)

    I couldn’t make out the detail on your Q1 (my old eyes) but you may want to consider adding a base resistor and maybe another on the collector.

    I can’t determine the connections or the purpose of your D3.

    Have fun!!

    :) joe
     
    appleLady likes this.
  11. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    Thanks! I'll try to remake my circuit and coding.
     
  12. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    if i run the code,

    Code ( (Unknown Language)):
    1.  
    2. IF b1 > 154 THEN ' 154=3.1vdc
    3. GOSUB alarm ' input is greater than 3.1vdc volts
    4. ELSEIF b1 >= 102 then ' 102=2.0vdc
    5. GOSUB normal ' input is between 2.0v and 3.0v
    6. ELSE ' everything not covered above
    7. GOSUB short ' below 102 - less than 2.0vdc
    8. ENDIF
    9.  
    syntax error
    "syntax check for picaxe-14M failed
    elseif alarm<=128 then gosub normal

    Error : Elseif without if
     
    Last edited by a moderator: Nov 17, 2010
  13. bertus

    Administrator

    Apr 5, 2008
    15,649
    2,348
    Hello,

    With the code tags you can do better formatting of the programs.
    This can make readabilty better.

    Is it possible to use a case command in your compiler?

    Bertus
     
    Last edited: Nov 17, 2010
  14. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    i'm not sure if i can do it using case.
    What i need to do here is to detect if the copper wire is cut.
    since the copper wire got resistance, i replaced the copper wire with potentiometer as i need to done the circuit in simulation only.
     
  15. JoeFromOzarks

    Active Member

    Apr 14, 2010
    95
    27
    @AppleLady
    I've changed the code to reflect the schematic I sketched.

    @Bertus Yes, CASE can be used and I prefer it.

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #picaxe 14M
    4. SYMBOL LED1 = output3 ' short LED
    5. SYMBOL LED2 = output4 ' normal LED
    6. SYMBOL BUZ1 = output5 ' alarm buzzer
    7. SYMBOL AdcPot = b1
    8. main:
    9. readadc 0,AdcPot
    10.  
    11. SELECT CASE AdcPot
    12. CASE > 154   ' 154=3.1vdc
    13.   GOSUB alarm
    14. CASE >= 102   ' 102=2.0vdc
    15.   GOSUB normal
    16. CASE < 102  
    17.   GOSUB short ' below 102 - less than 2.0vdc  
    18. ENDSELECT
    19. GOTO main
    20. short:
    21.   high LED1
    22.   low LED2
    23.   low BUZ1
    24. RETURN
    25. normal:
    26.   low LED1
    27.   high LED2
    28.   low BUZ1
    29. RETURN
    30. alarm:
    31.   low LED2
    32.   low LED1
    33.   high BUZ1
    34.   pause 2000
    35.   low BUZ1
    36. RETURN
    37.  
    38.  
    Using IF/ENDIF
    Code ( (Unknown Language)):
    1.  
    2.  
    3. #picaxe 14M
    4. SYMBOL LED1 = output3 ' short LED
    5. SYMBOL LED2 = output4 ' normal LED
    6. SYMBOL BUZ1 = output5 ' alarm buzzer
    7. SYMBOL AdcPot = b1
    8. main:
    9. readadc 0,AdcPot
    10. IF AdcPot > 154 THEN ' 154=3.1vdc
    11.   GOSUB alarm ' input is greater than 3.1vdc volts
    12. ELSEIF AdcPot >= 102 THEN ' 102=2.0vdc
    13.   GOSUB normal ' input is between 2.0v and 3.0v
    14. ELSE ' everything not covered above
    15.   GOSUB short ' below 102 - less than 2.0vdc
    16. ENDIF
    17. GOTO main
    18. short:
    19.   high LED1
    20.   low LED2
    21.   low BUZ1
    22. RETURN
    23. normal:
    24.   low LED1
    25.   high LED2
    26.   low BUZ1
    27. RETURN
    28. alarm:
    29.   low LED2
    30.   low LED1
    31.   high BUZ1
    32.   pause 2000
    33.   low BUZ1
    34. RETURN
    35.  
    joe
     
    appleLady likes this.
  16. JoeFromOzarks

    Active Member

    Apr 14, 2010
    95
    27
    Oh, well, that changes things significantly.

    Is this what you're thinking will happen? The RED-X indicates the open circuit.
     
    • Open.JPG
      Open.JPG
      File size:
      116.5 KB
      Views:
      22
  17. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    yes. something like that. do you think my circuit is relevant?
     
  18. JoeFromOzarks

    Active Member

    Apr 14, 2010
    95
    27
    Maybe.

    (My back is really screaming today. I was struck by a car, awaiting back surgery and I'm not thinking clearly. )

    Is the power supply feeding the PicAxe the same power supply feeding your "copper" wire? If both are using the same power supply, then yes, it'll work. How long is your "copper" wire?

    If not, you might want to look at an inexpensive opto-isolator.

    joe
     
  19. appleLady

    Thread Starter New Member

    Nov 15, 2010
    11
    0
    OMG!
    Are you ok?

    The copper wire will be about 2km.. I already calculate that the value of resistance is about 165.7Ω and if i put 4.7kΩ in series with the potentiometer, i got the value of 2.5V
     
  20. JoeFromOzarks

    Active Member

    Apr 14, 2010
    95
    27
    2km? As in, over a mile? ‘Round these parts we call that an antenna or lightning rod. It would not be wise to connect a wire that long to an input of any PicAxe, or many other chips!

    You’ll need to come up with something to interface to the PicAxe. To cut down on wire resistance (2km, really?) you might want to think about running a low voltage alternating current through your wire, then change it to DC for input to an opto, then to the PicAxe.

    I’ll stew on some alternatives. Today is a “screaming back” day, I’m not sure what my brain will let me consider. Tell us more about this 2km length of wire.

    joe
     
Loading...